Enable Button after list Item of RecyclerView was selected - java

I have an activity with a RecyclerView and a button that is disabled at first. The elements of the RV are selectable, and I want the button to be enabled only after an element was selected. I tried to set addOnItemTouchListener on the recyclerview, the problem is that it is also called when scrolling through the list.
Here is my Adapter:
public class CLusterListAdapter extends RecyclerView.Adapter<CLusterListAdapter.ClusterListViewHolder> {
private ArrayList<ClusterItem> mListOfClusters;
private OnItemClickListener mListener;
//index
int row_index = -1; //Default no row chosen
public interface OnItemClickListener{
//delete
void onDeleteCluster(int position);
//edit
void onEditCluster(int position);
//select
void onClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){mListener = listener;}
public static class ClusterListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mClusterName;
public TextView mClusterMembers;
public ImageView mDeleteCluster;
public ImageView mEditCluster;
//This
private OnItemClickListener itemClickListener;
public void setItemClickListener(OnItemClickListener listener){
this.itemClickListener = listener;
}
public ClusterListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mClusterName = itemView.findViewById(R.id.tv_clusters_name_cluster_list);
mClusterMembers = itemView.findViewById(R.id.tv_clusters_members_cluster_list);
mDeleteCluster = itemView.findViewById(R.id.iv_delete_cluster);
mEditCluster = itemView.findViewById(R.id.iv_edit_cluster);
mDeleteCluster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onDeleteCluster(position);
}
}
}
});
mEditCluster.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onEditCluster(position);
}
}
}
});
//used for highlighting item
itemView.setOnClickListener(this);
}
//This
#Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition());
}
}
public CLusterListAdapter(ArrayList<ClusterItem> listOfClusters ){ mListOfClusters = listOfClusters;}
#NonNull
#Override
public ClusterListViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chose_your_cluster_list_item, viewGroup, false);
ClusterListViewHolder clusterListViewHolder = new ClusterListViewHolder(view, mListener);
return clusterListViewHolder;
}
#Override
public void onBindViewHolder(#NonNull ClusterListViewHolder clusterListViewHolder, int i) {
ClusterItem currentCluster = mListOfClusters.get(i);
clusterListViewHolder.mClusterName.setText(currentCluster.getClustersName());
clusterListViewHolder.mClusterMembers.setText(currentCluster.getClustersMembers());
clusterListViewHolder.mClusterMembers.setSelected(true);
clusterListViewHolder.setItemClickListener(new OnItemClickListener() {
#Override
public void onDeleteCluster(int position) {
//nothing here
}
#Override
public void onEditCluster(int position) {
//nothing here
}
#Override
public void onClick(View view, int position) {
//Select a certain item
row_index = position; //Set row index to selected position
SpTAGs.currentItem = mListOfClusters.get(position); //Set current item is item selection
notifyDataSetChanged(); //Made effect on RecyclerView's Adapter
SpTAGs.selectedClusterPosition = position;
}
});
//Set highlight color
if(row_index == i) {
clusterListViewHolder.itemView.setBackgroundColor(Color.parseColor("#E64A19"));
}else {
clusterListViewHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
#Override
public int getItemCount() {
return mListOfClusters.size();
}
}
And here is how I applied the addOnItemTouch
clusterRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView recyclerView, #NonNull MotionEvent motionEvent) {
selectButton.setEnabled(true);
Log.v("MyTAG", "Touch");
return false;
}
#Override
public void onTouchEvent(#NonNull RecyclerView recyclerView, #NonNull MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {
}
});
Any idea how to obtain this? Thank you!

Have a callback from the Adapter to the activity. Enable your button there.
public class CLusterListAdapter extends RecyclerView.Adapter<CLusterListAdapter.ClusterListViewHolder> {
...
interface Listener {
void onItemClick(int position);
}
}
Invoke this on item click
clusterListViewHolder.setItemClickListener(new OnItemClickListener() {
#Override
public void onDeleteCluster(int position) {
//nothing here
}
#Override
public void onEditCluster(int position) {
//nothing here
}
#Override
public void onClick(View view, int position) {
//Select a certain item
row_index = position; //Set row index to selected position
SpTAGs.currentItem = mListOfClusters.get(position); //Set current item is item selection
notifyDataSetChanged(); //Made effect on RecyclerView's Adapter
listener.onItemClick(position); // notify the listener (activity) of the click
SpTAGs.selectedClusterPosition = position;
}
});
In activity:
clusterRecyclerView.addOnItemTouchListener( ... );
clusterRecyclerView.setListener((int position) -> {
// logic to enable button goes here
});

