Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作

這些時(shí)間做安卓盒子項(xiàng)目,因?yàn)榘沧侩娨暤娘@示器比較大,所以一個(gè)界面顯示 很多數(shù)據(jù) ,最多的時(shí)候,一個(gè)Actvity中用到了好幾個(gè)RecyclerView。

創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,公司以網(wǎng)站制作、做網(wǎng)站、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計(jì)等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶上1000家,涉及國內(nèi)多個(gè)省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗(yàn)。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計(jì)、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計(jì)、獨(dú)特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。

在RecyclerView中實(shí)現(xiàn)Item選中處理時(shí),發(fā)現(xiàn)用CheckBox的OnCheckedChangeListener監(jiān)聽事件時(shí),會達(dá)不到預(yù)期,所以用了OnClickListener來實(shí)現(xiàn)。

主界面代碼:

public class CheckRecyclerViewActivity extends AppCompatActivity implements CheckAdapter.CheckItemListener {
  //適配器
  private CheckAdapter mCheckAdapter;
  //列表
  private RecyclerView check_rcy;
  //全選操作
  private CheckBox check_all_cb;
  //列表數(shù)據(jù)
  private List<CheckBean> dataArray;
  //選中后的數(shù)據(jù)
  private List<CheckBean> checkedList;
  private boolean isSelectAll;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_recyclerview);
    checkedList = new ArrayList<>();
    initDatas();
    initViews();
  }

  private void initViews() {
    check_rcy = (RecyclerView) findViewById(R.id.check_rcy);
    check_all_cb = (CheckBox) findViewById(R.id.check_all_cb);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    check_rcy.setLayoutManager(linearLayoutManager);
    mCheckAdapter = new CheckAdapter(this, dataArray, this);
    check_rcy.setAdapter(mCheckAdapter);
    //如果使用CheckBox的OnCheckedChangeListener事件,則選中事件會有一些意想不到的結(jié)果,歡迎體驗(yàn)
    //在列表Item中的CheckBox也一樣的效果
    check_all_cb.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        isSelectAll = !isSelectAll;
        checkedList.clear();
        if (isSelectAll) {//全選處理
          checkedList.addAll(dataArray);
        }
        for (CheckBean checkBean : dataArray) {
          checkBean.setChecked(isSelectAll);
        }
        mCheckAdapter.notifyDataSetChanged();
      }
    });
  }

  private void initDatas() {
    dataArray = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
      CheckBean bean = new CheckBean();
      bean.setOrder(String.valueOf(i + 1));
      bean.setName("名稱_" + i);
      bean.setContent("第" + i + "條內(nèi)容");
      bean.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
      dataArray.add(bean);
    }
  }

  @Override
  public void itemChecked(CheckBean checkBean, boolean isChecked) {
    //處理Item點(diǎn)擊選中回調(diào)事件
    if (isChecked) {
      //選中處理
      if (!checkedList.contains(checkBean)) {
        checkedList.add(checkBean);
      }
    } else {
      //未選中處理
      if (checkedList.contains(checkBean)) {
        checkedList.remove(checkBean);
      }
    }
    //判斷列表數(shù)據(jù)是否全部選中
    if (checkedList.size() == dataArray.size()) {
      check_all_cb.setChecked(true);
    } else {
      check_all_cb.setChecked(false);
    }
  }
}



列表數(shù)據(jù)適配器:

