Prevent Recycleview scroll when notifyDataSetChange() - android

when i notify my recycleview using notifyDataSetChange() its automatically scroll to top.
I am make this adapter for multi pane purpose.
the app is only for tablet.
so can any please give me the solution for this.??
here is my recycleview adapter.
public class PickersListAdapter extends RecyclerView.Adapter<PickersListAdapter.DataObjectHolder>
{
private Context context;
private ArrayList<PickersModel> pickersModelArrayList;
private ItemsListFragment fragment;
public PickersListAdapter(Context context, ArrayList<PickersModel> pickersModelArrayList, ItemsListFragment itemsListFragment)
{
this.context = context;
this.pickersModelArrayList = pickersModelArrayList;
fragment = itemsListFragment;
}
#Override
public PickersListAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, final int viewType)
{
View view = LayoutInflater.from(context).inflate(R.layout.inflate_list_pickers, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(final PickersListAdapter.DataObjectHolder holder, final int position)
{
holder.myBackground.setSelected(pickersModelArrayList.get(position).isClicked());
holder.checkBox.setChecked(pickersModelArrayList.get(position).isChecked());
holder.txtItemName.setText(pickersModelArrayList.get(position).getItemName());
holder.txtitemQuantity.setText(pickersModelArrayList.get(position).getQuantity());
Log.e("image url...", pickersModelArrayList.get(position).getItemIcon());
Glide.with(context).load(pickersModelArrayList.get(position).getItemIcon()).placeholder(R.drawable.ic_item_icon).into(holder.imageView);
holder.myBackground.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
for (int i = 0; i < pickersModelArrayList.size(); i++)
{
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
}
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount()
{
return pickersModelArrayList.size();
}
public class DataObjectHolder extends RecyclerView.ViewHolder
{
private CheckBox checkBox;
private ImageView imageView;
private TextView txtItemName, txtitemQuantity;
private LinearLayout myBackground;
public DataObjectHolder(View itemView)
{
super(itemView);
myBackground = (LinearLayout) itemView.findViewById(R.id.myBackground);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxPickersItems);
imageView = (ImageView) itemView.findViewById(R.id.imgItemIconPickerItems);
txtItemName = (TextView) itemView.findViewById(R.id.txtItemNamePickerItems);
txtitemQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantityPickerItems);
}
}
}

Use setTag(), getTag() and then use different functions to notify.
Try something like this:
holder.myBackground.setTag(position);
holder.myBackground.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (null != v.getTag()) {
int position = (int) v.getTag();
for (int i = 0; i < pickersModelArrayList.size(); i++) {
pickersModelArrayList.get(i).setClicked(pickersModelArrayList.get(position).getItemName().equals(pickersModelArrayList.get(i).getItemName()));
}
notifyItemChanged(position);
}
}
});

Related

How to make visible only one listitem at a time in recyclerview?

