(android) How to dynamically add items to the current nested RecyclerView - android

This picture shows the problem.
I am using nested RecyclerView right now.
What I want is that when the ADD button of the outer RecyclerView is pressed, the item of the inner RecyclerView is added.
But what I made is that no matter which button is pressed, the last added inner RecyclerView's item is added.
How can I add items each the proper RecyclerView position?
This is my code.
RoutineModel.java
public class RoutineModel {
String routine;
public RoutineModel(String routine) {
this.routine = routine;
}
public String getRoutine() {
return routine;
}
}
RoutineAdapter.java
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
public RoutineDetailAdapter detailAdapter;
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
}
public void addDetailItem() {
detailAdapter.addItem(new RoutineDetailModel());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.routine_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView routine;
Button addSet;
RecyclerView routine_detail;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.OnItemClick();
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
routine_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
routine_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
routine_detail.setHasFixedSize(true);
}
}
public interface OnRoutineItemClickListener {
public void OnItemClick();
}
}
RoutineDetailModel
public class RoutineDetailModel {
String set;
String weight;
String reps;
}
RoutineDetailAdapter
public class RoutineDetailAdapter extends RecyclerView.Adapter<RoutineDetailAdapter.ViewHolder>{
ArrayList<RoutineDetailModel> items = new ArrayList<>();
public void addItem(RoutineDetailModel item) {
items.add(item);
notifyDataSetChanged();
}
public ArrayList<RoutineDetailModel> getItem() {
return this.items;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.routine_detail_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineDetailModel item = items.get(position);
holder.setItem(item);
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView set;
public ViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
}
private void setItem(RoutineDetailModel item) {
set.setText("TEST");
}
}
}
MainActivity
public class WriteRoutineActivity extends AppCompatActivity {
RecyclerView routine_recyclerview;
TextView title;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_recyclerview.setAdapter(routineAdapter);
// add detail item
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnItemClick() {
routineAdapter.addDetailItem();
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_recyclerview = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_recyclerview.setLayoutManager(routineLayoutManger);
routine_recyclerview.setHasFixedSize(true);
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routineLayoutManger.scrollToPositionWithOffset(0, 0);
}
}

detailAdapter in RoutineAdapter gets re-assigned every time onCreateViewHolder gets called meaning it will always point to the RoutineDetailAdapter of the last item created in the RecyclerView.
A fix for this could be to pass the RoutineDetailAdapter of the clicked item with the click listener interface.
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
}
public void addDetailItem(RoutineDetailAdapter routineDetailAdapter) {
routineDetailAdapter.addItem(new RoutineDetailModel());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
RoutineDetailAdapter detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.routine_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView routine;
Button addSet;
RecyclerView routine_detail;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter routineDetailAdapter = (RoutineDetailAdapter) routine_detail.getAdapter();
listener.OnItemClick(routineDetailAdapter);
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
routine_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
routine_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
routine_detail.setHasFixedSize(true);
}
}
public interface OnRoutineItemClickListener {
public void OnItemClick(RoutineDetailAdapter adapter);
}
}
public class WriteRoutineActivity extends AppCompatActivity {
RecyclerView routine_recyclerview;
TextView title;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_recyclerview.setAdapter(routineAdapter);
// add detail item
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnItemClick(RoutineDetailAdapter routineDetailAdapter) {
routineAdapter.addDetailItem(routineDetailAdapter);
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_recyclerview = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_recyclerview.setLayoutManager(routineLayoutManger);
routine_recyclerview.setHasFixedSize(true);
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routineLayoutManger.scrollToPositionWithOffset(0, 0);
}
}

Related

Nested Recyclerview click listener not working