you don't need to use the addOnItemTouchListener here.
just create a methode inside the activity like this
public void VisibleButton(){
yourButton.setVisibility(View.VISIBLE);
/**....*/
}
and just call the methode
VisibleButton()
from your adapter,from where you want your button to be visible, just like this
((YourActivityName)your_ACtivity_Context).VisibleButton();

Related

How to highlight a selected RecyclerView item and use its values in Activity

I have a list of words with translations in RecyclerView. I want the selected word to be highlighted, but at the same time I could use these values in Activity. I can highlight selected values or use the values of a selected item, but the combination of the both fails.
Please, maybe You see where I am wrong and can help solve my problem.
p.s. Please don’t judge me for the not enough clean code and my English, I’m learning. :)
MyListActivity.java
public class MyListActivity extends AppCompatActivity {
public ArrayList<MyWord> myWordList = new ArrayList<>();
SelectData sd = new SelectData();
int wordId;
String word;
String translation;
public RecyclerView mRecycleView;
public MyAdapter mAdapter;
public RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list);
sd.selectMyWords(myWordList);
buildRecycleView();
Button btnInsert = findViewById(R.id.mywords_button_insert);
Button btnEdit = findViewById(R.id.mywords_button_edit);
Button btnDelete = findViewById(R.id.mywords_button_delete);
Button btnMain = findViewById(R.id.mywords_button_backttomain);
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
// **This is my selected item values**
wordId = myWordList.get(position).getWordId();
word = myWordList.get(position).getWord();
translation = myWordList.get(position).getTranslation();
Toast.makeText(MyListActivity.this, "Selected -> " + word, Toast.LENGTH_SHORT).show();
}
});
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!(word == null)) {
Intent intentInsert = new Intent(MyListActivity.this, InsertActivity.class);
intentInsert.putExtra("W", word);
intentInsert.putExtra("TR", translation);
intentInsert.putExtra("ACTION", "ED");
startActivity(intentInsert);
} else {
Toast.makeText(MyListActivity.this, "Select word", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void buildRecycleView(){
mRecycleView = findViewById(R.id.mywords_RecycleView);
mRecycleView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MyAdapter(myWordList);
mRecycleView.setLayoutManager(mLayoutManager);
mRecycleView.setAdapter(mAdapter);
if (myWordList.isEmpty()){
Toast.makeText(this, "word list is empty", Toast.LENGTH_SHORT).show();
}
}
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<MyWord> mMyWordList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewEN;
public TextView mTextViewLT;
public ImageView imageViewLearned;
public MyViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
imageViewLearned = itemView.findViewById(R.id.iV_learned);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
public MyAdapter(ArrayList<MyWord> myWordList){
mMyWordList = myWordList;
}
int selectedItem = -1;
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_view_layout, parent, false);
MyViewHolder mvh = new MyViewHolder(v, mListener);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
MyWord currentMyWord = mMyWordList.get(position);
holder.mTextViewEN.setText(currentMyWord.getWord());
holder.mTextViewLT.setText(currentMyWord.getTranslation());
// TODO - START (select item highlight) - if I activate highlighting, the assignment of values no longer works in MyListActivity.
/* holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedItem = position;
notifyDataSetChanged();
}
});
if (selectedItem == position) {
holder.itemView.setBackgroundColor(Color.parseColor("#C0C0C0"));
} else {
holder.itemView.setBackgroundColor(Color.parseColor("#F8F8F8"));
} */
//TODO -END
}
#Override
public int getItemCount() {
return mMyWordList.size();
}
}
Looks like your commented code would overwrite your click listener with one that only hightlights. Try combining them:
public MyViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
imageViewLearned = itemView.findViewById(R.id.iV_learned);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
selectedItem = position;
notifyDataSetChanged();
listener.onItemClick(position);
}
}
}
});
}

How To Refresh Item Positions In Recylcerview for filtering?