I am using RecyclerView and it has two TextViews; one for a question and one for an answer. Following is the view for reference. By default, I set the answer TextView to invisible to the user. Now if the user clicks on button answer will be visible. So far it works fine, now what I want is if user open answer for question 1 answer will be visible, now if the user clicks second listitem button then first answer TextView should be hidden. I want only one answer to be visible at a time.
Question 1 (Button)
Answer
Question 2 (Button)
Answer
Question 3 (Button)
Answer
Question 4 (Button)
Answer
public class BuyerFaqsAdapter extends RecyclerView.Adapter<BuyerFaqsAdapter.MyViewHolder> {
private ArrayList<BuyerMemberShipFAQParent> brandList;
private Context context;
private int counter;
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvans,tvfaq;
public ImageView ivImage;
MyViewHolder(View view) {
super(view);
tvfaq = (TextView) view.findViewById(R.id.row_layout_faqtv);
tvans = (TextView) view.findViewById(R.id.row_layout_answertv);
ivImage = (ImageView) view.findViewById(R.id.row_layout_ivarrow);
}
}
public BuyerFaqsAdapter(final Context context, final ArrayList<BuyerMemberShipFAQParent> brandList) {
this.brandList = brandList;
this.context = context;
}
#Override
public BuyerFaqsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_layout_faq_expand_parent, parent, false);
return new BuyerFaqsAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final BuyerFaqsAdapter.MyViewHolder holder, final int position) {
holder.tvfaq.setText(brandList.get(position).getFAQParentText());
holder.tvans.setVisibility(View.GONE);
holder.tvans.setText(brandList.get(position).getFAQAnswerText());
holder.itemView.setId(position);
counter = 1;
holder.ivImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
holder.tvans.setVisibility(View.VISIBLE);
holder.ivImage.setImageResource(R.drawable.ic_arrow_faq_up);
counter = 2;
}
else {
holder.tvans.setVisibility(View.GONE);
holder.ivImage.setImageResource(R.drawable.ic_arrow_faq);
counter = 1;
}
}
});
}
#Override
public int getItemCount() {
return brandList.size();
}
I didn't tested it because I don't have your layout files but you should do something like this:
public class BuyerFaqsAdapter extends RecyclerView.Adapter<BuyerFaqsAdapter.MyViewHolder> {
private ArrayList<BuyerMemberShipFAQParent> brandList;
private Context context;
private MyViewHolder lastClickedHolder;
public BuyerFaqsAdapter(final Context context, final ArrayList<BuyerMemberShipFAQParent> brandList) {
this.brandList = brandList;
this.context = context;
}
#Override
public BuyerFaqsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_layout_faq_expand_parent, parent, false);
return new BuyerFaqsAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final BuyerFaqsAdapter.MyViewHolder holder, final int position) {
holder.tvfaq.setText(brandList.get(position).getFAQParentText());
holder.tvans.setVisibility(View.GONE);
holder.tvans.setText(brandList.get(position).getFAQAnswerText());
holder.itemView.setId(position);
holder.ivImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (lastClickedHolder != null) {
collapse(lastClickedHolder);
}
expand(holder);
lastClickedHolder = holder;
}
});
}
#Override
public int getItemCount() {
return brandList.size();
}
private void collapse(BuyerFaqsAdapter.MyViewHolder holder) {
holder.tvans.setVisibility(View.VISIBLE);
holder.ivImage.setImageResource(R.drawable.ic_arrow_faq_up);
}
private void expand(BuyerFaqsAdapter.MyViewHolder holder) {
holder.tvans.setVisibility(View.GONE);
holder.ivImage.setImageResource(R.drawable.ic_arrow_faq);
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvans, tvfaq;
public ImageView ivImage;
MyViewHolder(View view) {
super(view);
tvfaq = (TextView) view.findViewById(R.id.row_layout_faqtv);
tvans = (TextView) view.findViewById(R.id.row_layout_answertv);
ivImage = (ImageView) view.findViewById(R.id.row_layout_ivarrow);
}
}
}

Values out of ScrollView not get accessed to the Fragment from RecyclerAdapter

