RecyclerView 使用 一次只能选择一个条目 到下一个页面
程序员文章站
2022-05-14 19:37:21
...
public class SelectSurveyedPeopleAdapter extends RecyclerView.Adapter {
private LayoutInflater inflater;
private Context mContext;
private List<Staff> mListDatas;
private int currentSelect = -1;
public SelectSurveyedPeopleAdapter(Context context, List<Staff> listDatas) {
mContext = context;
mListDatas = listDatas;
inflater = LayoutInflater.from(mContext);
}
public Staff getCurrent() {
if (currentSelect >= 0) {
return mListDatas.get(currentSelect);
}
return null;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_select_survey_list, parent, false);
SelectSurveyedPeopleViewHolder holder = new SelectSurveyedPeopleViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
SelectSurveyedPeopleViewHolder viewHolder = (SelectSurveyedPeopleViewHolder) holder;
viewHolder.setData((position));
}
@Override
public int getItemCount() {
if (mListDatas != null) {
return mListDatas.size();
}
return 0;
}
class SelectSurveyedPeopleViewHolder extends RecyclerView.ViewHolder {
public SelectSurveyedPeopleViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
@BindView(R.id.checkbox)
CheckBox mCheckbox;
@BindView(R.id.tv_survey_name)
TextView mTvSurveyName;
@BindView(R.id.tv_survey_unit)
TextView mTvSurveyUnit;
@BindView(R.id.tv_survey_dep)
TextView mTvSurveyDep;
@BindView(R.id.tv_survey_phone)
TextView mTvSurveyPhone;
public void setData(final int pos) {
final Staff staff = mListDatas.get(pos);
if (!TextUtils.isEmpty(staff.staffName)) {
mTvSurveyName.setText(staff.staffName);
}
if (!TextUtils.isEmpty(staff.address)) {
mTvSurveyUnit.setText(staff.address);
}
if (!TextUtils.isEmpty(staff.orginazitionName)) {
mTvSurveyDep.setText(staff.orginazitionName);
}
if (!TextUtils.isEmpty(staff.mobileNo)) {
mTvSurveyPhone.setText(staff.mobileNo);
}
mCheckbox.setChecked(false);
//当前选中的
mCheckbox.setChecked(pos == currentSelect);
mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && currentSelect != pos) {
mCheckbox.setChecked(true);
if (currentSelect != -1) {
notifyItemChanged(currentSelect, 0);
}
currentSelect = pos;
}
if (!isChecked && currentSelect == pos) {
currentSelect = -1;
}
}
});
}
}
}
public class MainActivity extends AppCompatActivity {
@BindView(R.id.recyclerview)
RecyclerView mRecyclerview;
private SelectSurveyedPeopleAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
}
private void initData() {
List<Staff> staffList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Staff staff = new Staff();
staff.staffName = "姓名=" + i;
staff.address = "单位=" + i;
staff.orginazitionName = "部门=" + i;
staff.mobileNo = "电话=" + i;
staffList.add(staff);
}
mAdapter = new SelectSurveyedPeopleAdapter(this, staffList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置布局管理器
mRecyclerview.setLayoutManager(layoutManager);
//设置为垂直布局,这也是默认的
layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
mRecyclerview.setAdapter(mAdapter);
//设置增加或删除条目的动画
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
}
@OnClick({R.id.tv_confrim, R.id.tv_cancel})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tv_confrim:
//带数据到许下一个页面
Staff current = mAdapter.getCurrent();
if (current != null) {
Intent intent = new Intent(MainActivity.this, GetActivity.class);
startActivity(intent);
}
break;
case R.id.tv_cancel:
break;
}
}
}