I want to filter recyclerView items, my code is working well in terms of displaying filtered items but when I am clicking on those items it's clicking on previous items though those items are not visible. I thought the problem is on item clickListener implementation but I have tried two recylcerview onitemClickListener implementation but both of them aren't working.
Can You Please Help? Thanks In Advance.
Here is the filter code in Recyclerview Adapter.
public void updateList(List<ProductInfo> list){
mProductList = list;
notifyDataSetChanged();
}
Here is the Clicklistener on Recyclerview in Activity.
mProductAdapter = new AdapterTest(mProductInfos, new AdapterTest.ClickListener(){
#Override
public void onPositionClicked(View v, int position) {
ProductInfo productInfo = mProductInfos.get(position);
Intent intent = new Intent(VegetableActivity.this, DescriptionActvity.class);
intent.putExtra(MainActivity.EXTRA_OBJECT, productInfo);
startActivity(intent);
}
#Override
public void onLongClicked(View v, int position) {
}
});
And This is the Adapter Class.
public class AdapterTest extends RecyclerView.Adapter<AdapterTest.MyviewHolder> {
private List<ProductInfo> mProductList;
private ClickListener listener;
public class MyviewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
private TextView mProductName, mProductPrice, mProductRating;
private ImageView mProductImage;
private WeakReference<ClickListener> listenerRef;
public MyviewHolder(View itemView, ClickListener listener) {
super(itemView);
listenerRef = new WeakReference<>(listener);
mProductName = itemView.findViewById(R.id.product_name);
mProductPrice = itemView.findViewById(R.id.product_price);
//mProductRating = itemView.findViewById(R.id.product_rating);
mProductImage = itemView.findViewById(R.id.product_image);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// if(view.getId() == decreaseView.getId()){
// Toast.makeText(view.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
// }else {
// Toast.makeText(view.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
// }
listenerRef.get().onPositionClicked(view, getAdapterPosition());
}
#Override
public boolean onLongClick(View view) {
return false;
}
}
public AdapterTest(List<ProductInfo> productList, ClickListener listener){
mProductList = productList;
this.listener = listener;
}
#Override
public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vegetable_layout, parent, false);
return new MyviewHolder(itemView, listener);
}
#Override
public void onBindViewHolder(MyviewHolder holder, int position) {
ProductInfo productInfo = mProductList.get(position);
holder.mProductName.setText(productInfo.getProductName());
holder.mProductPrice.setText(productInfo.getProductPrice());
//holder.mProductRating.setText(productInfo.getProductRating());
Picasso.get()
.load(productInfo.getProductImage())
.into(holder.mProductImage);
}
#Override
public int getItemCount() {
return mProductList.size();
}
public interface ClickListener {
void onPositionClicked(View v, int position);
void onLongClicked(View v, int position);
}
public void updateList(List<ProductInfo> list){
mProductList = list;
notifyDataSetChanged();
}
}
you can move below code segement to onCLick evenet of the recycle item
Intent intent = new Intent(VegetableActivity.this, DescriptionActvity.class);
intent.putExtra(MainActivity.EXTRA_OBJECT, productInfo);
startActivity(intent);
you donot need the onPositionClicked listener for that and you can remove ProductInfo productInfo = mProductInfos.get(position); as well. I have no clear idea about what u doing. but let say u want to nevigate to another page when u click on recycle item. u can add it like below
#Override
public void onBindViewHolder(MyviewHolder holder, int position) {
ProductInfo productInfo = mProductList.get(position);
holder.mProductName.setText(productInfo.getProductName());
holder.mProductPrice.setText(productInfo.getProductPrice());
//holder.mProductRating.setText(productInfo.getProductRating());
Picasso.get()
.load(productInfo.getProductImage())
.into(holder.mProductImage);
//new code - start here
holder.anyuielementInItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
// Do things you want
}
});
//
}

In Recycle view how to Highlight always one adapter item with the click and without click

