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的基本使用方法

1.RecyclerView的使用方法(使用的Android Studio开发) 首先是recyclerview的配置问题,recyclerview是要导入android.support.v7.wi...
  • Javacainiao931121
  • Javacainiao931121
  • 2016年06月18日 09:26
  • 12219

RecyclerView基本使用

RecyclerView是android 5.0推出的新的控件,官方给出的说明是: RecyclerView is a more advanced and flexible version of ...
  • zhoudailiang
  • zhoudailiang
  • 2015年07月01日 13:20
  • 2289

RecyclerView的基本使用

整整写了两天的RecyclerView,ListView,分类型RecyclerView,分类型ListView,实在写得厌烦了,于是就索性来放松一下,写个博客做个整理。 学任何东西都应该一...
  • cjm2484836553
  • cjm2484836553
  • 2016年12月02日 21:16
  • 1284

RecyclerView的基本使用线性布局、Grid布局、瀑布流效果,以及实例讲解时间轴

  • 2017年09月21日 10:14
  • 11.96MB
  • 下载

RecyclerView的基本使用

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

RecyclerView的基本使用,例子在Android studio中跑通

  • 2016年03月11日 17:54
  • 6.83MB
  • 下载

RecyclerView的基本使用、万能adapter及多条目的实现、添加头部和底部、下拉刷新上拉加载、仿支付宝拖拽,滑动删除

  • 2017年08月18日 19:38
  • 29.24MB
  • 下载

RecyclerView的基本使用、封装、多条目布局、添加头部和底部

  • 2017年07月16日 22:37
  • 26.49MB
  • 下载

Recyclerview基本使用

顶栏菜单选择 RecyclerView.Adapter.MyViewHolder>:Adapter继承RecyclerView.Adapter,泛型Viewholder优化类 布局文件中android...
  • weixin_40321535
  • weixin_40321535
  • 2018年01月01日 20:28
  • 19

【Android】Android中的RecyclerView的基本使用

RecyclerView RecyclerView是一个更高级更灵活的ListView的版本,这个组件是一个能够包含能被有效的回收和滚动的视图(View)的大集合,当你有能被动态改变元素的列表时,...
  • White_Idiot
  • White_Idiot
  • 2017年01月30日 21:39
  • 308
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:RecyclerView的基本使用
举报原因:
原因补充:

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