I have two buttons and one textview in each child row of Recyclerview. Those two buttons are used as triggers to count number and then write the number in textview.
Issue: Text-view value is not Updated while scrolling and the item-count only give the count of items in the list which is visible.
public class RecyclerAdapterItem extends RecyclerView.Adapter<RecyclerAdapterItem.ItemViewHolder> {
private LayoutInflater inflater;
private Context ctx;
public RecyclerAdapterItem(Context ctx) {
inflater = LayoutInflater.from(ctx);
this.ctx = ctx;
}
#Override
public RecyclerAdapterItem.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recyclerviewitem_item_tab, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final RecyclerAdapterItem.ItemViewHolder holder, int position) {
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
holder.Itemname.setText(ItemFragment.itemArrayList.get(position).getItem_name().toString());
Float rt=Float.valueOf(ItemFragment.itemArrayList.get(position).getRate());
holder.Rate.setText(String.valueOf(df.format(rt)));
holder.qty.setText(String.valueOf(ItemFragment.itemArrayList.get(position).getQty()));
}
#Override
public int getItemCount() {
return ItemFragment.itemArrayList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected Button btn_plus, btn_minus;
private TextView Itemname,Rate, qty;
public ItemViewHolder(View itemView) {
super(itemView);
Itemname = (TextView) itemView.findViewById(R.id.Itemname);
Rate=(TextView) itemView.findViewById(R.id.Rate);
qty = (TextView) itemView.findViewById(R.id.Qty);
btn_plus = (Button) itemView.findViewById(R.id.plus);
btn_minus = (Button) itemView.findViewById(R.id.minus);
btn_plus.setTag(R.integer.btn_plus_view, itemView);
btn_minus.setTag(R.integer.btn_minus_view, itemView);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == btn_plus.getId()){
View tempview = (View) btn_plus.getTag(R.integer.btn_plus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) + 1;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
} else if(v.getId() == btn_minus.getId()) {
View tempview = (View) btn_minus.getTag( R.integer.btn_minus_view);
TextView tv = (TextView) tempview.findViewById(R.id.Qty);
int number = Integer.parseInt(tv.getText().toString()) - 1;
if(number<0)
number =0;
tv.setText(String.valueOf(number));
ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
}
}
}
}
This is the recycleradapter code.The mentioned textview is qty.
If your code is working then only try this method and try to check update..
notifyDataSetChanged();
other wise make interfaces to puls and mins value update like below code ..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(Item str);//pass your object types.
}
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
Item item=mDataset.get(position);
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setPlus(true);
onItemClickListner.onClick(data);
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.setMinus(true);
onItemClickListner.onClick(data);
}
});
}
then after define adapter object and bind into recylcer view then call below code ..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
#Override
public void onClick(Item str) {
if (str.getPlus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
if (str.getMinus()){
str.setQty();// set your logic
recyclerViewAdpater.notifyDataSetChanged();
}
}
});
Move the click listeners for the buttons to BindView method
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDataset.get(position).setQty(mDataset.get(position).getQty() + 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDataset.get(position).getQty()==0)
{
holder.qty.setText(String.valueOf(0));
}
else
{
mDataset.get(position).setQty(mDataset.get(position).getQty() - 1);
holder.qty.setText(String.valueOf(mDataset.get(position).getQty()));
}
}
});
}

Android many item selected RecyclerView

If someone can help me. I've a RecyclerView with items but when i try to select ONE item, i can select many items. I think that the problem is in the onClick callback but i don't know exactly. This is the adapter code:
public class HorizontalRecyclerViewAdapter extends RecyclerView
.Adapter<HorizontalRecyclerViewAdapter
.DataObjectHolder> {
private ArrayList<HorizontalData> mDataset;
private static MyClickListener myClickListener;
private SparseBooleanArray selectedItem = new SparseBooleanArray();
public class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView mLabel;
TextView mDateTime;
LinearLayout linearLayout;
public DataObjectHolder(View itemView) {
super(itemView);
mLabel = (TextView) itemView.findViewById(R.id.item_list_view_text_view);
mDateTime = (TextView) itemView.findViewById(R.id.item_list_view_text_view_two);
linearLayout = (LinearLayout) itemView.findViewById(R.id.myBackground);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (selectedItem.get(getAdapterPosition(), false)) {
selectedItem.delete(getAdapterPosition());
v.setSelected(false);
} else {
selectedItem.put(getAdapterPosition(), true);
v.setSelected(true);
}
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public HorizontalRecyclerViewAdapter(ArrayList<HorizontalData> myDataset) {
mDataset = myDataset;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.date_item, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.linearLayout.setSelected(selectedItem.get(position, false));
holder.mLabel.setText(mDataset.get(position).getmTitle());
holder.mDateTime.setText(mDataset.get(position).getmSubTitle());
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
void onItemClick(int position, View v);
}
}
Thanks!
First of all you don't need SparseBooleanArray, because you want only one item to be selected. It'd be enough to have int selectedItemPos with would represent currently selected item position.
Here's a simple adapter that I've just created and it's working, with comments explaining what's happening:
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private ArrayList<String> items;
//field mentioned before
private int selectedItemPos = -1;
//I create some items to see if my adapter is working.
public TestAdapter() {
this.items = new ArrayList<>();
for (int i=0; i<20; i++) {
items.add("Test" + i);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false));
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
String item = items.get(position);
holder.mName.setText(item);
//This line sets selection state to true if current position is the same as selected one, and false otherwise.
holder.itemView.setSelected(selectedItemPos == position);
}
#Override
public int getItemCount() {
return items.size();
}
//helper method
protected void setSelectedItem(int position) {
int oldSelected = selectedItemPos;
selectedItemPos = position;
// update view of unselected item
notifyItemChanged(oldSelected);
//update view of just selected item
notifyItemChanged(selectedItemPos);
}
protected class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mName;
public ViewHolder(View view) {
super(view);
mName = (TextView) view.findViewById(R.id.taskListItem_name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
//if the clicked item is already selected,
// we will just unselect it and no new item will be selected
//(new one is -1, which is none)
int newSelectedItem = getAdapterPosition()==selectedItemPos?-1:getAdapterPosition();
//apply changes on adapter
setSelectedItem(newSelectedItem);
}
}
}
You should be able to easily merge this code with yours.

