Fork me on GitHub

安卓学习-- RecyclerView简单入门

一、加入JAR包

第一感觉这个东东,好复杂,没ListView来的快,方便

在项目中加入android-support-v7-recyclerview.jar包

这个包在extras\android\support\v7\recyclerview\libs这个目录下。
在studio中加入依赖

//添加依赖包
compile 'com.android.support:recyclerview-v7:+

二、在布局文件中加入RecyclerView。

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.liunan.recyclerviewstudy.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/main_recylist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

三在Activtiy中初始化

package com.liunan.recyclerviewstudy;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;

    private List<String> mDataList ;

    private MyAdapter myAdapter;

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

    }

    /**
     * 初始化
     */
    private void initView() {
        //找到这个Listview
        mRecyclerView = (RecyclerView) findViewById(R.id.main_recylist);
        //设置线性管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    }


    /**
     * 初始化数据
     */
    private void initData() {

        mDataList = new ArrayList<String>();
        for (int i=0;i<50;i++){
            mDataList.add("内容 - "+i);
        }
        /*
        设置适配器
         */
        myAdapter = new MyAdapter(mDataList);
        mRecyclerView.setAdapter(myAdapter);
    }





}

 

三设置Adapter与Item

这个Adapter要继承RecyclerView.Adapter

package com.liunan.recyclerviewstudy;


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

import java.util.List;

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

    private List<String> mDataList;

    public MyAdapter(List<String> list) {
        mDataList = list;
    }

    @Override
    public int getItemCount() {
        // 返回数据集合大小
        return mDataList == null ? 0 : mDataList.size();
    }


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

        //获取这个TextView

        TextView tv= holder.mTvTitle;

        tv.setText(mDataList.get(position));
    }


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


    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.item_tv);

        }


    }


}

item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:clickable="true"
        android:textSize="18sp"
        android:id="@+id/item_tv"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="#ff0"
        android:text="测试"/>

</LinearLayout>

运行效果

 

四更改为GridView

在Activity中将

//设置线性管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

 

更改为

//设置表格管理器
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));

效果

 

五使用瀑布式的方式

同样更改上面的地方

//设置瀑布式,交错
       mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));

更改adapter中onBindViewHolder方法

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

        //获取这个TextView

        TextView tv= holder.mTvTitle;

       // 获取布局参数

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tv.getLayoutParams();

        //根据数据的位置设置不同的高度与背景色
        if(position%2==0){
            //设置背景色
            tv.setBackgroundColor(Color.parseColor("#3300ff00"));
            //设置高度
            lp.height=100;
        }else{
            tv.setBackgroundColor(Color.parseColor("#3300ffff"));
            lp.height=50;
        }
        tv.setLayoutParams(lp);
        tv.setText(mDataList.get(position));
    }

效果

 

默认的分割线没有实现要自己实现RecyclerView.ItemDecoration

 



 

posted @ 2016-07-17 10:59  森林森  阅读(5809)  评论(1编辑  收藏  举报