最近在項目中遇到一個需求,就是點(diǎn)擊選擇按鈕,彈出一個選擇列表,并且可以實(shí)現(xiàn)多選的功能,現(xiàn)在我將這個功能寫出來,讓大伙瞧瞧,
先上一個效果圖(只看功能哈,其他的不是重點(diǎn))

22.gif
效果就是這個效果,下面我們直接開始代碼吧!
1.數(shù)據(jù)實(shí)體類
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="選擇設(shè)備" />
<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.綁定數(shù)據(jù)的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);//設(shè)置實(shí)體類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) {
//實(shí)現(xiàn)自定義接口,供MainActivity調(diào)用
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,從此一心一意寫代碼。
..