I have a nested recyclerview with Parent RV: representing a category whereas Child RV: represents a list of items of a certain category. Currently I am getting null pointer exception but I have no idea why. I have 2 interface - 1st, takes the position of the child RV (selected item) and the 2nd one - gets the position of parent RV position (i.e. which category was selected), child RV position and title of the selected item.
Issue:
ParentRVAdapter.java:57 -> rvItemClickListener.onChildItemClick(holder.getAdapterPosition(),position,itemTitle);
CatalogItemChildRVAdapter.java:68 -> onItemListener.onItemClick(getAdapterPosition());
Parent Recycler View:
public class ParentRVAdapter extends RecyclerView.Adapter<ParentRVAdapter.ViewHolder> implements CatalogItemChildRVAdapter.onItemListener{
private Context context;
private List<CatalogCategories> categories;
private CatalogItemChildRVAdapter itemChildRVAdapter;
private List<CategoryItem> categoryItemList;
private RVItemClickListener rvItemClickListener;
public void setRvItemClickListener(RVItemClickListener rvItemClickListener) {
this.rvItemClickListener = rvItemClickListener;
}
public ParentRVAdapter(Context context, List<CatalogCategories> categories) {
this.context = context;
this.categories = categories;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.home_parentrv_item,parent,false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.categoryTv.setText(categories.get(position).getCatTitles());
categoryItemList = categories.get(position).getCategoryItemList();
setCategoriesItemRV(holder.itemRv, categoryItemList);
itemChildRVAdapter.setMonItemListener(new CatalogItemChildRVAdapter.onItemListener() {
#Override
public void onItemClick(int position) {
String itemTitle = categoryItemList.get(position).getTitle();
rvItemClickListener.onChildItemClick(holder.getAdapterPosition(),position,itemTitle);
System.out.println("Works");
}
});
}
#Override
public int getItemCount() {
return categories.size();
}
#Override
public void onItemClick(int position) {}
public static final class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView categoryTv;
RecyclerView itemRv;
public ViewHolder(#NonNull View itemView) {
super(itemView);
categoryTv = itemView.findViewById(R.id.categoriesTv);
itemRv = itemView.findViewById(R.id.itemRv);
}
#Override
public void onClick(View view) {
}
}
private void setCategoriesItemRV(RecyclerView recyclerView, List<CategoryItem> categoryItemList){
itemChildRVAdapter = new CatalogItemChildRVAdapter(context,categoryItemList,this);
recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL,false));
recyclerView.setAdapter(itemChildRVAdapter);
}
public interface RVItemClickListener{
void onChildItemClick (int parentPos, int childPos, String item);
}
}
Child RecyclerView:
public class CatalogItemChildRVAdapter extends RecyclerView.Adapter<CatalogItemChildRVAdapter.ViewHolder> {
private Context context;
private List<CategoryItem> categoryItemList;
private onItemListener monItemListener;
public void setMonItemListener(onItemListener monItemListener) {
this.monItemListener = monItemListener;
}
public CatalogItemChildRVAdapter(Context context, List<CategoryItem> categoryItemList, onItemListener monItemListener) {
this.context = context;
this.categoryItemList = categoryItemList;
this.monItemListener = monItemListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.childrv_eacat_item,parent,false);
return new ViewHolder(view,monItemListener);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.image.setImageResource(categoryItemList.get(position).getImage());
holder.itemname.setText(categoryItemList.get(position).getTitle());
}
#Override
public int getItemCount() {
return categoryItemList.size();
}
public static final class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView itemname;
//onItemListener onItemListener;
int itempos;
public ViewHolder(#NonNull View itemView, onItemListener onItemListener) {
super(itemView);
//this.onItemListener = onItemListener;
image = itemView.findViewById(R.id.categoryitemIv1);
itemname = itemView.findViewById(R.id.categoryitemTv1);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemListener.onItemClick(getAdapterPosition());
itempos = getAdapterPosition();
System.out.println("Clicked at postion: "+itempos);
}
});
}
/* #Override
public void onClick(View view) {
onItemListener.onItemClick(getAdapterPosition());
itempos = getAdapterPosition();
System.out.println("Clicked at postion: "+itempos);
} */
}
public interface onItemListener{
void onItemClick(int position);
} }

How to to set onClick in the RecyclerView?