Here I clicked on the item to change item background and color. I've stored the clicked item value in the database and change the layout color and text color and recreating the adapter and showing the list again while refreshing.
But layout colors not changed when I get its position. Please show the right path to handle the set of background item color always.
public class LoadVehicleTypeAdapter extends RecyclerView.Adapter<LoadVehicleTypeAdapter.CarTypesHolder> {
private List<TaxiTypeResponse.Message> CarTypesModelsList;
private Context mContext;
VehicleTypeView vehicleTypeView;
int I = -1;
int idd = 0;
int II = 0;
Activity activity;
GoogleMap map;
List<VehicleClick> list;
private SparseBooleanArray selectedItems;
public class CarTypesHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public CustomTextView mCarType;
public CircleImageView mCarTypeImage;
LinearLayout llRoot;
CardView cardView;
setOnitemclick listener;
public void setOnItemClickListner(setOnitemclick listener) {
this.listener = listener;
}
public CarTypesHolder(View view) {
super(view);
mCarType = (CustomTextView) view.findViewById(R.id.frag_cartypes_inflated_name);
mCarTypeImage = (CircleImageView) view.findViewById(R.id.frag_cartype_inflated_frameImage);
llRoot = (LinearLayout) view.findViewById(R.id.root1);
selectedItems = new SparseBooleanArray();
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.ImageClick(v, getAdapterPosition());
}
}
public LoadVehicleTypeAdapter(Context context, List<TaxiTypeResponse.Message> CarTypesModelsList, VehicleTypeView vehicleTypeView, Activity activity, GoogleMap map, List<VehicleClick> lists) {
this.CarTypesModelsList = CarTypesModelsList;
mContext = context;
this.vehicleTypeView = vehicleTypeView;
this.activity = activity;
this.map = map;
this.list = lists;
}
#Override
public CarTypesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.frag_cartype_inflated_view, parent, false);
return new CarTypesHolder(itemView);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(final CarTypesHolder holder, int position) {
if (list.size() != 0) {
II = Integer.parseInt(list.get(0).RideId);
//setSelection(Integer.parseInt(list.get(0).RideId));
}
if (II == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#FFFFFF"));
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
SharedPreferences sharedPreferences = activity.getSharedPreferences("mSelected", Context.MODE_PRIVATE);
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext, PreferenceHandler.SECONDRY_COLOR, "#006fb6"));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position1) {
I = position1;
notifyDataSetChanged();
try {
if (list.size() != 0) {
VehicleTypeFragment.myAppRoomDataBase.userDao().delete();
list.clear();
}
VehicleClick vehicleClick = new VehicleClick();
vehicleClick.setRideId(String.valueOf(position1));
VehicleTypeFragment.myAppRoomDataBase.userDao().insert(vehicleClick);
list.add(vehicleClick);
} catch (Exception e) {
}
}
});
if (I == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}
#Override
public long getItemId(int position) {
return CarTypesModelsList.get(position).getID();
}
#Override
public int getItemCount() {
return CarTypesModelsList.size();
}
public void setSelection(int position) {
II = position;
//notifyDataSetChanged();
}
public interface setOnitemclick {
void ImageClick(View view, int position);
}
#Override
public int getItemViewType(int position) {
return position;
}
}
I am not sure what did you mean by refreshing your list. I am guessing that you are recreating the adapter and showing the list again while you are refreshing. Hence the value of I is initialized with -1 each time you are creating the adapter.
You need to do the initialization as follows. Please consider the following is a pseudo code and you need to implement this of your own.
// While declaring your I
// int I = -1;
int I = getTheStoredValueFromDatabase(); // If there is no entry in database, getTheStoredValueFromDatabase function will return -1
I hope you get the idea. You might consider doing the same for other stored values.
for keep track record you need to add Boolean variable in TaxiTypeResponse.Message boolean isClick=false; and toggle this in
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position) {
CarTypesModelsList.get(position).isClick=!CarTypesModelsList.get(position).isClick;
notifyDataSetChanged();
}
}
and modify below code as follow
if (CarTypesModelsList.get(position).isClick) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
}
else{
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Note: onBindViewHolder() is not a place to implement the click
listeners, but I am just providing you the logic for how to implement
single selection in recyclerView.
Now lets jump to the solution,
simply follow the below tutorial and change the variable, fields, and background according to your need, you have to implement the below method in onBindViewHolder() method of RecyclerView
First, initialize the lastClickedPosition and isclicked
int lastPositionClicked = -1;
boolean isClicked = false;
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.YOUR_VIEW.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// store the position which you have just clicked and you will change the background of this clicked view
lastPositionClicked = position;
isClicked = true;
// need to refresh the adapter
SlabAdapter.this.notifyDataSetChanged();
}
});
// only change the background of last clicked item
if (isClicked && position == lastPositionClicked) {
// change clicked view background color
} else {
// otherwise set the default color for all positions
}
}
let me know if this works.
on BindViewHolder method you'll use this code and set I=0 on globally
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(final CarTypesHolder holder, int position) {
SharedPreferences sharedPreferences = activity.getSharedPreferences("mSelected", Context.MODE_PRIVATE);
TaxiTypeResponse.Message carTypesModel = CarTypesModelsList.get(position);
holder.mCarType.setText(carTypesModel.getName());
holder.mCarTypeImage.setBackgroundResource(R.drawable.wait);
int color = Color.parseColor(PreferenceHandler.readString(mContext, PreferenceHandler.SECONDRY_COLOR, "#006fb6"));
holder.mCarType.setTextColor(color);
holder.setOnItemClickListner(new setOnitemclick() {
#Override
public void ImageClick(View v, int position1) {
I = position1;
notifyDataSetChanged();
try {
if (list.size() != 0) {
VehicleTypeFragment.myAppRoomDataBase.userDao().delete();
list.clear();
}
VehicleClick vehicleClick = new VehicleClick();
vehicleClick.setRideId(String.valueOf(position1));
VehicleTypeFragment.myAppRoomDataBase.userDao().insert(vehicleClick);
list.add(vehicleClick);
} catch (Exception e) {
}
}
});
if (I == position) {
holder.llRoot.setBackgroundColor(Color.parseColor("#999999"));
holder.mCarType.setTextColor(Color.parseColor("#ffffff"));
Animation bounce = AnimationUtils.loadAnimation(mContext, R.anim.bounce);
holder.llRoot.startAnimation(bounce);
} else {
holder.llRoot.setBackgroundColor(Color.parseColor("#f3f3f3"));
holder.mCarType.setTextColor(Color.parseColor("#2196F3"));
}
Picasso.with(mContext).load(carTypesModel.getImagePath()).into(holder.mCarTypeImage);
}

