码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView(替代ListView)用法介绍

时间:2015-06-30 16:32:27      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:recycler   回收   listview   

在build.gradle文件添加下面代码

 compile ‘com.android.support:cardview-v7:21.0.3‘
 compile ‘com.android.support:recyclerview-v7:21.0.3‘

item类型相同的情况

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>

页面代码

public class OneActivity extends Activity {
    private RecyclerView mRecyclerView;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 类似于listview
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 类似于grid view
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
        mRecyclerView.setAdapter(new RecyclerViewAdapter(this));

    }

    //RecyclerView适配器
    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context mContext;
        private LayoutInflater mLayoutInflater;
        private String[] mCityName;

        public RecyclerViewAdapter(Context mContext) {
            this.mContext = mContext;
            mLayoutInflater = LayoutInflater.from(mContext);
            mCityName = mContext.getResources().getStringArray(R.array.city_name);
        }

        //创建新ViewHolder,被LayoutManager所调用
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new RecyclerViewViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((RecyclerViewViewHolder) holder).mTextView.setText(mCityName[position]);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mCityName == null ? 0 : mCityName.length;
        }

        //自定义的ViewHolder,持有每个Item的的所有界面元素
        public class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
            TextView mTextView;

            public RecyclerViewViewHolder(View itemView) {
                super(itemView);
                mTextView = (TextView) itemView.findViewById(R.id.tv_city);
                mTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.show(mContext, getPosition() + "");
                    }
                });
            }
        }
    }
}

效果图:
技术分享

item类型不同的情况

设配器代码

//RecyclerView适配器
public class MultipleRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public static enum ITEM_TYPE {
        ITEM_TYPE_IMAGE, ITEM_TYPE_TEXT
    }

    public MultipleRecyclerViewAdapter(Context mContext) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    //创建新ViewHolder,被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_TYPE_TEXT.ordinal()) {
            return new TextViewViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
        } else {
            return new ImageViewViewHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));
        }
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return 10;
    }

    //Item类型
    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_TEXT.ordinal() : ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal();
    }

    //ItemHolder类型一
    public class TextViewViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        public TextViewViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_city);
        }
    }

    //ItemHolder类型二
    public class ImageViewViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;

        public ImageViewViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.iv_city);
        }
    }

}

效果图:
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

RecyclerView(替代ListView)用法介绍

标签:recycler   回收   listview   

原文地址:http://blog.csdn.net/pengkv/article/details/46695523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!