欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android RecyclerView详解及简单实例

程序员文章站 2024-02-23 10:55:28
android  recyclerview 小白今天第一次接触recyclerview,前辈大神告诉我这是一个很神奇的控件,一看就是一整天。 recycl...

android  recyclerview

小白今天第一次接触recyclerview,前辈大神告诉我这是一个很神奇的控件,一看就是一整天。

recyclerview中有规定好的方法去显示列表,图片甚至视频。还带有删除新建某一列表的方法。相对于listview这个
recyclerview控件就更加牛了。

明白的大神看一眼就懂,小白可以自己照源码敲一遍看看效果。这段程序是把a-z按列排列下来:

package com.example.osserver.recycler;

import android.app.activity;
import android.os.bundle;
import android.support.v7.app.actionbaractivity;
import android.support.v7.widget.*;
import android.support.v7.widget.linearlayoutmanager;
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 mrecyclerview;
  private list<string> mdatas;
  private homeadapter madapter;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    initdata();
    mrecyclerview = (recyclerview)findviewbyid(r.id.id_recyclerview);
    mrecyclerview.setlayoutmanager(new linearlayoutmanager(this));
    mrecyclerview.setadapter(madapter = new homeadapter());
    mrecyclerview.additemdecoration(new divideritemdecoration(this,divideritemdecoration.horizontal_list));

  }

  private void initdata() {
        mdatas = new arraylist<string>();
        for (int i ='a';i<='z';i++){
          mdatas.add(""+(char)i);
    }
  }
class homeadapter extends recyclerview.adapter<myviewholder>
{
  @override
  public myviewholder oncreateviewholder(viewgroup parent, int viewtype) {
    myviewholder holder = new myviewholder(layoutinflater.from(mainactivity.this).inflate(r.layout.item,parent,false));
    return holder;
  }

  @override
  public void onbindviewholder(myviewholder holder, int position) {
    holder.tv.settext(mdatas.get(position));

  }

  @override
  public int getitemcount() {
    return mdatas.size();
  }
}

  class myviewholder extends recyclerview.viewholder
  {
    textview tv;
    public myviewholder(view view) {
      super(view);
      tv = (textview)view.findviewbyid(r.id.id_num);
    }
  }

}

接下来就是列表的润色部分:

今天我也只是进行到了这一步,踩了好多坑。

package com.zhy.sample.demo_recyclerview;

/*
 * copyright (c) 2014 the android open source project
 *
 * licensed under the apache license, version 2.0 (the "license");
 * limitations under the license.
 */

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.support.v7.widget.recyclerview.state;
import android.util.log;
import android.view.view;


/**
 * this class is from the v7 samples of the android sdk. it's not by me!
 * <p/>
 * see the license above for details.
 */
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) {
    log.v("recyclerview - itemdecoration", "ondraw()");

    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);
    }
  }
}

不能添加图片,那就写这些好了。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!