[英]How to display RecyclerView items after clicking on "View All" button
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.
我正在创建一个应用程序,其中数据将通过 recyclerview 显示,但我希望只有某些项目应显示在 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活动详情.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;
}
} }
getItemCount
method work for limitation. getItemCount
方法用于限制。
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:单击“查看全部”按钮时,重定向到新活动并在
recyclerview
适配器中绑定数据:
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.您可以在类 Recycler View Adapter 中添加一个参数来设置限制数据。 That's the function to display data recycler view with limit data.
这就是显示具有限制数据的数据回收器视图的功能。 You make 2 constructors in that class.
您在该类中创建了 2 个构造函数。
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.因此,当您单击“查看全部”时,您将使用构造函数类型 1 调用类 Recycler View Adapter。对于查看受限项目,调用构造函数类型 2,然后使用“limit”变量设置方法getItemCount 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.