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
原创粉丝点击