我创建了listview,它在每一行都提供了独特的视图,但我添加了复选框。我遇到了一个问题,即检查第一项并滚动列表。奇怪的匿名项目,检查。你能为此提供解决方案吗?谢谢
@PreDestroy
答案 0 :(得分:1)
您必须创建一个包含要在listview行中显示的所有组件(EX:ComponentClass)的类。在这个类中创建布尔值和所有setter / getter和.... 在适配器Java类范围arrayAdapter中,您必须创建名为ViewHolder的内部类,并使用此内部类来显示适配器的组件。
static class ViewHolder {
TextView text;
CheckBox checkbox;
}
在getView方法中,使用blew code:
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
ComponentClass.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
答案 1 :(得分:0)
我遇到了同样的问题。我通过在适配器类中重写这两个方法来解决它。
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
希望这有帮助!
答案 2 :(得分:0)
您正在选择一个并且View holder会保持选中状态。您可以将一个bolean变量添加到ObjectIncome对象中,并保持项目的选择状态。
public class AdapterTrashIncome extends RecyclerView.Adapter<AdapterTrashIncome.ViewHolder> {
private ArrayList<ObjectIncome> myItems = new ArrayList<>();
public AdapterTrashIncome(ArrayList<ObjectIncome> getItems, Context context){
try {
mContext = context;
myItems = getItems;
}catch (Exception e){
Log.e(FILE_NAME, "51: " + e.toString());
e.printStackTrace();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvContent;
public CheckBox cbSelect;
public ViewHolder(View v) {
super(v);
tvContent = (TextView) v.findViewById(R.id.tvContent);
cbSelect = (CheckBox) v.findViewById(R.id.cbSelect);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ObjectIncome objIncome = myItems.get(position);
String content = "<b>lalalla</b>";
holder.tvContent.setText(Html.fromHtml(content));
//in some cases, it will prevent unwanted situations
holder.cbSelect.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.cbSelect.setChecked(objIncome.isSelected());
holder.cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set your object's last status
objIncome.setSelected(isChecked);
}
});
}
}
在您的活动的onCreate方法中。
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recylerViewLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);
recyclerView.setAdapter(recyclerViewAdapter);
您的Activiy xml
就像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.recyclerview_android_examplescom.MainActivity"
android:id="@+id/relativelayout1">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>