getListData() {
mModelList = new ArrayList();
for (int i = 1; i

Model.java

Social class model {

private String text;
private boolean isSelected = false ;

public template (string) {
this.text = text;
}

public String getText() {
returns the text;
}

public void setSelected(boolean selected) {
isSelected = selected;
}

public boolean isSelected() {
return isSelected;
}
}

RecyclerViewAdapter.java

public RecyclerViewAdapter class expands RecyclerView.adapt {

mModelList ;

public RecyclerViewAdapter(ModelList List) {
mModelList = ModelList;
}

@Check
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())).inflate(R.layout.item_row, parent, false);
returns new MyViewHolder(view);
}.

General
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Model = mModelList.get(position);
holder.textView.setText(model.getText());
holder.setBackgroundColor(model.isSelected()? Color.CYAN: Color.WHITE);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setSelected(!model.isSelected());
holder.view.setBackgroundColor(model.isSelected()) ? Color.CYAN: Color.WHITE);
}
});
}

@Override
public int getItemCount() {
return mModelList == null ? 0: mModelList.size();
}

The MyViewHolder public class expands RecyclerView.ViewHolder {

closed display;
closed text display TextView ;

private MyViewHolder(View itemView) {
super(itemView);
view = itemView;
textView = (TextView) itemView.findViewById(R.id.text_view);
}
}
}

How does it work? The
onBindViewHolder() method links the ArrayList data to the display objects. By linking the data to the time display only one object is retrieved in the ArrayList – Model = mModelList.get(position); with the current position. Now we need to check if this specific object is selected or not. Here’s how,

model.isSelected()

who either returns the truth or the lie. If this object is already selected we need to change the background color of the selected row_element. To do this, here’s the code

holder.view.setBackground color(model.isSelected() ? Color.CYAN: Color.WHITE) ;

Once selected, the background color changes to cyan or white.

The setOnClickListener() method should be used to make the selection. (Here I only use TextView. So I run the Click event on the TextView). Here, holder.view means a whole_row element. Click the button to change the boolean values to true or false.

holder.textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {model.setSelected(!model.isSelected());holder.view.setBackgroundColor(model.isSelected() {@Overridepublic void onClick(View)) {model.setSelected(!model.isSelected());holder.view.setBackgroundColor(model.isSelected()? Color.CYAN: Color.WHITE);}) ;

In your activity or fragmentation of RecyclerView you can find the selected objects/items as follows.

Text string = ;
for (Model : mModelList) {
if (model.isSelected()) {
text += model.getText() ;
}
}
Log.d(TAG,Output : + text) ;

Here’s the result.

Publisher 1 : Limit the selection of a single item.

private int lastSelectedPosition = -1; // indicate this variable

// Your code

General
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Model = mModelList.get(position);
holder.textView.setText(model.getText());
holder.setBackgroundColor(model.isSelected()? Color.CYAN: Color.WHITE);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public null and void onClick(View view) { View.)

// check that you have chosen

if(lastSelectedPosition > 0) {
mModelList.get(lastSelectedPosition).setSelected(false);
}

model.setSelected(! model.isSelected());
holder.view.setBackgroundColor(model.isSelected()? Color.CYAN: Color.WHITE) ;

// Save the last selected position of the element

lastSelectedPosition = holder.getAdapterPosition();
}
} );
}

I hope this helps.

Solution 2:

Making the above mentioned custom multi-selection implementations works well, but can lead to performance problems with large datasets. I recommend that you read the section Creating a looped view, which contains documentation on how to select list items on Google. I have listed the
Android documentation link for creating a recycling overview and activating an item.

Solution 3:

The RecyclerColorAdapter public class expands the RecyclerView.Adapter {

private finished activity;
private finished activity ArrayArrayList items ;

public RecycleColorAdapter(Activity, ArrayList itemArrayList) {
super();
this.activity = activity;
this.itemArrayList = itemArrayList;
}

@Check
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_color_recycleview, viewGroup, false);
returns new ViewHolder(v);
}.

@Check public void
onBindViewHolder(final ViewHolder, final int i) {
holder.setIRecyclable(true) ;

colorItem final colorItem = itemArrayList.get(i) ;

holder.button_color.setText(colorItem.getColorName()) ;

holder.button_color.setBackgroundColor(colorItem.isSelected() ? color.CYAN : color.WHITE) ;

holder.button_color.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {colorItem.setSelected(!colorItem.isSelected());holder.button_color.setBackgroundColor(colorItem.isSelected()) {@Overridepublic void onClick(View v) {colorItem.setSelected(! colorItem.isSelected())? Color.CYAN : Color.WHITE);if (colorItem.isSelected())){arrayListColor.add(diamond_color[]=+colorItem.getValue()+&);Log.e(arrayListColor,———+arrayListColor);}else {arrayListColor.remove(diamond_color[]=+colorItem.getValue()+&);Log.e(arrayListColor,———+arrayListColor);}.

}
});

}

@Override
public int getItemCount() {

return itemArrayList.size();
}

The ViewHolder public class expands RecyclerView.ViewHolder implements View.OnClickListener {

Own button_color ;

public ViewHolder(View itemView) {
super(itemView) ;
itemView.setOnClickListener(this) ;
button_color = (Button) itemView.findViewById(R.id.button_color) ;

}

@General invalid
opClisk(View v) {

}

}

}

Solution 4:

Without creating a model class, we can select an item multiple times in the Recycler view. Use this code in the Recycler view adapter.

holder.favplayIcon.setOnClickListener(View.OnClickListener {
if (!row_index.contains(position)))) {
row_index.add(position)
holder.favplayIcon.setImageDrawable(
ResourcesCompatible.getDrawable(resources, R.drawable.ic_starfilled, null)))

} otherwise {
row_index.removeAt(row_index.indexOf(position))
holder.favplayIcon.setImageDrawable(
ResourcesCompatible.getDrawable(resources,R.drawable.ic_starborder, null)
) } } } }
})

///// produces the following code of the onclicklistener method of element
if (! row_index.contains(position)) {

holder.favplayIcon.setImageDrawable(ResourcesCompatible.getDrawable(resources,R.drawable.ic_starborder,null))}other {holder.favplayIcon.setImageDrawable(ResourcesCompat.getDrawable(resources,R.drawable.ic_starfill,null)))

Solution No 5:

A quick and easy way to do it:

holder.parentLayout.setOnClickListener {

if (holder.categoryIcon.isSelected) {
selectedPos = position
}

if (selectedPos == position) {
notifyItemChanged(selectedPos)
selectedPos = RecyclerView.NO_POSITION
} else {
selectedPos = position
notifyItemChanged(selectedPos)
}
}.

if (selectedPos == position) {
owner.categoryIcon.setBackgroundColor(ContextCompat.getColor(context, R.color.orange))
owner.categoryIcon.isSelected = true
} otherwise {
owner.categoryIcon.setBackgroundColor(ContextCompat.getColor(context, R.color.white))
owner.categoryIcon.isSelected = false
}

Good luck!

Related Tags:

multi and single selection in recyclerview github,recyclerview multi select and delete github,select all items in recyclerview android,multiple selection in recyclerview android kotlin,select multiple items in recyclerview android github,android multi select recyclerview library,android recyclerview with checkbox multiple selection,recyclerview-selection,recyclerview single item selection android,how to delete multiple items from recyclerview in android,how to highlight selected item in recyclerview android example

Categories: World Tech