经验总结-RecyclerView列表中获取每个item中已经选择的CheckBox值

最近在做一个公司的项目—平板广告机,相当于一个超大手机里面打开一个App。需求中有一个功能是从服务器获取广告机可能出现的问题数据,Android开发人员用列表显示,这些数据是给公司的检修人员看的。


需求:点击页面上的一个按钮,在页面上弹出一个包含列表的PopuWindow,列表的每一项中只含有一个CheckBox控件,如果选中相应的CheckBox控件,那么当检修人员点击提交按钮时,会把每个选中的CheckBox值传给服务器。


需求已经很清楚了,那我就写一个简单易懂的demo重现我们刚才的需求:


上项目列表


上布局文件

1.activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xgr.checkbox.MainActivity">

    <Button
        android:id="@+id/btn_skip_this"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="跳转"/>

    <RelativeLayout
        android:id="@+id/rl_bottom"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:background="#cccccc">

        <ImageView
            android:id="@+id/iv_show"
            android:layout_width="64dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:src="@drawable/ic_launcher_round"/>

        <ImageView
            android:id="@+id/iv_dismiss"
            android:layout_width="64dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher_round"/>
    </RelativeLayout>

</RelativeLayout>



2.popuwindow_repair.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_submit"
        android:padding="5dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        android:layout_marginBottom="12dp"
        android:text="提交"/>

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

    <CheckBox
        android:id="@+id/checkbox_problem"
        android:layout_centerInParent="true"
        android:text="问题检修"
        android:layout_width="wrap_content"
        android:layout_height="48dp"/>

</RelativeLayout>

上java代码
 
 
1.MainActivity
 
package com.xgr.checkbox;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView ivShow;
    private ImageView ivDismiss;
    private RelativeLayout rlBottom;
    private Button btnSkip;
    private PopupWindow mPopupWindow;

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

        setViews();
        setListeners();

    }

    private void setViews() {
        ivShow = (ImageView) findViewById(R.id.iv_show);
        ivDismiss = (ImageView) findViewById(R.id.iv_dismiss);
        rlBottom= (RelativeLayout) findViewById(R.id.rl_bottom);
        btnSkip= (Button) findViewById(R.id.btn_skip_this);
    }

    private void setListeners() {
        ivShow.setOnClickListener(this);
        ivDismiss.setOnClickListener(this);
        btnSkip.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_show:
                if(mPopupWindow!=null){
                    return;
                }
                mPopupWindow=new CustomPopuWindow(this,R.layout.popuwindow_repair,0);
                mPopupWindow.showAtLocation(rlBottom,0,0, Gravity.NO_GRAVITY);
                break;
            case R.id.iv_dismiss:
                if(mPopupWindow!=null){
                    mPopupWindow.dismiss();
                    mPopupWindow=null;
                    return;
                }
                break;
            case R.id.btn_skip_this:
                Intent intent=new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

}

2.BeanAdapter.class
 
 
 
package com.xgr.checkbox;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

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

/**
 * Created by xgr on 2017/11/25.
 */

public class BeanAdapter extends RecyclerView.Adapter<BeanAdapter.ViewHolder> {

    private Context context;
    private List<JavaBean> list;
    private List<String> checklist;

    public List<String> getChecklist() {
        return checklist;
    }

    public void setChecklist(List<String> checklist) {
        this.checklist = checklist;
    }

    public BeanAdapter(Context context, List<JavaBean> list) {
        this.context = context;
        this.list = list;
        checklist=new ArrayList<>();
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        JavaBean bean = list.get(position);
        holder.checkBox.setText(bean.getName());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    checklist.add(holder.checkBox.getText().toString());
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;

        public ViewHolder(View view) {
            super(view);
            checkBox = (CheckBox) view.findViewById(R.id.checkbox_problem);
        }
    }
}


3.JavaBean.class
 
 
package com.xgr.checkbox;

import java.io.Serializable;

/**
 * Created by xgr on 2017/11/24.
 */

public class JavaBean implements Serializable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
4.CustomPopuWindow.class
 
package com.xgr.checkbox;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

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

/**
 * Created by xgr on 2017/11/25.
 */

public class CustomPopuWindow extends PopupWindow {
    private Context context;
    private View mPopuWindow;

    private RecyclerView mRecyclerView;
    private List<JavaBean> list = new ArrayList<>();
    private BeanAdapter adapter;
    private Button btnSubmit;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
            }
        }
    };

    public CustomPopuWindow(Context context, int resLayout, int anchorHeight) {
        super(context);
        this.context = context;
        initResLayout(resLayout);
        setPopuWindow(anchorHeight);
        initData(resLayout);
    }

    private void initResLayout(int resLayout) {
        mPopuWindow = LayoutInflater.from(context).inflate(resLayout, null);
    }

    private void setPopuWindow(int anchorHeight) {
        this.setContentView(mPopuWindow);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(800);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
    }

    private void initData(int resLayout) {
        //报修弹窗布局
        if (resLayout == R.layout.popuwindow_repair) {
            initData();
            setViews();
            setAdapter();
            setListeners();
        }
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            JavaBean bean = new JavaBean();
            bean.setName("视频卡顿");
            list.add(bean);
        }
    }

    private void setViews() {
        mRecyclerView = (RecyclerView) mPopuWindow.findViewById(R.id.recycler_view);
        btnSubmit = (Button) mPopuWindow.findViewById(R.id.btn_submit);
    }

    private void setListeners() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitData();
            }
        });
    }

    private void submitData() {
        if(adapter.getChecklist().size()<=0){
            Toast.makeText(context,"您还未选择报修原因",Toast.LENGTH_LONG).show();
            return;
        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<adapter.getChecklist().size();i++){
            String name=adapter.getChecklist().get(i);
            sb.append(name+",");
        }

        String param=sb.toString().substring(0,sb.length()-1);
        //联网上传数据
        //成功后handler.send.....在activity中取消弹窗,也可直接dismiss(),但是直接dismiss()
        //并没有销毁PopupWindow实例
        this.dismiss();
    }

    private void setAdapter() {
        int number = 0;//用于指定列数,目的:让数据居中显示
        if (list.size() > 1) {
            number = 2;//当数据大于1条时,列表分两列显示
        } else {
            number = 1;//当数据小于2条时,列表为一列显示,且数据居中
        }
        GridLayoutManager manager = new GridLayoutManager(context, number);
        mRecyclerView.setLayoutManager(manager);
        adapter = new BeanAdapter(context, list);
        mRecyclerView.setAdapter(adapter);
    }

}
ok,代码一股脑上完了,就是一个简单的demo,不是很难,但是很精髓。我也不过多的讲解了,大家试一试吧
 




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xie_guo_rong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值