Android Firebase Recyclerview - How to deselect previous selected item?

I have a simple question. I have a recyclerview with list items, what I can do is highlight the items you click on. My problem is that I don't know how to deselect the previous item the user has clicked on so only 1 item can be selected.
private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.categoryaddquestions_layout,
CategoryViewHolder.class,
categories
) {
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
viewHolder.category_name.setText(model.getName());
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
view.setSelected(false);
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
Here is my view holder I use
public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView category_name;
public ImageView category_image;
public LinearLayout category_layout;
private ItemClickListener itemClickListener;
public CategoryViewHolder(View itemView) {
super(itemView);
category_layout = itemView.findViewById(R.id.categoryaddquestion_layout);
category_image = itemView.findViewById(R.id.category_image);
category_name = itemView.findViewById(R.id.category_name);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
Create a variable in your adapter called SelectedPosition. On click, update that variable to the adapterPosition and reload the recyclerView.
Make a check in populateViewHolder saying:
if selectedPosition == position {
//highlighted
}
else {
//set back to normal
}
here is my own solution.
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final
Category model, int position) {
viewHolder.category_name.setText(model.getName());
if (position == Common.index) {
viewHolder.category_layout.setSelected(true);
} else {
viewHolder.category_layout.setSelected(false);
}
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Common.index = position;
adapter.notifyDataSetChanged();
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
I just used a Common java class and problem solved with a public static int index

Get data on click inside recycle view

I have build a list of data with a recyclerview. Everything works as expected. But I want to acces some data in my adapter when I click on a cell. The click works. But I don't know how to acces my events list.
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ViewHolder> {
private final LayoutInflater layoutInflater;
private final Context context;
private ArrayList<Event> events;
public ExampleAdapter(Context context, ArrayList<Event> events) {
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.events = events;
}
#Override
public ExampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(layoutInflater.inflate(R.layout.activity_row, parent, false));
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
//holder.textView.setText(titles[position]);
holder.title.setText(events.get(position).getTitle());
holder.time.setText(events.get(position).getTime());
holder.places.setText(events.get(position).getPlacesLeft());
}
public void add(ArrayList<Event> events){
this.events = events;
this.notifyDataSetChanged();
}
#Override
public int getItemCount() {
return events == null ? 0 : events.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView time;
TextView places;
ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.activity_title);
time = (TextView) view.findViewById(R.id.activity_time);
places = (TextView) view.findViewById(R.id.activity_places);
//textView = (TextView) view.findViewById(R.id.text_view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ViewHolder", "onClick--> position = " + getPosition());
}
});
}
}
}
I have declared the onclick listener in the ViewHolder class. The click listener works as expected. But I want to acces the data in my events array. The only problem is that I can't acces it.
your onclick should be inside your onBindViewHolder
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
//holder.textView.setText(titles[position]);
holder.title.setText(events.getTitle());
holder.time.setText(events.getTime());
holder.places.setText(events.getPlacesLeft());
holder.yourview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//access from here
Toast.makeText(yourActivity.this,holder.yourview.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
If you just want to handle the click on the item you can do like this :
In your adapter write a function to get an event at a position :
public Event getEventAtPosition(int position) {
if (events != null && events.size() > position)
return events.get(position);
else return null;
}
Then implement a RecyclerTouchListener :
public interface ClickListener{
void onClick(View view,int position);
void onLongClick(View view,int position);
}
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private ClickListener clicklistener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){
this.clicklistener=clicklistener;
gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child=recycleView.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null){
clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
clicklistener.onClick(child,rv.getChildAdapterPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Finally, you can use it this way :
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new ClickListener() {
#Override
public void onClick(View view, final int position) {
Event event= ((EventListAdapter)recyclerView.getAdapter()).getEventAtPosition(position);
}
#Override
public void onLongClick(View view, int position) {
}
}));
This way, you have only 1 ClickListener for all your recycler view, this is more optimal than declaring a listener for each item.
For more informations about RecyclerView and onClick, check this topic : RecyclerView onClick

Categories

Resources