RecyclerView的基本使用

转载 2016年06月01日 16:11:56

RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用。 下面来看下基本实现的代码

MainActivity.java

package recyclerviewdemo.czh.com.myrecyclerviewdemo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
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.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private RecyclerView act_main_recyclerView;
    private List<String> mDates;
    private MyAdapter myAdapter;

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

    private void initData() {
        mDates = new ArrayList<>();
        for (int i = 'A'; i <= 'Z'; i++) {
            mDates.add("" + (char) i);
        }
    }

    private void initView() {
        act_main_recyclerView = (RecyclerView) findViewById(R.id.act_main_recyclerView);
        act_main_recyclerView.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MyAdapter();
        act_main_recyclerView.setAdapter(myAdapter);
        act_main_recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));


    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        class MyViewHolder extends RecyclerView.ViewHolder  {
            TextView recyclerViewTv;

            public MyViewHolder(View itemView) {
                super(itemView);
                recyclerViewTv = (TextView) itemView.findViewById(R.id.recyclerViewTv);
            }

        }

        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.recyclerview_item,viewGroup,false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.MyViewHolder myViewHolder, int i) {
            myViewHolder.recyclerViewTv.setText(mDates.get(i));
        }

        @Override
        public int getItemCount() {
            return mDates.size();
        }
    }
}
DividerItemDecoration.java

package recyclerviewdemo.czh.com.myrecyclerviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Administrator on 2016/5/31.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {

        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }

    }


    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}
activity_main.xml

<?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.support.v7.widget.RecyclerView
        android:id="@+id/act_main_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#ffff0000"
        android:dividerHeight="10dp" />
</RelativeLayout>
recyclerview_item.xml

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

    <TextView
        android:id="@+id/recyclerViewTv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"/>

</FrameLayout>
转载自http://blog.csdn.net/lmj623565791/article/details/45059587; 


相关文章推荐

RecyclerView的基本使用

  • 2015年07月04日 11:48
  • 4.57MB
  • 下载

2.RecyclerView基本使用

详解RecyclerView系列文章目录概述上篇文章我们简单介绍了什么是RecyclerView,以及他有什么什么优势。那么从这篇文章开始我们就开始一步一步学习RecyclerView的使用方法。实现...

RecyclerView基本使用

RecyclerView是android 5.0推出的新的控件,官方给出的说明是: RecyclerView is a more advanced and flexible version of ...

Android控件之RecyclerView的基本使用

一、为什么叫RecyclerView? 1.不关心Item是否显示在正确的位置、如何显示。 2.不关心Item间的分割线。 3.不关注Item增加和删除的动画效果。 4.仅仅关注如何回...
  • Wtoria
  • Wtoria
  • 2016年06月03日 13:33
  • 472

Android 控件 RecyclerView 的基本使用

android 控件 RecyclerView 的基本用法。

RecyclerView CardView的基本使用

RecyclerView  看Java Code Geeke上说的是他比ListView更加的灵活, 我们都知道如何在我们的应用程序中使用列表视图,我们知道如果我们想增加ListView的表现,我们可...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:RecyclerView的基本使用
举报原因:
原因补充:

(最多只允许输入30个字)