I have created a RecyclerView Adapter but now i'm struggling to do the onClick to open new activity.
This is my RecyclerView Adapter class:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private ArrayList<HashMap<String, String>> mDataset;
Context ctx;
public RecycleAdapter(ArrayList<HashMap<String, String>> mDataset) {
this.mDataset = mDataset;
this.ctx = ctx;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_procedure, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.getId().setText(mDataset.get(position).get("id"));
holder.getItemname().setText(mDataset.get(position).get("name"));
//holder.getIvImage().setBackground(mDataset.get(position).get("icon"));
}
#Override
public int getItemCount() {
return mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CardView cardView;
private TextView itemId;
private TextView tvName;
private ImageView ivImage;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemId = itemView.findViewById(R.id.tvProfileItemName);
tvName = itemView.findViewById(R.id.tvProfileItemAge);
ivImage = itemView.findViewById(R.id.imageView3);
}
public TextView getId() {
return itemId;
}
public TextView getItemname() {
return tvName;
}
public ImageView getIvImage() {
return ivImage;
}
#Override
public void onClick(View v) {
}
}
}
My Main Activity just display the data inside of a recycler view
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{"id", "name", "icon"},
new int[]{R.id.id, R.id.name, R.id.icon});
mAdapter = new RecycleAdapter(contactList);
mRecyclerView.setAdapter(mAdapter);
}
use interface
in recycler view adapter class
public interface recClick{
void onClickRec(int p);
}
private recClick rc;
public void setRecClick(recClick rc){
this.rc = rc
}
in recycler view holder class
#Override
public void onClick(View v) {
rc.onClickRec(getAdapterPosition());
}
in MainActivity use Interface
class MainActivity implements AdapterClass.recClick{
mAdapter.setRecClick(this);
#Override
public onClickRec(int p)
{
// do someting
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final int pos =position;
holder.getId().setText(mDataset.get(position).get("id"));
holder.getItemname().setText(mDataset.get(position).get("name"));
//holder.getIvImage().setBackground(mDataset.get(position).get("icon"));
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,NewActivity.class);
intent.putExtra("yourValue", String.valueOf(mDataset.get(position).get(pos)));
ctx.startActivity(intent);
}
});
}
Remember to give reference to your cardview
cardView = (CardView)itemView.findViewById(R.id.cardview);

Handle button click inside row in recyclerView

I would like to know how to handle position of the row if the adapter is changed - for example if I write something to SearchView - adapter has changed and position is different. I have done this (below), but it shows the item before changed position.
My issue is with Recycler view item click.
Could you help me with that?
Adapter
public class ContactsRecyclerAdapter extends RecyclerView.Adapter<ContactsRecyclerAdapter.BindingHolder> {
private static OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
private List<Contact> mContacts;
private List<Contact> mContactsFiltered;
SearchViewFilter filter;
Context mContext;
public ContactsRecyclerAdapter(Context context, List<Contact> contact) {
this.mContext = context;
this.mContacts = contact;
this.mContactsFiltered = contact;
filter = new SearchViewFilter(mContacts, this);
}
public static class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(final View rowView) {
super(rowView);
binding = DataBindingUtil.bind(rowView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.onItemClick(itemView, getLayoutPosition());
}
});
}
public ViewDataBinding getBinding() {
return binding;
}
}
#Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
BindingHolder holder = new BindingHolder(view);
return holder;
}
#Override
public void onBindViewHolder(BindingHolder holder, int position) {
final Contact contact = mContactsFiltered.get(position);
holder.getBinding().setVariable(BR.contact, contact);
holder.getBinding().executePendingBindings();
}
Fragment
public class ContactListFragment extends Fragment implements View.OnClickListener {
FloatingActionButton fabButton;
SearchView searchView;
RecyclerView recyclerView;
String name, number, email;
private List<Contact> mContact = new ArrayList<>();
private ContactsRecyclerAdapter mAdapter;
public static ContactListFragment newInstance() {
Bundle args = new Bundle();
ContactListFragment fragment = new ContactListFragment();
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact_list, container, false);
searchView = (SearchView) view.findViewById(R.id.search_view);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mContact = SugarRecord.listAll(Contact.class);
mAdapter = new ContactsRecyclerAdapter(getActivity(), mContact);
recyclerView.setAdapter(mAdapter);
//item click
mAdapter.setOnItemClickListener(new ContactsRecyclerAdapter.OnItemClickListener() {
#Override
public void onItemClick(View itemView, int position) {
Contact contact = SugarRecord.findById(Contact.class, (long) position + 1);
name = contact.getName();
number = contact.getNumber();
email = contact.getEmail();
showAlertDialog();
}
});
searchFilter();
return view;
}
public void sortList() {
if(mContact.size() > 0) {
if (!isSort) {
Collections.sort(mContact, new Comparator<Contact>() {
#Override
public int compare(Contact contact1, Contact contact2) {
return contact1.getName().compareToIgnoreCase(contact2.getName());
}
});
}
else {
Collections.reverse(mContact);
isSort = false;
}
}
mAdapter.notifyDataSetChanged();
Click listener does not work with a RecyclerView. use OnItemTouchListener()
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override public void onItemClick(View view, int position) {
// do whatever
}
#Override public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
you have to overide onclick in BindingHolder class. Inside onclick method invoke a callback method of object passed via constructor etc
public static class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(final View rowView) {
super(rowView);
binding = DataBindingUtil.bind(rowView);
public void onClick(View view) {
//call a callback method here
}
}
public ViewDataBinding getBinding() {
return binding;
}
}

