项目需要实现一个功能效果,要保持被选中条目的选中状态,其余的都保持未被选中的状态:
如图:
接收人下面是一个recyclerview(外层):
布局文件:
外层Recyclerview的Adapter布局: adapter_receivertitile
然后内层recyclerview的adapter布局:adapter_grid_receiver
下面上代码:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter mAdapter = new MyAdapter(R.layout.adapter_receivertitile, datalist);
mRecyclerView.setAdapter(mAdapter);
private RadioButton oldView ; //定义一个view记录被点击的view
public class MyAdapter extends BaseQuickAdapter{
public MyAdapter(int layoutResId, Listdata) {
super(layoutResId, data);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
final ListusersList = datalist.get(position).getUsers();
mGridRecyclerView.setHasFixedSize(true);
mGridRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),3));
mGrideAdapter = new MyGridAdapter(R.layout.adapter_grid_receiver,usersList);
mGridRecyclerView.setAdapter(mGrideAdapter);
mGrideAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
selectedPersonName = usersList.get(position).getRealName();
selectedUserId = usersList.get(position).getUserId();
//强转成radiobutton
RadioButton radioButton = (RadioButton) view;
if(oldView!=null){
//将上一条被点击的设置为未选中
oldView.setChecked(false);
}
//记录当前被点击的item
oldView = radioButton;
}
});
}
@Override
protected void convert(BaseViewHolder helper, Object item) {
ReceiverBean bean= (ReceiverBean) item;
helper.setText(R.id.tv_title,bean.getAreaName());
mGridRecyclerView = helper.getView(R.id.recyclerview2);
}
}
class MyGridAdapter extends BaseQuickAdapter{
public MyGridAdapter(int layoutResId, Listdata) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, Object item) {
ReceiverBean.UsersBean bean= (ReceiverBean.UsersBean) item;
helper.setText(R.id.tv_name,bean.getRealName());
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
}
}对了,recyclerview用的是github上的框架--BaseRecyclerViewAdapterHelper
这里我就不解释怎么用了,大家如果感兴趣可以自己去搜.
顺便记一笔:(跟上面的无关)
设置Gridlayout布局的时候 item的列数,间距设置
int spanCount = 3; // 3 columns
int spacing = 30; // 30px
boolean includeEdge = false;
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge)
有个需求里面嵌套的recyclerview是多张图片,结果习惯性的按ListView那种布局设置这个,导致图片显示的高度不一致.汗....