AndroidRecyclerView选择多个item的实现代码-创新互联

模仿网易新闻客户端阅读偏好的频道选择,先看实现的页面:

创新互联公司是一家专业提供华龙企业网站建设,专注与做网站、网站设计、H5网站设计、小程序制作等业务。10年已为华龙众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。


直接上代码:

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);
          }
        }
      }
    });
  }
}


文章名称:AndroidRecyclerView选择多个item的实现代码-创新互联
地址分享:http://hbruida.cn/article/doiisp.html