美文网首页自定义ViewAndroid开发Android 开发技术分享
RecyclerView结合PopupWindow实现多选功能

RecyclerView结合PopupWindow实现多选功能

作者: 根艮哏艮根 | 来源:发表于2017-09-08 10:27 被阅读123次

最近在项目中遇到一个需求,就是点击选择按钮,弹出一个选择列表,并且可以实现多选的功能,现在我将这个功能写出来,让大伙瞧瞧,
先上一个效果图(只看功能哈,其他的不是重点)

22.gif

效果就是这个效果,下面我们直接开始代码吧!

1.数据实体类

    private String name;
    private String id;
    private boolean isChosed;//用来判断是否是选中的

    public PopDataBean(String name, String id, boolean isChosed) {
        this.name = name;
        this.id = id;
        this.isChosed = isChosed;
    }

    public boolean isChosed() {
        return isChosed;
    }

    public void setChosed(boolean chosed) {
        isChosed = chosed;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

2.MainActivity的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    android:orientation="vertical"
    tools:context="com.text.multipletext.MainActivity">

    <LinearLayout
        android:id="@+id/ll_chose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:padding="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择设备" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:src="@mipmap/down" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_show_chosed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ffffff">

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

    </LinearLayout>


</LinearLayout>

3.主要是RecyclerView的使用,如果你还没有使用过RecyclerView的话,建议你试着去用一下,你去看看鸿洋大神对RecyclerView的理解,传送门:http://blog.csdn.net/lmj623565791/article/details/45059587
4.绑定数据的Adapter

public class PopDataAdapter extends RecyclerView.Adapter<PopViewHolder> {

    private Context context;
    private List<PopDataBean> listBean;
    public Map<String, Object> maps;

    public PopDataAdapter(Context context, List<PopDataBean> listBean) {
        this.context = context;
        this.listBean = listBean;
        initData();//初始化
    }

    private void initData() {
        maps = new HashMap<>();
        for (int i = 0; i < listBean.size(); i++) {
            maps.put(listBean.get(i).getName(), listBean.get(i));
        }
    }


    @Override
    public PopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PopViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.layout_pop_item, parent, false));
    }

    @Override
    public void onBindViewHolder(PopViewHolder holder, final int position) {
        final PopDataBean dataBean = listBean.get(position);
        holder.cbPopItem.setChecked(((PopDataBean) (maps.get(dataBean.getName()))).isChosed());
        holder.cbPopItem.setEnabled(!((PopDataBean) (maps.get(dataBean.getName()))).isChosed());
        holder.tvPopItemName.setText(dataBean.getName());
        holder.cbPopItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                dataBean.setChosed(isChecked);//设置实体类chosed的值
                //将改变后的值添加到map集合中
                maps.put(listBean.get(position).getName(), listBean.get(position));
            }
        });
    }


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

5.子布局的Adapter

public class ChoseDataAdapter extends RecyclerView.Adapter<ChosedViewHolder> {
    private Context mContext;
    private List<PopDataBean> listBeanChosed;
    private OnDeleteListenter onDeleteListenter;

    public ChoseDataAdapter(Context mContext, List<PopDataBean> listBeanChosed) {
        this.mContext = mContext;
        this.listBeanChosed = listBeanChosed;
    }

    @Override
    public ChosedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChosedViewHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.layout_chosed_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ChosedViewHolder holder, final int position) {
        final String name = listBeanChosed.get(position).getName();
        holder.tvChoseName.setText(name);
        holder.imDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现自定义接口,供MainActivity调用
                getOnDeleteListenter().onDelete(position, name);
            }
        });
    }

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

    public OnDeleteListenter getOnDeleteListenter() {
        return onDeleteListenter;
    }

    public void setOnDeleteListenter(OnDeleteListenter onDeleteListenter) {
        this.onDeleteListenter = onDeleteListenter;
    }
}

6.定义删除接口

public interface OnDeleteListenter {
    /**
     * 删除接口
     * @param position
     * @param name
     */
    void onDelete(int position, String name);
}

完整的demo,下载传送门:http://download.csdn.net/download/qq_32376365/9969522
另附findbyviewid的好帮手:https://www.buzzingandroid.com/tools/android-layout-finder/,各位可以看看,一键帮你解决繁琐的findbyviewid,从此一心一意写代码。

..

相关文章

网友评论

    本文标题:RecyclerView结合PopupWindow实现多选功能

    本文链接:https://www.haomeiwen.com/subject/pkkgjxtx.html