Set and Get item id of recycler view

I am new in RecyclerView. We can get clicked item position in RecyclerView, but can we set and get item's ID of RecyclerView? Because in my RecyclerView, the item has custom ID provided by server. I've try this code :
FragmentListProduct.java
productData = new JSONArray();
linearLayoutManager = new LinearLayoutManager(fragmentActivity);
lpAdapter = new ListProductAdapter(fragmentActivity, R.layout.list_product_grid, productData, null,
new ListProductHolder.OnProductClickListener() {
#Override
public void onProductClick(View v, long id) {
Toast.makeText(fragmentActivity, "id:"+id, Toast.LENGTH_SHORT).show();
MainActivity.setIdToRead((int) id);
MainActivity.replaceFragment(FragmentListProduct.this, new FragmentViewProduct());
}
});
rvProductList = (RecyclerView) mainView.findViewById(R.id.rvProductList);
rvProductList.setLayoutManager(linearLayoutManager);
rvProductList.setAdapter(lpAdapter);
rvProductList.setHasFixedSize(true);
ListProductHolder.java
public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvTitle;
TextView tvPrice;
ImageView ivImage;
private OnProductClickListener onProductClickListener;
ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
super(itemView);
this.onProductClickListener = onProductClickListener;
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onProductClickListener.onProductClick(view, getItemId());
}
public static interface OnProductClickListener {
public void onProductClick(View v, long id);
}
}
ListProdutAdapter.java
public class ListProductAdapter extends RecyclerView.Adapter<ListProductHolder> {
private Activity activity;
private int layoutResource;
private JSONArray productDataArray;
private StringSignature downloadSignature;
private ListProductHolder.OnProductClickListener onProductClickListener;
public ListProductAdapter(Activity activity, int layoutResource, JSONArray productDataArray, StringSignature downloadSignature, ListProductHolder.OnProductClickListener onProductClickListener) {
this.activity = activity;
this.layoutResource = layoutResource;
this.productDataArray = productDataArray;
this.downloadSignature = downloadSignature;
this.onProductClickListener = onProductClickListener;
}
#Override
public ListProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(activity).inflate(layoutResource, parent, false);
return new ListProductHolder(v, onProductClickListener);
}
#Override
public void onBindViewHolder(ListProductHolder holder, int position) {...}
#Override
public long getItemId(int i) {
long itemId = -1;
try {
itemId = productDataArray.getJSONObject(i).getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
return itemId;
}
}
But when i tried it, the ID is always -1. How to solve this? or is there another solution i can do?
Add click listener in the on bind view holder as view will be reused you have to set click for each view reused. Add click listener to root layout
#Override
public void onBindViewHolder(ListProductHolder holder, int position) {
final long itemId = productDataArray.getJSONObject(position).getInt("id");
holder.rootLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onProductClickListener.onProductClick(view, itemId);
}
});
}
You can use
itemView.setTag(your_id);
and onClick you need to use
view.getTag();
Check the below example
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.LogHolder>
{
public RecyclerAdapter()
{
}
#UiThread
protected void dataSetChanged()
{
notifyDataSetChanged();
}
#Override
public int getItemCount() {
//you can pass ur server response array here
//int count = (csMainJsonArr != null) ? csMainJsonArr.length() : 0;
int count = 20;
return count;
}
public class LogHolder extends RecyclerView.ViewHolder
{
TextView nameTxt = null;
TextView contactTxt = null;
TextView statusTxt = null;
ImageView listImg = null;
public LogHolder(View itemView)
{
super(itemView);
nameTxt = (TextView)itemView.findViewById(R.id.custom_NameTxt);
contactTxt = (TextView)itemView.findViewById(R.id.custom_ContactTxt);
statusTxt = (TextView)itemView.findViewById(R.id.custom_StatusTxt);
listImg = (ImageView)itemView.findViewById(R.id.custom_listImg);
}
}
#Override
public LogHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LogHolder holder = new LogHolder(inflater.inflate(R.layout.custom_design, parent, false));
return holder;
}
#Override
public void onBindViewHolder(LogHolder holder, int position)
{
//just get data from your json arrahy and add here
holder.nameTxt.setText("name "+position);
holder.contactTxt.setText("contact "+position);
holder.statusTxt.setText("status "+position);
//you need to set tag for holder image and holder item to get position of item
holder.listImg.setTag(position);
holder.itemView.setTag(position);
holder.listImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//create your own action
// using view.getTag() you can get the position of item clicked
int position = (int) view.getTag();
Log.d("=======", String.valueOf(position));
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//create your own action
//using view.getTag() you can get the position of item clicked
int position = (int) view.getTag();
Log.d("=======", String.valueOf(position));
}
});
}
}
and don't forget to declare this.
csRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
csAdapter = new RecyclerAdapter();
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
//manager.setReverseLayout(true);
//manager.setStackFromEnd(true);
csRecyclerView.setLayoutManager(manager);
csRecyclerView.setAdapter(csAdapter);
Does the method getItemId() int ListProductAdapter have any other code? Is it same with getItemId(int i) in ListProductAdapter.In my opinion,what you need to do is just replace getItemId() with productDataArray.getJSONObject(getLayoutPosition()).getInt("id").
public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvTitle;
TextView tvPrice;
ImageView ivImage;
private OnProductClickListener onProductClickListener;
ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
super(itemView);
this.onProductClickListener = onProductClickListener;
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onProductClickListener.onProductClick(view, productDataArray.getJSONObject(getLayoutPosition()).getInt("id"));
}
public static interface OnProductClickListener {
public void onProductClick(View v, long id);
}
}

Recyclerview Scrolling Glitch

I had some problem about scrolling with Card inside RecyclerView. There some additional gaps when I scroll inside the RecyclerView, you can see it in this gif:
This is my adapter. Is this because the fragment or what ?
public class PosItemAdapter extends RecyclerView.Adapter<PosItemAdapter.MyViewHolder> {
private List<Item> itemList;
private ArrayList<CartItem> cartList;
private Boolean isMixed = false;
public class MyViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public ImageView picture;
public TextView name;
public TextView desc;
public TextView price;
public MyViewHolder(View view) {
super(view);
mView = view;
picture = (ImageView) view.findViewById(R.id.list_pos_item_picture);
name = (TextView) view.findViewById(R.id.list_pos_item_name);
price = (TextView) view.findViewById(R.id.list_pos_item_price);
}
}
public PosItemAdapter(ArrayList<Item> itemList, Boolean isMixed) {
this.itemList = itemList;
this.isMixed = isMixed;
}
public Item getValueAt(int position) {
return itemList.get(position);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_pos_item_layout, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
if (itemList != null) {
return itemList.size();
} else {
return 0;
}
}
}
Solved, the problem was my card height. I set to wrap_content and the glitch fixed.

Categories

Resources