Android中RecyclerView上拉下拉,分割线,多条目的实例代码

Android中RecyclerView上拉下拉,分割线,多条目的实例代码,第1张

概述//activity的xml<?xmlversion=\"1.0\"encoding=\"utf-8\"?><RelativeLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\"

//activity的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:ID="@+ID/activity_main"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  tools:context="baway.com.cj.myrecyclervIEw2.MainActivity">  <androID.support.v4.Widget.SwipeRefreshLayout    androID:ID="@+ID/swipeRefreshLayout"    androID:layout_wIDth="match_parent"    androID:layout_above="@+ID/progress"    androID:layout_height="match_parent">    <androID.support.v7.Widget.RecyclerVIEw      androID:ID="@+ID/recyclerVIEw"      androID:layout_wIDth="match_parent"      androID:layout_height="match_parent">    </androID.support.v7.Widget.RecyclerVIEw>  </androID.support.v4.Widget.SwipeRefreshLayout>  <Progressbar    androID:ID="@+ID/progress"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_centerHorizontal="true"    androID:visibility="gone"    androID:layout_alignParentBottom="true"/></relativeLayout>item1的xml和item2的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:ID="@+ID/activity_main"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:background="#f00000"  tools:context="baway.com.cj.myrecyclervIEw2.MainActivity">  <TextVIEw    androID:ID="@+ID/name"    androID:layout_wIDth="match_parent"    androID:layout_height="50dp"    androID:textSize="20sp"    androID:gravity="center"    androID:textcolor="#00ff00"/></relativeLayout>item2<?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:ID="@+ID/activity_main"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:background="#f000ff"  tools:context="baway.com.cj.myrecyclervIEw2.MainActivity">  <TextVIEw    androID:ID="@+ID/name"    androID:layout_wIDth="match_parent"    androID:layout_height="50dp"    androID:textSize="20sp"    androID:gravity="center"    androID:textcolor="#00ff00"/></relativeLayout>//activity,有上啦下拉,添加分割线,package baway.com.cj.myrecyclervIEw2;import androID.graphics.color;import androID.os.Handler;import androID.support.v4.Widget.SwipeRefreshLayout;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.DefaultItemAnimator;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.Progressbar;import androID.Widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {  private RecyclerVIEw recyclerVIEw;  private MainAdapter mainAdapter;  private SwipeRefreshLayout swipeRefreshLayout;  private linearlayoutmanager linearlayoutmanager;  private Progressbar progress;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    initVIEw();    initData();  }  private voID initVIEw() {    recyclerVIEw = (RecyclerVIEw)findVIEwByID(R.ID.recyclerVIEw);    swipeRefreshLayout = (SwipeRefreshLayout) findVIEwByID(R.ID.swipeRefreshLayout);    progress = (Progressbar)findVIEwByID(R.ID.progress);    linearlayoutmanager = new linearlayoutmanager(this);    recyclerVIEw.setLayoutManager(linearlayoutmanager);    mainAdapter = new MainAdapter(this);    mainAdapter.setonClickListener(new MyItemOnClickListener() {      @OverrIDe      public voID onItemClick(VIEw vIEw,int position) {        Toast.makeText(MainActivity.this," "+position,Toast.LENGTH_SHORT).show();      }    });    recyclerVIEw.setAdapter(mainAdapter);    recyclerVIEw.addItem@R_419_3489@(new MyItem@R_419_3489@());    recyclerVIEw.setItemAnimator(new DefaultItemAnimator());    swipeRefreshLayout.setcolorSchemecolors(color.RED,color.GREEN,color.BLUE);    swipeRefreshLayout.setProgressVIEwOffset(true,200);    swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {      @OverrIDe      public voID onRefresh() {        new Handler().postDelayed(new Runnable() {          @OverrIDe          public voID run() {            swipeRefreshLayout.setRefreshing(false);          }        },3000);      }    });    recyclerVIEw.addOnScrollListener(new RecyclerVIEw.OnScrollListener() {      @OverrIDe      public voID onScrolled(RecyclerVIEw recyclerVIEw,int dx,int dy) {        super.onScrolled(recyclerVIEw,dx,dy);        int lastVisibleItemposition = linearlayoutmanager.findLastVisibleItemposition();        int itemCount = linearlayoutmanager.getItemCount();        if (lastVisibleItemposition + 1 >= itemCount && dy > 0) {          progress.setVisibility(VIEw.VISIBLE);          new Handler().postDelayed(new Runnable() {            @OverrIDe            public voID run() {              progress.setVisibility(VIEw.GONE);            }          },3000);        }      }      @OverrIDe      public voID onScrollStateChanged(RecyclerVIEw recyclerVIEw,int newState) {        super.onScrollStateChanged(recyclerVIEw,newState);      }    });  }  private voID initData() {    ArrayList<String> strings = new ArrayList<>();    for (int i = 0; i < 50; i++) {      strings.add("name "+i);    }    mainAdapter.setData(strings);    mainAdapter.notifyDataSetChanged();  }}//adpter有多条目展示package baway.com.cj.myrecyclervIEw2;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.TextVIEw;import java.util.ArrayList;/** * Created by cj on 2017/1/12. */public class MainAdapter extends RecyclerVIEw.Adapter {  private Context context;  private ArrayList<String> datas = new ArrayList<>();  private MyItemOnClickListener myItemOnClickListener;  private final int TYPE_0 = 0;  private final int TYPE_1 = 1;  public MainAdapter(Context context) {    this.context = context;  }  @OverrIDe  public int getItemVIEwType(int position) {    if (position %3 == 0) {      return TYPE_0;    } else {      return TYPE_1;    }  }  public voID setonClickListener(MyItemOnClickListener onClickListener) {    this.myItemOnClickListener = onClickListener;  }  @OverrIDe  public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    if (vIEwType == TYPE_0) {      VIEw inflate = LayoutInflater.from(context).inflate(R.layout.item_main,null);      return new MyVIEwHolder(inflate);    } else {      VIEw inflate = LayoutInflater.from(context).inflate(R.layout.item_main2,null);      return new MyVIEwHolder2(inflate);    }  }  @OverrIDe  public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder,final int position) {    if (getItemVIEwType(position) == TYPE_0) {      MyVIEwHolder myVIEwHolder = (MyVIEwHolder)holder;      myVIEwHolder.name.setText(datas.get(position));      myVIEwHolder.name.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {          myItemOnClickListener.onItemClick(v,position);        }      });    } else {      MyVIEwHolder2 myVIEwHolder = (MyVIEwHolder2)holder;      myVIEwHolder.name.setText(datas.get(position));      myVIEwHolder.name.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {          myItemOnClickListener.onItemClick(v,position);        }      });    }  }  @OverrIDe  public int getItemCount() {    return datas.size();  }  public voID setData(ArrayList<String> data) {    if (data == null) {      return;    }    this.datas.addAll(data);  }  public class MyVIEwHolder extends RecyclerVIEw.VIEwHolder{    public TextVIEw name;    public MyVIEwHolder(VIEw itemVIEw) {      super(itemVIEw);      name = (TextVIEw) itemVIEw.findVIEwByID(R.ID.name);    }  }  public class MyVIEwHolder2 extends RecyclerVIEw.VIEwHolder{    public TextVIEw name;    public MyVIEwHolder2(VIEw itemVIEw) {      super(itemVIEw);      name = (TextVIEw) itemVIEw.findVIEwByID(R.ID.name);    }  }}//自定义分割线继承系统的 RecyclerVIEw.Item@R_419_3489@package baway.com.cj.myrecyclervIEw2;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Rect;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.Log;import androID.vIEw.VIEw;/** * Created by cj on 2017/1/12. */public class MyItem@R_419_3489@ extends RecyclerVIEw.Item@R_419_3489@ {  private Paint paint;  public MyItem@R_419_3489@() {    paint = new Paint();    paint.setcolor(color.BLUE);  }  @OverrIDe  public voID onDraw(Canvas c,RecyclerVIEw parent,RecyclerVIEw.State state) {    super.onDraw(c,parent,state);    //c.drawCircle(300,300,paint);    int childCount = parent.getChildCount();    for (int i = 0; i < childCount; i++) {      VIEw childAt = parent.getChildAt(i);      c.drawRect(childAt.getleft(),childAt.getBottom(),childAt.getWIDth()+childAt.getleft(),childAt.getBottom()+5,paint);    }  }  @OverrIDe  public voID onDrawOver(Canvas c,RecyclerVIEw.State state) {    super.onDrawOver(c,state);//    int childCount = parent.getChildCount();//    for (int i = 0; i < childCount; i++) {//      VIEw childAt = parent.getChildAt(i);//      c.drawRect(childAt.getleft(),childAt.gettop(),paint);//    }  }  @OverrIDe  public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw.State state) {    super.getItemOffsets(outRect,vIEw,state);    outRect.set(20,20,5);  }}

以上所述是小编给大家介绍的AndroID中RecyclerVIEw上拉下拉,分割线,多条目的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android中RecyclerView上拉下拉,分割线,多条目的实例代码全部内容,希望文章能够帮你解决Android中RecyclerView上拉下拉,分割线,多条目的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存