简体   繁体   English

单击“查看全部”按钮后如何显示 RecyclerView 项目

[英]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.

相关问题 单击按钮后如何在所有回收站视图行中隐藏视图? - how to hide a view in all recyclerview row after button clicked? 如何通过单击 recyclerview 中的项目在具有 web 视图的新片段上加载网站? - How to load a website on a new fragment with a web view by clicking on items in the recyclerview? 添加了notifyItemInserted的RecyclerView项目仅在重新加载View后显示 - RecyclerView Items added with notifyItemInserted only display after View is reloaded 单击recyclerview项目后无法更改片段 - Not able to change fragment after clicking on recyclerview items 单击适配器内的按钮后,从自己的活动中更改 recyclerview 适配器项目 - Change recyclerview adapter items from own activity after clicking on button inside adapter 如何通过单击按钮在TextView中显示数组项 - How can I display Array items in a TextView by clicking button 在 RecyclerView 中单击项目的问题 - Problem with Clicking Items in RecyclerView 单击按钮获取api后如何在片段中显示recyclerview - how to display recyclerview in a fragment after fetching an api on the click of a button 更改RecyclerView所有项目的View可见性 - Change the View visibility of all items of RecyclerView 单击“提交”按钮后如何显示输入的数据? - How to display my inputted data after clicking “submit” button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM