ListView设置不同item(包涵radiobutton和EditText),并解决滑动listview后radiobutton和EditText内容被清空的情况

本文介绍了一种使用自定义Adapter实现复杂列表的方法,通过不同的布局类型来区分输入项和确认项,实现了输入框和单选按钮组的功能,并且能够根据用户选择更新视图状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Override
public int getItemViewType(int position) {
int type = 0;
if (jcxtypeList.get(position).equals(TYPE_INPUT)) {
type = 0;
} else if (jcxtypeList.get(position).equals(TYPE_CONFIRM)) {
type = 1;
}
return type;
}

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = null;
int type = getItemViewType(position);
switch (type) {
// 输入项
case 0:
view = inflater.inflate(R.layout.item_input, null);
TextView tvName = (TextView) view
.findViewById(R.id.tv_input_name);
final EditText etInputValue = (EditText) view
.findViewById(R.id.et_input_value);

etInputValue.setFocusable(true);
etInputValue.setFocusableInTouchMode(true);


tvName.setText((position + 1) + "、" + jcxnameList.get(position));

// 获取焦点
etInputValue.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
selectedPosition = position;
}
return false;
}
});

if (selectedPosition == position) {
// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
etInputValue.requestFocus();

} else {
etInputValue.clearFocus();
}

etInputValue.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void afterTextChanged(Editable s) {
hashMap.put(position, s.toString());
}
});

// 如果hashMap不为空,就设置的editText
if (hashMap.get(position) != null) {
etInputValue.setText(hashMap.get(position));
}
break;

// 确认项
case 1:

view = inflater.inflate(R.layout.item_confirm, null);
TextView tvConfirmName = (TextView) view
.findViewById(R.id.tv_confirm_name);

tvConfirmName.setText((position + 1) + "、"
+ jcxnameList.get(position));

RadioGroup rgConfirm = (RadioGroup) view
.findViewById(R.id.rg_confirm);

RadioButton rbtnYes = (RadioButton) view
.findViewById(R.id.rbtn_yes);
RadioButton rbtnNo = (RadioButton) view
.findViewById(R.id.rbtn_no);
RadioButton rbtnNotExist = (RadioButton) view
.findViewById(R.id.rbtn_not_exist);

rgConfirm.setFocusable(true);
rgConfirm.setFocusableInTouchMode(true);

rgConfirm.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
selectedPosition = position;
}
return false;
}
});

if (selectedPosition == position) {
// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
rgConfirm.requestFocus();
} else {
rgConfirm.clearFocus();
}

rgConfirm
.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
switch (checkedId) {
case R.id.rbtn_yes:
radioMap.put(position, 0);
break;
case R.id.rbtn_no:
radioMap.put(position, 1);
break;
case R.id.rbtn_not_exist:
radioMap.put(position, 2);
break;

default:
break;
}
}
});

// 如果hashMap不为空,就设置的editText
if (radioMap.get(position) != null) {
switch (radioMap.get(position)) {
case 0:
rgConfirm.check(R.id.rbtn_yes);
break;
case 1:
rgConfirm.check(R.id.rbtn_no);
break;
case 2:
rgConfirm.check(R.id.rbtn_not_exist);
break;
default:
break;
}
}
break;
default:
break;
}

return view;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值