Cardview click event is not working on android?

I show list of data in card view successfully but now i want to open another activity and show details of clicked data.
I have three class MainActivity, ContactAdapter, Contact.
Main Activity Code:
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] name,email;
ArrayList<Contact> list = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
name = getResources().getStringArray(R.array.person_name);
email = getResources().getStringArray(R.array.person_email);
int count = 0;
for (String Name : name)
{
Contact contact = new Contact(Name,email[count]);
count++;
list.add(contact);
}
recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter=new ContactAdapter(list);
recyclerView.setAdapter(adapter);
i tried but it's not working.
Adapter Code:
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
ArrayList<Contact> contacts = new ArrayList<Contact>();
public ContactAdapter(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contact CON = contacts.get(position);
holder.person_name.setText(CON.getName());
holder.person_email.setText(CON.getEmail());
}
#Override
public int getItemCount() {
return contacts.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
TextView person_name, person_email;
public ContactViewHolder(View view) {
super(view);
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "OnClick Version :",
Toast.LENGTH_SHORT).show();
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "OnLongClick Version :",
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
}
In your adapter do this:
public class ProdutoAdapter extends RecyclerView.Adapter<ProdutoAdapter.ProdutoViewHolder>{
private static ProdutoAdapter.ClickListener clickListener; //this is important
private List<Produto> produtos; //this is my list of beans
public ProdutoAdapter(List<Produto> produtos) {
this.produtos = produtos;
}
#Override
public ProdutoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_produtos, parent, false);
ProdutoViewHolder mvh = new ProdutoViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(ProdutoViewHolder holder, int position) {
//holder.nome.setText(this.produtos.get(position).getNome());
holder.img.setImageDrawable(produtos.get(position).getImgDrawable());
holder.nome.setText(produtos.get(position).getNome());
holder.preco.setText(String.valueOf(produtos.get(position).getPreco()));
}
#Override
public int getItemCount() {
return this.produtos.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class ProdutoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
Produto produto;
CardView cardView;
TextView nome;
TextView preco;
ImageView img;
ProdutoViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
cardView = (CardView) itemView.findViewById(R.id.cardviewProduto);
nome = (TextView) itemView.findViewById(R.id.nome);
preco = (TextView) itemView.findViewById(R.id.preco);
img = (ImageView) itemView.findViewById(R.id.foto);
}
public void setItem(Produto produto){
this.produto = produto;
}
public Produto getItem(){
return this.produto;
}
//this is what you'll need in your activity
#Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
#Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public void setOnItemClickListener(ClickListener clickListener){
ProdutoAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}
}
and in your activity:
public static ProdutoAdapter mAdapter;
myActivity.mAdapter.setOnItemClickListener(new ProdutoAdapter.ClickListener() {
#Override
public void onItemClick(int position, View v) {
//do what you want
}
#Override
public void onItemLongClick(int position, View v) {
}
});
myActivity.mRecyclerView.setAdapter(mAdapter);

RecyclerView is showing nothing

I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);

Categories

Resources