public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.ViewHolder> {
  private Context mContext;
  private List<CheckBean> mDatas;
  private CheckItemListener mCheckListener;

  public CheckAdapter(Context mContext, List<CheckBean> mDatas, CheckItemListener mCheckListener) {
    this.mContext = mContext;
    this.mDatas = mDatas;
    this.mCheckListener = mCheckListener;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.check_recyclerview_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    final CheckBean bean = mDatas.get(position);
    holder.item_order_tv.setText(bean.getOrder());
    holder.item_name_tv.setText(bean.getName());
    holder.item_content_tv.setText(bean.getContent());
    holder.item_time_tv.setText(bean.getTime());
    holder.item_cb.setChecked(bean.isChecked());
    //點(diǎn)擊實(shí)現(xiàn)選擇功能,當(dāng)然可以把點(diǎn)擊事件放在item_cb對應(yīng)的CheckBox上,只是焦點(diǎn)范圍較小
    holder.item_content_ll.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        bean.setChecked(!bean.isChecked());
        holder.item_cb.setChecked(bean.isChecked());
        if (null != mCheckListener) {
          mCheckListener.itemChecked(bean, holder.item_cb.isChecked());
        }
        notifyDataSetChanged();
      }
    });
  }

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

  public class ViewHolder extends RecyclerView.ViewHolder {
    //序號
    private TextView item_order_tv;
    //選擇
    private CheckBox item_cb;
    //整個(gè)條目
    private LinearLayout item_content_ll;
    //名稱
    TextView item_name_tv;
    //內(nèi)容
    TextView item_content_tv;
    //時(shí)間
    private TextView item_time_tv;


    public ViewHolder(View itemView) {
      super(itemView);
      item_order_tv = (TextView) itemView.findViewById(R.id.item_order_tv);
      item_cb = (CheckBox) itemView.findViewById(R.id.item_cb);
      item_name_tv = (TextView) itemView.findViewById(R.id.item_name_tv);
      item_content_tv = (TextView) itemView.findViewById(R.id.item_content_tv);
      item_time_tv = (TextView) itemView.findViewById(R.id.item_time_tv);
      item_content_ll = (LinearLayout) itemView.findViewById(R.id.item_content_ll);
    }
  }

  public interface CheckItemListener {

    void itemChecked(CheckBean checkBean, boolean isChecked);
  }
}



測試數(shù)據(jù)實(shí)體BEAN:

public class CheckBean implements Serializable {
  private String order;
  private String name;
  private String content;
  private String time;
  private boolean isChecked;

  public String getOrder() {
    return order;
  }

  public void setOrder(String order) {
    this.order = order;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public String getTime() {
    return time;
  }

  public void setTime(String time) {
    this.time = time;
  }

  public boolean isChecked() {
    return isChecked;
  }

  public void setChecked(boolean checked) {
    isChecked = checked;
  }
}



主界面布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@drawable/drawable_white_round_bg"
    android:orientation="vertical">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="20dp"
      android:orientation="horizontal">

    <TextView
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:text="序號"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <CheckBox
        android:id="@+id/check_all_cb"
        android:layout_width="12dp"
        android:layout_gravity="center"
        android:button="@null"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="12dp"
        android:background="@drawable/drawable_cb_selector"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="名稱"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <TextView
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="內(nèi)容"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <TextView
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:text="時(shí)間"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"
        android:textColor="#333333"
        android:gravity="center"
        />
  </LinearLayout>

  <View
      android:layout_width="match_parent"
      android:layout_height="1px"
      android:background="#bcbcbc"></View>

  <android.support.v7.widget.RecyclerView
      android:id="@+id/check_rcy"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

</LinearLayout>

列表Item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

  <LinearLayout
      android:id="@+id/item_content_ll"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:orientation="horizontal">

    <TextView
        android:id="@+id/item_order_tv"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <CheckBox
        android:id="@+id/item_cb"
        android:layout_width="20dp"
        android:layout_gravity="center"
        android:button="@null"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="20dp"
        android:background="@drawable/drawable_cb_selector"
        />

    <TextView
        android:id="@+id/item_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <TextView
        android:id="@+id/item_content_tv"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textColor="#333333"
        android:gravity="center"
        />

    <TextView
        android:id="@+id/item_time_tv"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textColor="#333333"
        android:gravity="center"
        />
  </LinearLayout>

  <View
      android:layout_width="match_parent"
      android:layout_height="1px"
      android:background="#bcbcbc"/>
</LinearLayout>

界面布局是隨意寫的,請根據(jù)實(shí)際情況調(diào)整。上丑圖:

Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作

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

新聞名稱:Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作
標(biāo)題鏈接:http://muchs.cn/article16/gehodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、營銷型網(wǎng)站建設(shè)、網(wǎng)站排名網(wǎng)站改版、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司