RecyclerView is loading twice product on click event in recyclerView in android

auto refresh recyclerview android
reset recyclerview adapter android
android-recyclerview update item
how to refresh recyclerview in android
android-recyclerview update without scroll
android recyclerview adapter
recyclerview adapter notifydatasetchanged not working in fragment
update recyclerview adapter in fragment

i have applied recyclerView in fragment in which i am showing product from api that is working fine. i have performed click listener on recyclerView, when i click the product and go to next fragment. But when i go to previous fragment that then my recycelerView loads data again and again. how i can avoid from this. i want to that it loads data once. here is my code xml code:

<android.support.v7.widget.RecyclerView
    android:id="@+id/lsCategory"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="4dp"
    android:layout_marginRight="2dp"
    android:background="@color/colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

here is my recyclerView Code:

 public class HomeFragment extends Fragment implements View.OnClickListener {
private List<Category> categories = new ArrayList<>();
    private CategoryAdapter categoryAdapter;

 public void getInfo(View view){
    lsCategory =  view.findViewById(R.id.lsCategory);
}
public void setInfo(){
    getData();
}
public void getData(){
    final StringRequest request = new StringRequest(Request.Method.GET, URLs.homeMainURL,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
                //categories
                JSONArray categoryArray = jsonObject.getJSONArray("ListCategories");
                setCategoryAdapter(categoryArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
         error.printStackTrace();
        }
    });
    VolleySingleton.getInstance(context).addToRequestQueue(request);
}
private void setCategoryAdapter(JSONArray array) {

    try {
        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            final String name = object.getString("Name");
            final int Id = object.getInt("ID");
            categories.add(new Category(Id, name));
        }

    }catch(JSONException e) {
        e.printStackTrace();
    }
    categoryAdapter = new CategoryAdapter(context, categories);
    lsCategory.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
    lsCategory.setAdapter(categoryAdapter);
}

and here is my category Adapter

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
    holder.setData(categories.get(position));
}
public class ViewHolder extends RecyclerView.ViewHolder {
    TextView textViewItemName;
    private int Id ;
    public ViewHolder(View itemView) {
        super(itemView);
        textViewItemName = itemView.findViewById(R.id.tvCategoris);
    }
    public void setData(final Category category){
        textViewItemName.setText(category.getCategoryName());
        Id = category.getId();

        textViewItemName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExpandableCategoryList fragment = new ExpandableCategoryList();
                Bundle bundle = new Bundle();
                bundle.putInt("Id",Id);
                fragment.setArguments(bundle);
                ((MainActivity)context).replaceFragment(fragment,
                        "TAG",null,false);
            }
        });
    }
}

I hope this will work for you.

Clear your arraylist before adding the data into it and add before try catch in your code. given like below and notify your adapter.

  if(categories!=null){
        categories.clear();
     }

try {
       for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            final String name = object.getString("Name");
            final int Id = object.getInt("ID");
            categories.add(new Category(Id, name));
        }

    }catch(JSONException e) {
        e.printStackTrace();
    }
    categoryAdapter = new CategoryAdapter(context, categories);
    rvCategory.setLayoutManager(new 
    LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
    rvCategory.setAdapter(categoryAdapter);
    categoryAdapter.notifyDataSetChanged();

Handle Click Events of Multiple Buttons Inside a RecyclerView , Handle Click Events of Multiple Buttons Inside a RecyclerView. JoCodes. loading​ Rate this:  According to the documentation, a RecyclerView is a flexible view for providing a limited window into a large data set. If you have done any android development before, you definitely know the popular ListView class used to display lists of things. After its release by Google however, most of us have moved on and now …


Use fragmentTransaction.add(containerViewId, fragment, tag); instead of fragmentTransaction.replace(containerViewId, fragment, tag);

Difference between add and replace is:

replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked.

Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in 'paused' state hence when a back button is pressed onCreateView is not called for the existing fragment(the fragment which was there before new fragment was added).

In terms of fragment's life cycle events onPause, onResume, onCreateView and other life cycle events will be invoked in case of replace but they wont be invoked in case of add.

Android working with RecyclerView, Android tutorial about recycler view rendering a simple list using a custom adapter class. Also covered adding the list separator and item click listener. What if i want to load my data from database is there any any function as the item product details opens and when button is clicked product is added to  android development tutorial,android programming tutorial,android app development tutorial,android tutorial for beginners,android app tutoria,android studio tutorial,learn android programming


Its a common problem faced by most of the android developers and can be solved by following methods. 1. Fist of all, clear the category object every time this fragment is loaded/called before adding data to it. Example. category.clear(); if its a list 2. In your adapter, add this setHasStableIds(true); inside the constructor. It will stop duplicating the data in your list. 3. Override following methods in your adapter (IMP)

    @Override
    public int getItemViewType(int position) {
     return position;
    }

    @Override
    public long getItemId(int position){
        return position;
    }
    @Override
    public int getItemCount() {
        return category.size();
    }

Let me know if it works!

Updating data in an Android RecyclerView - Suragch, This post will hopefully help you with that as you are learning how to update the adapter data in an Android RecyclerView. If you need help setting up the  RecyclerView As ListView In Android: In this article we will discuss how to use a RecyclerView as ListView. For that we need to understand LayoutManager component of RecyclerView. Layout Manager is basically a very new concept introduced in RecyclerView for defining the type of Layout which RecyclerView should use.


There are two ways:

First :

if you are using replace then you should use addtobackstack(null) method with it as well. It would not call previous fragment onCreate method again when going back from next fragment to previous fragment. Make sure you are callling API in onCreate Method.

Second :

Try to read about setRetain(True) in fragments, Maybe it'll help you to stop loading fragment oncreate method again. Make sure you are callling API in onCreate Method.

Hope this helps. Happy Coding :)

Android Fundamentals: Working with the RecyclerView, Adapter , A Simple RecyclerView / Adapter / ViewHolder Pattern. First, we will need a goal for our end product. Create a list containing some number of  When you need to display a scrolling list of some data (for example, products, posts, movies) you should use RecyclerView. It is a very useful component in Android as in almost of the application


Three Lessons Learned From Using RecyclerView, android:id="@+id/recycler_view". android:scrollbars="vertical". />. In the fragment​/activity, load up a reference to your RecyclerView as you  This video is part of a course named "SQLIte for Beginners 2019". It's free and available on my website: https://codingwithmitch.com/courses/sqlite-room-pers


RecyclerView item onClick · GitHub, RecyclerView does not have an OnItemClickListener like it's predecessor, ListView . setText(item); } @Override public void onClick(View view) { Log.d(​TAG, <TextView xmlns:android="http://schemas.android.com/apk/res/android" Listener and in item row I have a TextView that have click listener event In this video I show you how to add an OnClickListener to your recyclerview and display data in a new activity. In the new activity you'll learn to display an image using an ImageView widgets and


Checkboxes, To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for  Items in my RecyclerView are added with Click Listener. This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.