我使用RecyclerView它支持拖放面臨setOnCheckedChangeListener位置值的問題。我的列表項有一行有兩個TextView,CheckBox,ImageView用於拖動行。在RecyclerView的Android
arrTaxStatus的價值觀是正確的,當我拖行。 arrTaxStatus的
價值觀是正確的,當我啓用/禁用的複選框。
但我面臨的問題時,我啓用/禁用一些複選框,拖幾行,然後再啓用/禁用一些複選框。該位置在setOnCheckedChangeListener中受到干擾。即使我點擊第一行,我得到一個不同的「位置」
請告訴我怎樣才能獲得點擊複選框的正確位置。下面
代碼:
public class RecyclerListFragment extends AppCompatActivity implements OnStartDragListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_tax_group);
arrTaxStatus = new ArrayList<Boolean>();
HashMap<String, Object> temp;
taxAccounts = new ArrayList<HashMap<String, Object>>();
Cursor cursor = dh.getDetailsFromTaxGivenTaxCategory();
if (cursor.moveToFirst()) {
do {
temp = new HashMap<String, Object>();
temp.put("taxName", cursor.getString(cursor.getColumnIndex("scheme_name")));
temp.put("taxPercent", numberFormat.format(cursor.getDouble(cursor.getColumnIndex("percentage"))));
taxAccounts.add(temp);
arrTaxStatus.add(false);
} while (cursor.moveToNext());
}
cursor.close();
RecyclerListAdapter adapter = new RecyclerListAdapter(this, taxAccounts);
RecyclerView recyclerView = (RecyclerView) findViewById(android.R.id.list);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(recyclerView);
}
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
mItemTouchHelper.startDrag(viewHolder);
}
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
implements ItemTouchHelperAdapter {
private final OnStartDragListener mDragStartListener;
public RecyclerListAdapter(OnStartDragListener dragStartListener, ArrayList<HashMap<String, Object>> taxAccounts) {
mDragStartListener = dragStartListener;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.create_tax_group_row, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
holder.taxName.setText(taxAccounts.get(position).get("taxName").toString());
holder.taxPercent.setText(taxAccounts.get(position).get("taxPercent").toString());
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
holder.taxStatus.setOnCheckedChangeListener(null);
holder.taxStatus.setChecked(arrTaxStatus.get(position));
holder.taxStatus.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("arrTaxStatus pos|checked", position + "|" + isChecked);
arrTaxStatus.set(position, isChecked);
Log.i("arrTaxStatus onCheckedChanged", arrTaxStatus.toString());
}
});
}
@Override
public void onItemDismiss(int position) {
taxAccounts.remove(position);
arrTaxStatus.remove(position);
notifyItemRemoved(position);
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(taxAccounts, fromPosition, toPosition);
Collections.swap(arrTaxStatus, fromPosition, toPosition);
Log.i("arrTaxStatus onItemMove", arrTaxStatus.toString());
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public int getItemCount() {
return taxAccounts.size();
}
/**
* Simple example of a view holder that implements {@link ItemTouchHelperViewHolder} and has a
* "handle" view that initiates a drag event when touched.
*/
public class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {
public final TextView taxName, taxPercent;
public final CheckBox taxStatus;
public final ImageView handleView;
public ItemViewHolder(View itemView) {
super(itemView);
taxName = (TextView) itemView.findViewById(R.id.childname);
taxPercent = (TextView) itemView.findViewById(R.id.balance);
taxStatus = (CheckBox) itemView.findViewById(R.id.check_status);
handleView = (ImageView) itemView.findViewById(R.id.handle);
}
@Override
public void onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY);
}
@Override
public void onItemClear() {
itemView.setBackgroundColor(0);
}
}
}
}