Amr*_*dri 44 android staggered-gridview android-recyclerview
我找不到任何使用最好的例子RecyclerView有StaggeredGridLayoutManager.甚至在Android Docs中也没有.
Q1.我需要一些例子,可以给有关如何使用适当的解释
RecyclerView有StaggeredGridLayoutManager.Q2.任何人都可以告诉我是否可以使用
RecyclerViewwith 创建以下布局StaggeredGridLayoutManager

到目前为止,我发现这个链接根本没用.
我还发现了cardlib的这个链接,但它在实现上太复杂了,而且依赖性太大会不必要地增加我的应用程序大小.
g4t*_*4th 55
对于那些仍然登陆这个问题的人.
您可以根据需要修改以下代码:
首先为Android RecyclerView和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'
Run Code Online (Sandbox Code Playgroud)
您的主要活动布局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>
Run Code Online (Sandbox Code Playgroud)
在名为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>
Run Code Online (Sandbox Code Playgroud)
将此布局定义为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;
}
}
Run Code Online (Sandbox Code Playgroud)
定义自定义适配器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();
}
}
Run Code Online (Sandbox Code Playgroud)
我们还需要每个ItemObject的视图.因此,定义一个类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();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在MainActivity中,将一个StaggeredGridLayoutManager实例分配给recycler_view,以定义组件的显示方式.
还使用SampleRecyclerViewAdapter实例填充卡片,如下所示
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;
}
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样
为了您的要求,您可以在book_list_item.xml结合的ImageView的和相应的填充它SampleViewHolders
另外请注意,从2改列数为3.
你可以在MainActivity为改变声明
_sGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(_sGridLayoutManager);
Run Code Online (Sandbox Code Playgroud)
这是另一个简单的教程
假设您已经创建了一个适配器并初始化了RecyclerView,以下代码应该可以满足您的需求.
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
Run Code Online (Sandbox Code Playgroud)
有关更多参考和文档,请查看以下链接:https: //developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
| 归档时间: |
|
| 查看次数: |
33553 次 |
| 最近记录: |