RecyclerView

1. 导入recyclerview包

app/build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:recyclerview-v7:22.1.1'
}

2. 在布局里加入RecyclerView

activity_recycler_view.xml

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

3. 显示Item的布局

recycleritem_recyclerview.xml

<?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="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="@drawable/recycler_view_item_ripple"
    android:paddingBottom="12dp"
    android:paddingEnd="16dp"
    android:paddingStart="16dp"
    android:paddingTop="12dp"
    android:layout_margin="2px">

    <FrameLayout
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginEnd="16dp">

        <ImageView
            android:id="@+id/iconBackgroundImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/home_list_bg" />
        <ImageView
            android:id="@+id/iconImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:textColor="@color/ta"
        android:textSize="19sp" />
</LinearLayout>

4. 新建数据类

RecyclerData.java

package com.silion.samples.recyclerview;

import android.content.Context;
import android.graphics.drawable.Drawable;

/**
 * Created by silion on 2015/11/4.
 */
public class RecyclerData {
    private Drawable mIcon;
    private String mTitle;
    private int mOffsetHeight;

    public RecyclerData(Context context, int icon, String title) {
        this(context, icon, title, 0);
    }

    public RecyclerData(Context context, int icon, String title, int offsetHeight) {
        this.mIcon = context.getResources().getDrawable(icon);
        this.mTitle = title;
        this.mOffsetHeight = offsetHeight;
    }

    public Drawable getmIcon() {
        return mIcon;
    }

    public String getmTitle() {
        return mTitle;
    }

    public int getmOffsetHeight() {
        return mOffsetHeight;
    }
}


5. RecyclerAdapter 继承于RecyclerView.Adapter

package com.silion.samples.recyclerview;

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

import com.silion.samples.R;

import java.util.List;

/**
 * Created by silion on 2015/11/4.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
    private Context mContext;
    private List<RecyclerData> mRecyclerDataList;


    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        protected ImageView mIconBackgroundImageView;
        protected ImageView mIconImageView;
        protected TextView mTitleTextView;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            mIconBackgroundImageView = (ImageView) itemView.findViewById(R.id.iconBackgroundImageView);
            mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
            mTitleTextView = (TextView) itemView.findViewById(R.id.titleTextView);
        }
    }

    interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int postition);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public RecyclerAdapter(Context context, List<RecyclerData> list) {
        mContext = context;
        mRecyclerDataList = list;
    }

    
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.recycleritem_recyclerview, viewGroup, false);
        RecyclerViewHolder viewHolder = new RecyclerViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, final int i) {
        RecyclerData recyclerData = mRecyclerDataList.get(i);
        viewHolder.mIconImageView.setImageDrawable(recyclerData.getmIcon());
        viewHolder.mTitleTextView.setText(recyclerData.getmTitle());
        viewHolder.mIconBackgroundImageView.setColorFilter(mContext.getResources().getColor(R.color.i));

        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
        layoutParams.height = 200 + recyclerData.getmOffsetHeight();
        viewHolder.itemView.setLayoutParams(layoutParams);

        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(viewHolder.itemView, viewHolder.getLayoutPosition());
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(viewHolder.itemView, viewHolder.getLayoutPosition());
                    return true;
                }
            });
        }
    }

    /**
     * @return the count of the item,
     * also the size of the data list
     */
    @Override
    public int getItemCount() {
        return mRecyclerDataList.size();
    }
}

6. 设置适配器

RecyclerViewActivity.java

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerAdapter = new RecyclerAdapter(this, mRecyclerDataList);
        mRecyclerAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecyclerViewActivity.this,
                        "CLICK : " + ((TextView) view.findViewById(R.id.titleTextView)).getText(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int postition) {
                Toast.makeText(RecyclerViewActivity.this,
                        "LONG CLICK" + ((TextView) view.findViewById(R.id.titleTextView)).getText(), Toast.LENGTH_SHORT).show();
                deleteItem(postition);
            }
        });
        mRecyclerView.setAdapter(mRecyclerAdapter);

7. 设置不同显示风格的LayoutManager

            case R.id.recycler_listView:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
                initDatas();
                return true;
            case R.id.recycler_gridView:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                initDatas();
                return true;
            case R.id.recycler_hor_gridView:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL));
                initDatas();
                return true;
            case R.id.recycler_staggered:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                initStaggeredData();
                return true;

你可能感兴趣的:(RecyclerView)