没有关于Android Docs中的RecyclerView和StaggeredGridLayoutManager的好示例 [英] No good example about RecyclerView and StaggeredGridLayoutManager in Android Docs

查看:73
本文介绍了没有关于Android Docs中的RecyclerView和StaggeredGridLayoutManager的好示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到将RecyclerViewStaggeredGridLayoutManager结合使用的更好示例.甚至在 Android文档中.

I couldn't find any better example for using RecyclerView with StaggeredGridLayoutManager. Not even in Android Docs.

第一季度.我需要一些示例,这些示例可以正确说明如何将RecyclerViewStaggeredGridLayoutManager结合使用.

Q1. I need some examples which can give proper explanation about how to use RecyclerView with StaggeredGridLayoutManager.

第二季度.谁能告诉我是否可以使用RecyclerViewStaggeredGridLayoutManager

Q2. Can anyone tell me if it is possible to create following layout using RecyclerView with StaggeredGridLayoutManager

到目前为止,我发现了这个链接,它不是完全有用.

So far i have found this link which is not at all useful.

我还为cardslib找到了这个链接,但是它实现起来过于复杂且具有过多的依赖性这将不必要地增加我的应用大小.

I also found this link for cardslib but it is too much complex in implementation and has too much dependencies which will increase my app size unnecessarily.

推荐答案

面向仍在着手解决此问题的人员.

您可以根据需要修改以下代码:
首先为Android RecyclerView和CardView添加依赖库

For those who are still landing up on this question.

You could modify the following code as per your needs:
First add dependency libraries for Android RecyclerView and CardView

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'

您的主要活动布局 activity_main.xml 就像

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="7dp"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</RelativeLayout>


在名为 book_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/BookName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/AuthorName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/country_photo"
        android:background="#1976D2"
        android:gravity="center_horizontal"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="@string/hello_world"
        android:textColor="#ffffff"
        android:textSize="13sp" />
</LinearLayout>

</android.support.v7.widget.CardView> 


将该布局定义为类 ItemObject.java


Define this layout as a class ItemObject.java

public class ItemObject
{
    private String _name;
    private String _author;

    public ItemObject(String name, String auth)
    {
        this._name = name;
        this._author = auth;
    }

    public String getName()
    {
        return _name;
    }

    public void setName(String name)
    {
        this._name = name;
    }

    public String getAuthor()
    {
        return _author;
    }

    public void setAuthor(String auth)
    {
        this._author = auth;
    }
}


定义自定义适配器 SampleRecyclerViewAdapter 来填充卡

public class SampleRecyclerViewAdapter extends RecyclerView.Adapter<SampleViewHolders>
{
    private List<ItemObject> itemList;
    private Context context;

    public SampleRecyclerViewAdapter(Context context,
        List<ItemObject> itemList)
    {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public SampleViewHolders onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.book_list_item, null);
        SampleViewHolders rcv = new SampleViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(SampleViewHolders holder, int position)
    {
        holder.bookName.setText(itemList.get(position).getName());
        holder.authorName.setText(itemList.get(position).getAuthor());
    }

    @Override
    public int getItemCount()
    {
        return this.itemList.size();
    }
}


我们还需要为每个 ItemObject 使用一个视图支架.因此,定义一个类 SampleViewHolders


We would also need a viewholder for each ItemObject. So define a class SampleViewHolders

public class SampleViewHolders extends RecyclerView.ViewHolder implements
    View.OnClickListener
{
    public TextView bookName;
    public TextView authorName;

    public SampleViewHolders(View itemView)
    {
        super(itemView);
        itemView.setOnClickListener(this);
        bookName = (TextView) itemView.findViewById(R.id.BookName);
        authorName = (TextView) itemView.findViewById(R.id.AuthorName);
    }

    @Override
    public void onClick(View view)
    {
        Toast.makeText(view.getContext(),
            "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT)
            .show();
    }
}

现在在 MainActivity 中,将StaggeredGridLayoutManager实例分配给 recycler_view 以定义组件的显示方式.
还使用 SampleRecyclerViewAdapter实例填充卡片,如下

Now in MainActivity, assign an instance of StaggeredGridLayoutManager to recycler_view to define how the components will appear.
Also populate the cards using instance of SampleRecyclerViewAdapter, as follows

public class MainActivity extends AppCompatActivity
{
    private StaggeredGridLayoutManager _sGridLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        _sGridLayoutManager = new StaggeredGridLayoutManager(2,
            StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(_sGridLayoutManager);

        List<ItemObject> sList = getListItemData();

        SampleRecyclerViewAdapter rcAdapter = new SampleRecyclerViewAdapter(
            MainActivity.this, sList);
        recyclerView.setAdapter(rcAdapter);
    }

    private List<ItemObject> getListItemData()
    {
        List<ItemObject> listViewItems = new ArrayList<ItemObject>();
        listViewItems.add(new ItemObject("1984", "George Orwell"));
        listViewItems.add(new ItemObject("Pride and Prejudice", "Jane Austen"));
        listViewItems.add(new ItemObject("One Hundred Years of Solitude", "Gabriel Garcia Marquez"));
        listViewItems.add(new ItemObject("The Book Thief", "Markus Zusak"));
        listViewItems.add(new ItemObject("The Hunger Games", "Suzanne Collins"));
        listViewItems.add(new ItemObject("The Hitchhiker's Guide to the Galaxy", "Douglas Adams"));
        listViewItems.add(new ItemObject("The Theory Of Everything", "Dr Stephen Hawking"));

        return listViewItems;
    }
}

输出看起来像这样

根据您的要求,您可以将ImageView合并到book_list_item.xml中,并相应地将其填充到 SampleViewHolders
另请注意,要将列数从2更改为3.

您可以将MainActivity中的声明更改为

Output will look something like this

For your requirement, you can incorporate an ImageView in book_list_item.xml and populate it accordingly in SampleViewHolders
Also note, to change number of columns from 2 to 3.

You could change declaration in MainActivity as

_sGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(_sGridLayoutManager);

哪个会给出这样的输出

Which will give an output like this

这是另一个简单的教程

这篇关于没有关于Android Docs中的RecyclerView和StaggeredGridLayoutManager的好示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