How to display RecyclerView items after clicking on "View All" button

Viewed 1627

I am a beginner in android. I am creating an application where data will be shown through recyclerview, but I want that only some items should display in recyclerview.

However when the user clicks the "View All" button then only whole items should display. Can anyone tell me how can I do this?

viewtext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), AppviewActivity.class);
            intent.putExtra("mylist","newmodel");
            getContext().startActivity(intent);
        }
    });

DetailsActivity.java

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appview);
    ArrayList<String> mylist = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

}

NewModel class

public class NewModel  {
Drawable sociallogo;
String socailtext;
String href;
public NewModel(Drawable sociallogo, String socailtext, String href){
    this.sociallogo = sociallogo;
    this.socailtext= socailtext;
    this.href = href;

}
public Drawable getSociallogo(){
    return sociallogo;
}
public String getSocailtext(){
    return socailtext;
}
public String getHref(){
    return href;
}

}

2 Answers

getItemCount method work for limitation.

If you bind with the below code then it shows limited data.

private final int limit = 10;


 @Override
public int getItemCount() {
    if(arrayList.size() > limit){
        return limit;
    }
}

When clicking on the "View All" button then redirect to the new activity and bind data in recyclerview adapter:

If you bind with the below code then it shows All data.

@Override
    public int getItemCount() {
        if(arrayList.size() > limit){
            return ad_list.size();
        }
    }

Implement your model class like below code:

public class NewModel implements Serializable {

Now pass your data with below code:

viewtext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), AppviewActivity.class);
            intent.putExtra("mylist", newmodel);
            getContext().startActivity(intent);
        }
    });

Get your data in the new activity like:

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
ArrayList<NewModel> mylist = (ArrayList<NewModel>) bundle.getSerializable("mylist");
        }  

You can add a parameter in class Recycler View Adapter to set a limit data. That's the function to display data recycler view with limit data. You make 2 constructors in that class.

public class ArrayDataAdapter extends RecyclerView.Adapter<ArrayDataAdapter.ArrayDataViewHolder> {
    private int limit = 0;
    private ArrayData<ModelData> listData;

    // Constructor 1: To display all data
    public class ArrayDataAdapter(ArrayList<ModelData> listData) {
       this.listData = listData;
       this.limit = listData.size;
    }

    // Constructor 2: To display data with limit data
    public class ArrayDataAdapter(ArrayList<ModelData> listData, int limit) {
       this.listData = listData;
       this.limit = limit;
    }
}

So, when you click "View All", you call class Recycler View Adapter with constructor type 1. For viewing limited items , Call constructor type 2 ,then you set method getItemCount with "limit" variable.

Related