真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

AndroidRecyclerView選擇多個item的實(shí)現(xiàn)代碼

模仿網(wǎng)易新聞客戶端閱讀偏好的頻道選擇,先看實(shí)現(xiàn)的頁面:

創(chuàng)新互聯(lián)自2013年起,先為通山等服務(wù)建站,通山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為通山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Android RecyclerView選擇多個item的實(shí)現(xiàn)代碼

直接上代碼:

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
  private RecyclerView recycler;
  private RecyclerAdapter mAdapter;
  private List channels = new ArrayList<>();
  private List channelsSelected;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view_acitivity);

    initData();

    initUI();

    findViewById(R.id.resultBTN).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int lens = channelsSelected.size();
        for (int i =0 ; i < lens; i++) {
          PreferCustomizableChannel customizableChannel = channelsSelected.get(i);
          if (customizableChannel.isSelected()) {
            Log.i("RecyclerViewActivity", "onClick: "+customizableChannel.toString());
          }
        }
      }
    });
  }

  private void initData() {
    Resources resources = getResources();
    TypedArray array = resources.obtainTypedArray(R.array.prefer_channel_icon);
    int len = array.length();
    String[] name = resources.getStringArray(R.array.prefer_channel_name);
    for (int i = 0; i < len; i++) {
      PreferCustomizableChannel customizableChannel = new PreferCustomizableChannel();
      customizableChannel.setChannel(name[i]);
      customizableChannel.setResId(array.getResourceId(i, 0));
      customizableChannel.setSelected(false);
      customizableChannel.setId(i * 100);
      channels.add(customizableChannel);
    }
    array.recycle();
    channelsSelected = channels;
  }

  private void initUI() {
    recycler = (RecyclerView) findViewById(R.id.recycler);
    final GridLayoutManager manager = new GridLayoutManager(this, 3);
    recycler.setLayoutManager(manager);
    recycler.setHasFixedSize(true);
    recycler.setItemAnimator(new DefaultItemAnimator());
    mAdapter = new RecyclerAdapter(RecyclerViewActivity.this, channels);
    recycler.setAdapter(mAdapter);
    mAdapter.setClickListener(new OnRecyclerViewItemClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        SparseBooleanArray selecteds = mAdapter.getSelectedItem();
        int len = channels.size();
        for (int i = 0; i < len; i++) {
          if (selecteds.get(i)) {
            channelsSelected.get(position).setSelected(true);
          }
        }
      }
    });
  }
}

布局文件RecyclerView橫豎都是“match_parent”,否則在點(diǎn)擊的時候gridView會自動上滑一段距離。

適配器的實(shí)現(xiàn):

import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.entity.PreferCustomizableChannel;
import com.listener.OnRecyclerItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PreferChannelAdapter extends RecyclerView.Adapter{
  private List lists;
  private OnRecyclerItemClickListener listener;
  private SparseBooleanArray selectLists = new SparseBooleanArray();

  public PreferChannelAdapter() {
  }

  public void setDatas(List lists) {
    this.lists = lists;
    notifyDataSetChanged();
  }

  public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
    this.listener = listener;
  }

  @Override
  public PreferChannelHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prefer_channel_item, null);
    return new PreferChannelHolder(view);
  }

  @Override
  public void onBindViewHolder(final PreferChannelHolder holder, final int position) {
    PreferCustomizableChannel channelItem = lists.get(position);
    holder.channelItemTV.setText(channelItem.getChannel());
    holder.channelItemImg.setImageResource(channelItem.getResId());
    if (!selectLists.get(position)) {
      holder.selectedMarkImg.setVisibility(View.GONE);
    } else {
      holder.selectedMarkImg.setVisibility(View.VISIBLE);
    }
    holder.preferChannelItemLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if (holder.selectedMarkImg.getVisibility() == View.GONE) {
          holder.selectedMarkImg.setVisibility(View.VISIBLE);
          selectLists.put(position, true);
        } else if (holder.selectedMarkImg.getVisibility() == View.VISIBLE){
          holder.selectedMarkImg.setVisibility(View.GONE);
          selectLists.put(position, false);
        }
        listener.onRecyclerClick(position);
      }
    });
  }

  @Override
  public int getItemCount() {
    return lists.size();
  }

  public class PreferChannelHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.preferChannelItemLayout)
    RelativeLayout preferChannelItemLayout;
    @BindView(R.id.channelItemTV)
    TextView channelItemTV;
    @BindView(R.id.channelItemImg)
    ImageView channelItemImg;
    @BindView(R.id.selectedMarkImg)
    ImageView selectedMarkImg;

    public PreferChannelHolder(View itemView) {
      super(itemView);
      ButterKnife.bind(this, itemView);
    }
  }

  public SparseBooleanArray getSelectedItem() {
    return selectLists;
  }
}

順便把item的布局也貼出來吧:

<?xml version="1.0" encoding="utf-8"?>

  
  
  
  

其他注意的地方:

SpareBooleanArrary.size()返回的是已經(jīng)設(shè)置為true的長度,比如選擇了一個則返回1,選擇了10則返回10,但在選擇了10后去掉一個的時候,size()返回的并不是9,而依然是10,這點(diǎn)注意,所以在遍歷的時候并沒有使用使用size()取長度。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


文章名稱:AndroidRecyclerView選擇多個item的實(shí)現(xiàn)代碼
文章鏈接:http://weahome.cn/article/gjeeeo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部