I want a certain layout to become visible on all my recyclerview items when a button on the actionbar is clicked.
Google didn't help, and I have no idea how to go about this.
An example of this would be when a user wants to delete a lot of SMS messages from the inbox and press-and-hold suddenly enables checkboxes on every item in the recyclerview.
EDIT:
public class ViewCartAdapter extends RecyclerView.Adapter<ViewCartAdapter.DataViewHolder>
{
private List<ViewCartModel.Product> dataList;
private Context con;
public ViewCartAdapter(Context context, List<ViewCartModel.Product> list)
{
this.dataList = list;
con = context;
}
#Override
public int getItemCount()
{
return dataList.size();
}
#Override
public void onBindViewHolder(final DataViewHolder dataViewHolder, int i)
{
dataViewHolder.setClickListener(new DataViewHolder.ClickListener()
{
#Override
public void onClick(View v, int pos, boolean isLongClick)
{
}
});
ViewCartModel.Product item = dataList.get(i);
Picasso.with(con).load(item.getThumb().replaceAll(" ", "%20")).into(dataViewHolder.itemImage);
dataViewHolder.itemName.setText(Html.fromHtml(item.getName().toUpperCase()).toString());
dataViewHolder.itemPrice.setText(item.getPrice());
dataViewHolder.categoryID = item.getKey();
}
#Override
public DataViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tile_cart, viewGroup, false);
DataViewHolder dvh = new DataViewHolder(itemView);
return dvh;
}
public static class DataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
protected ImageView itemImage;
protected TextView itemName;
protected TextView itemPrice;
protected String categoryID;
private ClickListener clickListener;
public DataViewHolder(View v)
{
super(v);
itemImage = (ImageView) v.findViewById(R.id.itemImage);
itemPrice = (TextView) v.findViewById(R.id.itemPrice);
itemName = (TextView) v.findViewById(R.id.itemName);
v.setOnClickListener(this);
}
public interface ClickListener
{
/**
* Called when the view is clicked.
*
* #param v view that is clicked
* #param position of the clicked item
* #param isLongClick true if long click, false otherwise
*/
public void onClick(View v, int position, boolean isLongClick);
}
public void setClickListener(ClickListener clickListener)
{
this.clickListener = clickListener;
}
#Override
public void onClick(View v)
{
clickListener.onClick(v, getPosition(), false); // here, get position
}
}
}
You should keep a boolean telling if the checkboxes are shown or hidden, using its value when binding to the ViewHolder. Then when your button is long pressed, toggle the value of the boolean, and call "notifyItemRangeChanged" on the adapter to rebind the ViewHolder.
Related
I have a RecyclerView and each CardView contains a TextView and an ImageView. Whenever I click an item, I want to set the image visibility to VISIBLE and to set the previous clicked item image's visibility to INVISIBLE.
This is my Adapter class :
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder>{
private Context context;
private List<Category> lista;
private LayoutInflater layoutInflater;
private IncomeCategoryActivity activity;
private static final int CATEGORY_REQUEST=6;
private static final int ITEM_EDIT=1;
private static final int ITEM_DELETE=2;
private static final int EDIT_REQUEST=7;
private int current_pos=-1;
public CategoryAdapter(List<Category> lista, Context context, IncomeCategoryActivity activity) {
this.context = context;
this.lista = lista;
this.activity=activity;
layoutInflater=LayoutInflater.from(context);
}
#Override
public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutInflater.inflate(R.layout.category_layout, parent, false);
ViewHolder viewHolder=new ViewHolder(view, activity);
return viewHolder;
}
#Override
public void onBindViewHolder(CategoryAdapter.ViewHolder holder, int position) {
holder.imageView.setImageURI(lista.get(position).getUri());
holder.textView.setText(lista.get(position).getCategory());
holder.position = position;
holder.category=lista.get(position);
if(holder.category.isChecked()==true){
holder.imageViewCheck.setVisibility(View.VISIBLE);
current_pos=position;
} else {
holder.imageViewCheck.setVisibility(View.INVISIBLE);
}
}
#Override
public int getItemCount() {
return lista.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{
public ImageView imageView;
public TextView textView;
public ImageView imageViewCheck;
public int position;
public Category category;
public IncomeCategoryActivity activity;
public ViewHolder(View itemView, IncomeCategoryActivity activity) {
super(itemView);
this.activity=activity;
imageView=itemView.findViewById(R.id.customCategoryImageView);
textView=itemView.findViewById(R.id.customCategoryTextView);
imageViewCheck=itemView.findViewById(R.id.customCheckImageView);
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
#Override
public void onClick(View v) {
String aux=textView.getText().toString();
if(aux=="CATEGORIE NOUĂ"){
Intent intent=new Intent(context, CustomIncomeActivity.class);
activity.startActivityForResult(intent, CATEGORY_REQUEST);
}
else{
imageViewCheck.setVisibility(View.VISIBLE);
int pozitie_check=getLayoutPosition();
Intent intent=new Intent(context, AddIncomeActivity.class);
intent.putExtra("categorie_venit", aux);
intent.putExtra("position_check", pozitie_check);
activity.setResult(Activity.RESULT_OK, intent);
activity.finish();
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Selectează acțiunea");
MenuItem edit=menu.add(0, ITEM_EDIT, 0, "Modifică");
MenuItem delete=menu.add(0, ITEM_DELETE, 0, "Șterge");
edit.setOnMenuItemClickListener(this);
delete.setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item) {
int position=getLayoutPosition();
if (item.getGroupId() == 0) {
if(item.getItemId()==ITEM_EDIT){
Category category=lista.get(position);
Intent intent=new Intent(activity, CustomIncomeActivity.class);
intent.putExtra("edit_icon", category.getUri());
intent.putExtra("edit_category", category.getCategory());
intent.putExtra("list_position", position);
activity.startActivityForResult(intent, EDIT_REQUEST);
}
else if(item.getItemId()==ITEM_DELETE){
lista.remove(position);
notifyDataSetChanged();
}
}
return true;
}
At this moment, whenever I click an item, there are two images VISIBLE on the RecyclerView: the clicked item's image and the previous clicked item's image. I think I need to get the previous View by its position and to manually set the visibility to INVISIBLE.
recycleView.getChildCount() and recycleView_parent.getChildAt() only gives the Adapter items which is shows only screen .
that means if your list has 200 items and the only 5 items shows on screen so we can find only 5 item with the help of recycleView
i am using one simple trick to solve the issue.
You can define Hashmap which hold your holder objects
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
ArrayList<Model> dataList;
Context context;
HashMap<Integer,ViewHolder> holderlist;
public MyAdapter(ArrayList<Model> dataList, Context context) {
this.context = context;
this.dataList = dataList;
holderlist = new HashMap<>();
}
And after that to save the holder in Hashmap
public void onBindViewHolder(final ViewHolder holder, final int position) {
if(!holderlist.containsKey(position)){
holderlist.put(position,holder);
}
Create a method in Adapter.
public MyListAdapter.ViewHolder getViewByPosition(int position) {
return holderlist.get(position);
}
Call this method from your Activity or whenever you want.
for (int i = 0; i < datalList.size(); i++) {
MyAdapter.ViewHolder holder = ((MyAdapter)recycleView.getAdapter()).getViewByPosition(i);
View view = holder.itemView;
TextView tv = view.findViewById(R.id.tv);
}
I have created the demo you can refer it and implement for single selection
recyclerView.setAdapter(new RecyclerView.Adapter() {
int selected_position = -1;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RecyclerView.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
R.layout.row_color_list,parent,false)) {
#Override
public String toString() {
return super.toString();
}
};
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
ImageView imageView = holder.itemView.findViewById(R.id.image);
if(selected_position == position){
imageView.setVisibility(View.VISIBLE);
}else {
imageView.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(selected_position != holder.getAdapterPosition()){
selected_position = holder.getAdapterPosition();
notifyDataSetChanged();
}
}
});
}
#Override
public int getItemCount() {
return 20;
}
});
You can do as following:
1) Declare a global variable:
private int selectedPos = -100; // Put any irrelevant number you want
2) Set selected position onClick() :
#Override
public void onClick(View v) {
selectedPos = getAdapterPosition();
}
3) Check selected postion and assign visibility inside onBindViewHolder():
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
if(position == selectedPos){
holder.imageViewCheck.setVisibility(View.VISIBLE);
} else {
holder.imageViewCheck.setVisibility(View.INVISIBLE);
}
}
Try this code..
add this code into adapter class for handling click event..
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(String data); //pass any data to shared it.
}
after bind method..
#Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
final String str=mStringList.get(position); // here your boject
if(holder.category.isChecked()==true){
holder.imageViewCheck.setVisibility(View.VISIBLE);
current_pos=position;
} else {
holder.imageViewCheck.setVisibility(View.INVISIBLE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.getPosition(str); // pass your data.
}
});
}
after bind adapter into recycler view it means adapter not null then called below code..
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
#Override
public void getPosition(String data) {
// hear update your value for check into if condition.
data="sdfsdf";
adpater.notifyDataSetChanged();
}
});
and also read comments and also try to make custon class and access that object value and update after click it..
this code only how to handle click event into recycler view.
I have a recyclerview from a list of Food Items. When you click on the item, I want the item to be greyed out. Now this does work, and the first item I press, does indeed turn grey. However when I press it again, some other cells turn grey. Then it turns into a big mess, with random ones greyed out?
I have set it up that when you click the item, the FoodItem instance has a boolean set. Then I refresh the data of the recyclerview, so that items with the boolean set, have a grey overlay.
Here is my adapter:
[
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.MyViewHolder> {
ClickListener clickListener;
private List<FoodItem> foodList;
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void ItemClicked(View v, int position);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView quantity;
public RelativeLayout background;
public LinearLayout eatenOverlay;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.textView_item_name);
quantity = (TextView) view.findViewById(R.id.textView_item_quantity);
background = (RelativeLayout) view.findViewById(R.id.food_item_background_relative_layout);
eatenOverlay = (LinearLayout) view.findViewById(R.id.eaten_overlay);
}
}
public FoodAdapter(List<FoodItem> foodList) {
this.foodList = foodList;
}
#Override
public FoodAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_item_list, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(FoodAdapter.MyViewHolder holder, final int position) {
FoodItem currentFoodItem = foodList.get(position);
if(currentFoodItem.isHasBeenEaten()){
holder.eatenOverlay.setVisibility(View.VISIBLE);
}
Resources res = holder.itemView.getContext().getResources();
holder.name.setText(currentFoodItem.getName());
holder.quantity.setText(currentFoodItem.getQuantity());
holder.background.setBackgroundResource(currentFoodItem.getImage());
holder.background.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.ItemClicked(v, position);
}
}
});
}
#Override
public int getItemCount() {
return foodList.size();
}
}
And the main activity implements the interface, so when the item is clicked, in the main acitivty I have this code;
#Override
public void ItemClicked(View v, int position) {
foodList.get(position).setHasBeenEaten(true);
Log.d(TAG,foodList.get(position).getName() + " clicked");
Log.d(TAG,"position: " + position);
mAdapter.notifyDataSetChanged();
}
Why is this happening? Thanks
You are just missing the other option.
if(currentFoodItem.isHasBeenEaten()){
holder.eatenOverlay.setVisibility(View.VISIBLE);
} else {
holder.eatenOverlay.setVisibility(View.INVISIBLE);
}
needed an else branch, otherwise because of the recycling you may reuse the previous view, with the previous "configuration".
I have a recycler view, which consists of list. The items in the list have their own details like this (each item will have image, id, name, description), and the items are shuffeled, not in order.
Example : recyclerview item position = 0, list item id= 7.
recyclerview item position = 1, list item id= 5.
recyclerview item position = 2, list item id= 12.
So i need to get the list item id on recycler on click. If i click on recyclerview position = 0, If i have to get the item id as 7. So that i can work on that furthur based on that id.
My example code is
recommendedrecyclerView=(RecyclerView)view.findViewById(R.id.recommended_recycler_view);
recommendedrecyclerView.setNestedScrollingEnabled(false);
recommendedrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),OrientationHelper.VERTICAL,true));
recommendedrecyclerView.setItemAnimator(new DefaultItemAnimator());
I am getting data from server and setting it to adapter
setrecommendedAppsContent(response.body().getData());
public void setrecommendedAppsContent(List<FeaturedAppsData> data){
if (data!=null&&data.size()>0) {
recommendedAdapter = new RecommendedAdapter(mCurrentContext, data);
recommendedrecyclerView.setAdapter(recommendedAdapter);
recommendedAdapter.setClickListener(this);
}
}
This is my adapter class
public class RecommendedAdapter extends RecyclerView.Adapter<RecommendedAdapter.ItemViewHolder> {
private LayoutInflater inflater = null;
private Context context;
private List<FeaturedAppsData> itemsData;
private ClickListener clicklistener = null;
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription;
public ImageView recommendedAppIcon;
Button recommendedBtn;
String appId;
public void setClickListener(ClickListener clickListener) {
this.clicklistener = clickListener;
}
public RecommendedAdapter(Context context, List<FeaturedAppsData> itemsData) {
this.context = context;
this.itemsData = itemsData;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.recommendedAppTitle.setText(itemsData.get(position).getName());
holder.recommendedAppCategory.setText(itemsData.get(position).getApp_description());
holder.recommendedAppDescription.setText(itemsData.get(position).getOffer_description());
holder.recommendedAppBtn.setText(itemsData.get(position).getButton_text());
String imageUrl = String.valueOf(itemsData.get(position).getImage().getUrl());
Glide.with(context).load(ApiConstant.ApiBaseUrl + imageUrl).into(recommendedAppIcon);
appId=itemsData.get(position).getId();
}
#Override
public int getItemCount() {
int size = 0;
if (itemsData != null && itemsData.size() > 0) {
size = itemsData.size();
}
return size;
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
public ImageView recommendedAppIcon;
public Button recommendedAppBtn;
public ArrayList<FeaturedAppsData> dataItems;
private Context context;
public ItemViewHolder(Context context, View itemView, int viewType) {
super(itemView);
this.context = context;
itemView.setOnClickListener(this);
recommendedAppTitle = (TextView) itemView.findViewById(R.id.recommended_app_title);
recommendedAppCategory = (TextView) itemView.findViewById(R.id.recommended_app_category);
recommendedAppDescription = (TextView) itemView.findViewById(R.id.recommended_app_description);
recommendedAppIcon = (ImageView) itemView.findViewById(R.id.recommended_app_icon);
recommendedAppBtn=(Button)itemView.findViewById(R.id.recommended_app_card_btn);
}
#Override
public void onClick(View v) {
if (clicklistener != null) {
clicklistener.itemClicked(v, getAdapterPosition());
}
}
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.recommended_item_layout, parent, false);
recommendedAppIcon=(ImageView)view.findViewById(R.id.recommended_app_icon);
return new ItemViewHolder(context, view, viewType);
}
}
I am not sure of the Onclick method. So please suggest me as required along with Onclick event.
The setOnClickListener inside the ViewHolder is a good way.
Considering your current solution, the easiest way is to change the ClickListener interface, put the data inside the Holder during onBindViewHolder and then pass it to the listener during onClick. Like this:
Change the view holder fields to:
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
public ImageView recommendedAppIcon;
public Button recommendedAppBtn;
public FeaturedAppsData data; // <<< ADD THIS
// remove that u don't need public ArrayList<FeaturedAppsData> dataItems;
// remove that u don't need private Context context;
... the rest of your holder
then inside onBindViewHolder
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.data = itemsData.get(position);
... the rest on bind
then inside onClick, inside the holder:
#Override
public void onClick(View v) {
if (clicklistener != null) {
clicklistener.itemClicked(v, data.getId());
}
}
I have been struggling to implement a click listener on a RecyclerView. I have seen a couple of approaches and would prefer to handle ImageView clicks from the Activity as I need to do pass an Intent to another Activity and do some work off the main thread. Here is what I have implemented so far:
Register the click in a Custom RecyclerView Adapter
public class BasemapAdapter extends RecyclerView.Adapter<BasemapViewHolder> {
// Context to construct view
private Context mContext;
// Copy of all BasemapItems
private ArrayList<BasemapItem> items;
private int rowLayout;
public BasemapAdapter(ArrayList<BasemapItem> portalItems, int rowLayout, Context context){
this.mContext = context;
this.items = portalItems;
this.rowLayout = rowLayout;
}
#Override
public BasemapViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new BasemapViewHolder(view);
}
/**
* Fill the view with data from our adapter
*
* #param viewHolder
* #param position
*/
#Override
public void onBindViewHolder(BasemapViewHolder viewHolder, final int position){
// get the element from your dataset at this position
// replace the contents of the view with the element
viewHolder.basemapImage.setImageBitmap(items.get((position)).basemapImage);
viewHolder.basemapName.setText(items.get((position)).item.getTitle());
viewHolder.setBasemapClickListener(new BasemapViewHolder.BasemapClickListener() {
#Override
public void onBasemapItemClicked(View view, int position) {
Log.d("TAG", "clicked at " + position);
}
});
}
#Override
public int getItemCount(){
return items == null ? 0 : items.size();
}
}
Custom ViewHolder
public class BasemapViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
#InjectView(R.id.basemapName) TextView basemapName;
#InjectView(R.id.basemapImage) ImageView basemapImage;
private BasemapClickListener clickListener;
public BasemapViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
// set listener to whole item view
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
clickListener.onBasemapItemClicked(v, getPosition());
}
/* setter for the listener */
public void setBasemapClickListener(BasemapClickListener clickListener){
this.clickListener = clickListener;
}
/**
* A callback interface that indicates when a basemap has been clicked
*/
public interface BasemapClickListener{
/**
* Callback when the view is clicked
*
* #param view view that is clicked
* #param position position of the clicked basemap
*/
public void onBasemapItemClicked(View view, int position);
}
}
Create an instance of the adapter in Activity
mBasemapAdapter = new BasemapAdapter( mBasemapList , R.layout.row_basemap, this);
mRecyclerView.setAdapter(mBasemapAdapter);
If you want to handle the ImageView clicks only your should do it as follows:
#InjectView(R.id.basemapName) TextView basemapName;
#InjectView(R.id.basemapImage) ImageView basemapImage;
private BasemapClickListener clickListener;
public BasemapViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
// set listener to whole item view
//itemView.setOnClickListener(this); // comment out this line
basemapImage.setOnClickListener(this); // <---- add this line
}
and you should move setBasemapClickListener from onBindViewHolder() to onCreateViewHolder().
#Override
public BasemapViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
view.setBasemapClickListener(new BasemapViewHolder.BasemapClickListener() {
#Override
public void onBasemapItemClicked(View view, int position) {
Log.d("TAG", "clicked at " + position);
}
});
return new BasemapViewHolder(view);
}
I resolved this by setting the onClickListener on the TextView as it was overlaying the ImageView:
BasemapAdapter
#Override
public void onBindViewHolder(BasemapViewHolder viewHolder, final int position){
final BasemapItem item = items.get(position);
// get the element from your dataset at this position
// replace the contents of the view with the element
viewHolder.image.setImageBitmap(items.get(position).getImage());
viewHolder.title.setText(items.get(position).getItem().getTitle());
viewHolder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(item != null){
clickListener.onImageClick(items.indexOf(item));
}
}
});
viewHolder.itemView.setTag(item);
}
Then I can call the onBaseMapClickListener from inside the Activity
mBasemapAdapter.setOnBaseMapClickListener(new BasemapClickListener() {
#Override
public void onImageClick(int position) {
Toast.makeText(getApplicationContext(), "tapped" + position, Toast.LENGTH_LONG).show();
}
});
I've been trying to follow the following guide to implement an onClickListener on a RecyclerView
http://blog.lovelyhq.com/creating-lists-with-recyclerview-in-android-part-2/
However, I can't seem to get any context to call setOnClickListener or getPosition.
Here is my code (errors have comments next to them):
public class CategoryDataAdapter extends RecyclerView.Adapter<CategoryDataAdapter.DataViewHolder> implements View.OnClickListener
{
private List<CategoryData.Category> dataList;
private static Context con;
private ClickListener clickListener;
public CategoryDataAdapter(Context context, List<CategoryData.Category> list)
{
this.dataList = list;
con = context;
}
#Override
public int getItemCount()
{
return dataList.size();
}
#Override
public void onBindViewHolder(DataViewHolder dataViewHolder, int i)
{
CategoryData.Category item = dataList.get(i);
Picasso.with(con).load(item.getImage()).into(dataViewHolder.categoryImage);
dataViewHolder.categoryText.setText(item.getName());
}
#Override
public DataViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tile_category, viewGroup, false);
DataViewHolder dvh = new DataViewHolder(itemView);
return dvh;
}
public static class DataViewHolder extends RecyclerView.ViewHolder
{
protected ImageView categoryImage;
protected TextView categoryText;
public DataViewHolder(View v)
{
super(v);
categoryImage = (ImageView) v.findViewById(R.id.categoryImage);
categoryText = (TextView) v.findViewById(R.id.categoryName);
v.setOnClickListener(this); // here, can't put in 'this'
}
}
public interface ClickListener
{
/**
* Called when the view is clicked.
*
* #param v view that is clicked
* #param position of the clicked item
* #param isLongClick true if long click, false otherwise
*/
public void onClick(View v, int position, boolean isLongClick);
}
public void setClickListener(ClickListener clickListener)
{
this.clickListener = clickListener;
}
#Override
public void onClick(View v)
{
clickListener.onClick(v, getPosition(), false); // here, get position
}
}
What can I do to fix this? I only need a click listener for the entire view, not one particular item (already implemented that)
Instead of making RecyclerView.Adapter implement View.OnClickListener, make DataViewHolder implement that. then also save the rootview at your DataViewHolder variable (like ImageView and TextView) and at onBindViewHolder use something like below:
dataViewHolder.rootView.setOnClickListener(dataViewHolder);
Add interface for detect click listener in recyclerAdapter
public class DeviceAdapter extends ArrayAdapter<Blablah>{
private OnClickItemListener listener;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
................
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null){
listener.click(int position)
}
}
});
.............
return convertView;
}
public interface OnClickItemListener {
public void click(int position);
}
public void setOnClickItemListener(OnClickItemListener listener) {
this.listener = listener;
}
}
And set adapter Listener to your fragment/activity
adapterRecycler.setOnsetOnClickItemListener(new ....{
});