RecyclerView 简单使用
来源:互联网 发布:java二分法查找 编辑:程序博客网 时间:2024/06/04 18:55
RecyclerView是support.v7包中的控件,他可以通过设置LayoutManager来快速实现listview、gridview、瀑布流的效果,而且还可以设置横向和纵向显示,添加动画效果也非常简单(自带了ItemAnimation,可以设置加载和移除时的动画,方便做出各种动态浏览的效果)
item中用了cardview ,先在build.gradle中添加
compile 'com.android.support:recyclerview-v7:25.2.0'compile 'com.android.support:cardview-v7:25.2.0'
RecyclerView布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><android.support.v7.widget.RecyclerView android:id="@+id/recycler_joke_fragment" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>
RecyclerView item 布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <android.support.v7.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" app:cardCornerRadius="3dp" app:cardElevation="8dp" > <TextView android:id="@+id/joke_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:textSize="16sp"/> </android.support.v7.widget.CardView></RelativeLayout>
adapter类 JokeAdapter
public class JokeAdapter extends RecyclerView.Adapter<JokeAdapter.ViewHolder> { private List<Joke> mJokeList; public JokeAdapter(List<Joke> mJokeList) { this.mJokeList = mJokeList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.joke_list_item,parent,false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Joke joke =mJokeList.get(position); holder.jokeContent.setText(joke.getContent()); } @Override public int getItemCount() { return mJokeList.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView jokeContent; public ViewHolder(View itemView) { super(itemView); jokeContent =(TextView)itemView.findViewById(R.id.joke_content); } }}
在acitivity中或者fragement中添加 对recyclerView的使用
recyclerView=(RecyclerView) view.findViewById(R.id.recycler_joke_fragment); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); adapter=new JokeAdapter(jokeList); recyclerView.setAdapter(adapter);
其中的jokeList 是数据。Joke 实体类就不粘贴了。
最该注意的地方就是 Adapter的写法和 RecyclerView多了一步setLayoutManager 。
0 0
- RecyclerView的简单使用
- RecyclerView 的简单使用
- recyclerview的简单使用
- Recyclerview的简单使用
- RecyclerView的简单使用
- RecyclerView简单使用
- RecyclerView简单使用
- RecyclerView的简单使用
- RecyclerView的简单使用
- RecyclerView简单使用
- RecyclerView的简单使用
- RecyclerView简单使用介绍
- RecyclerView的简单使用
- RecyclerView的简单使用
- RecyclerView 简单使用
- RecyclerView的简单使用
- RecyclerView的简单使用
- RecyclerView的简单使用
- Codeforces Round #408 (Div. 2) A. Buying A House
- 降维方法学习
- 递归漫谈(二)
- FlashBuilder+FariyGUI+Laya引擎开发H5游戏
- Android layout属性大全
- RecyclerView 简单使用
- 函数的调用
- 关于WindowListener的使用
- iOS多线程并发,串行,同步,异步
- Mybatis中更新数据时遍历传入的Map对象
- Android数据访问存储之内存读写
- matlab2012b与VS2012混编总结(3)——关于用mcc指令生成的文件集中没有ctf文件
- c++获取运行路径
- 数据流图(DFD)画法