Dear All,
I have done the single selection with Recyclerview. But it does
not work like ListView Single Choice. I'm using
notifyItemRangeChanged(0,itemList.size())
method to notify the selection. It shows time delay in the UI like a blink.
How to resolve the single choice mode in recyclerview. Here is my code for
my recyclerview adapter.
public class WorkSummaryRecyclerAdapter extends
RecyclerView.Adapter<WorkSummaryRecyclerAdapter.WorkSummaryViewHolder> {
private int mSelectedItem = -1;
private AdapterView.OnItemClickListener mOnItemClickListener;
private List<WorkSummaryItem> itemList;
private Context mContext;
public WorkSummaryRecyclerAdapter(Context context, List<WorkSummaryItem>
itemList) {
this.mContext = context;
this.itemList = itemList;
}
@Override
public WorkSummaryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(mContext).inflate(R.layout.item_work_summary, parent,
false);
return new WorkSummaryViewHolder(view, this);
}
@Override
public void onBindViewHolder(WorkSummaryViewHolder holder, int position) {
WorkSummaryItem item = itemList.get(position);
try {
holder.setDataToView(item);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return itemList.size();
}
public void setOnItemClickListener(AdapterView.OnItemClickListener
onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
public void onItemHolderClick(WorkSummaryViewHolder holder) {
if (mOnItemClickListener != null)
mOnItemClickListener.onItemClick(null, holder.itemView,
holder.getAdapterPosition(), holder.getItemId());
}
public class WorkSummaryViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
@Bind(R.id.work_title)
TextView mTitle;
@Bind(R.id.work_title_expanded)
TextView mTitleExpanded;
@Bind(R.id.work_description)
TextView mDescription;
@Bind(R.id.work_description_expanded)
TextView mDescriptionExpanded;
@Bind(R.id.work_count)
TextView mCount;
@Bind(R.id.work_count_expanded)
TextView mCountExpanded;
@Bind(R.id.work_checkbox)
CheckBox mCheckBox;
@Bind(R.id.work_checkbox_expanded)
CheckBox mCheckBoxExpanded;
@Bind(R.id.card_view)
CardView mLayoutUnChecked;
@Bind(R.id.layout_checked)
RelativeLayout mLayoutChecked;
@Bind(R.id.work_last_worked)
TextView mLastWorked;
@Bind(R.id.work_user)
TextView mUserName;
private WorkSummaryRecyclerAdapter mAdapter;
public WorkSummaryViewHolder(View itemView, WorkSummaryRecyclerAdapter
mAdapter) {
super(itemView);
ButterKnife.bind(this, itemView);
this.mAdapter = mAdapter;
itemView.setOnClickListener(this);
}
public void setDataToView(WorkSummaryItem item) {
mTitle.setText(item.getTitle());
mDescription.setText(item.getDescription());
mCount.setText("" + item.getCount());
mTitleExpanded.setText(item.getTitle());
mDescriptionExpanded.setText(item.getDescription());
mCountExpanded.setText("" + item.getCount());
if (getAdapterPosition() == mSelectedItem) {
mLayoutChecked.setVisibility(View.VISIBLE);
mLayoutUnChecked.setVisibility(View.GONE);
} else {
mLayoutChecked.setVisibility(View.GONE);
mLayoutUnChecked.setVisibility(View.VISIBLE);
}
}
@Override
public void onClick(View v) {
mSelectedItem = getAdapterPosition();
notifyItemRangeChanged(0, itemList.size());
mAdapter.onItemHolderClick(this);
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/9c80b025-e131-4bc9-a788-831af1161961%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.