RecyclerView

使用RecyclerView

RecyclerView 小部件比 ListView 更高级且更具灵活性。 此小部件是一个用于显示庞大数据集的容器,可通过保持有限数量的视图进行非常有效的滚动操作。 如果您有数据集合,其中的元素将因用户操作或网络事件而在运行时发生改变,请使用 RecyclerView 小部件。
使用RecyclerView需要添加类依赖项

dependencies {
    ...
    compile 'com.android.support:recyclerview-v7:21.0.+'
}
package com.s3abiscuit.android.testrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRecyclerView = new RecyclerView(this);
        setContentView(mRecyclerView);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new RecyclerView.Adapter() {

            class MyViewHolder extends RecyclerView.ViewHolder{
                private TextView tv;

                public MyViewHolder(TextView tv) {
                    super(tv);
                    this.tv = tv;
                }

                public TextView getTv() {
                    return tv;
                }
            }
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyViewHolder(new TextView(parent.getContext()));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((MyViewHolder)holder).getTv().setText(data[position]);
            }

            @Override
            public int getItemCount() {
                return data.length;
            }

            private String[] data = new String[]{"a","b","c","d","a","b","c","d","a","b","c","d",
                    "a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d"};

        });
    }
}

效果如下:

使用资源文件自定义列表项

新建list_cell.xml 包括两个TextView,一个新闻标题,一个新闻内容

<?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">

    <TextView
        android:id="@+id/tvTitle"
        android:textSize="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:id="@+id/tvContent"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

将Adapter独立成一个文件,命名为MyAdapter

package com.s3abiscuit.android.testrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by s3abiscuit on 2017/10/2.
 */
class MyAdapter extends RecyclerView.Adapter {

    class MyViewHolder extends RecyclerView.ViewHolder {
        private View root;
        private TextView tvTitle;
        private TextView tvContent;

        public MyViewHolder(View root) {
            super(root);
            tvTitle = root.findViewById(R.id.tvTitle);
            tvContent = root.findViewById(R.id.tvContent);
        }

        public TextView getTitle() {
            return tvTitle;
        }
        public TextView getContent(){return tvContent;}
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.getTitle().setText(data[position].title);
        myViewHolder.getContent().setText(data[position].content);
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    private CellData[] data = new CellData[]{new CellData("This is Title 1","This is Content 1")
            ,new CellData("This is Title 1","This is Content 1")
            ,new CellData("This is Title 2","This is Content 2")
            ,new CellData("This is Title 3","This is Content 3")
            ,new CellData("This is Title 4","This is Content 4")
            ,new CellData("This is Title 5","This is Content 5")
            ,new CellData("This is Title 6","This is Content 6")
            ,new CellData("This is Title 7","This is Content 7")
            ,new CellData("This is Title 8","This is Content 8")
            ,new CellData("This is Title 9","This is Content 9")
            ,new CellData("This is Title 10","This is Content 10")
            ,new CellData("This is Title 11","This is Content 11")
            ,new CellData("This is Title 12","This is Content 12")};

}

效果如下:

更改RecyclerView布局样式

RecyclerView 提供这些内置布局管理器:

  • LinearLayoutManager 以垂直或水平滚动列表方式显示项目。
  • GridLayoutManager 在网格中显示项目。
  • StaggeredGridLayoutManager 在分散对齐网格中显示项目。

修改MainActivity.java

mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));

效果如下:

posted @ 2017-10-02 17:35  s3abiscuit  阅读(258)  评论(0编辑  收藏  举报