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

Android RecyclerView实现数据列表展示效果

程序员文章站 2022-08-08 09:22:53
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 1.首先导入依赖:  compile 'com.android....

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

1.首先导入依赖: 

compile 'com.android.support:recyclerview-v7:25.3.1'

2.适配器类: 

public class newsadapter extends recyclerview.adapter {
 private view view;
 private context mcontext;
 private arraylist<newsdata.databean> mlist=new arraylist<>();

 public newsadapter(context mcontext) {
  this.mcontext = mcontext;
 }

 //找到需要导入的布局
 @override
 public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) {
  view = layoutinflater.from(mcontext).inflate(r.layout.news_item, null);
  return new viewholder(view);

 }
 //加载相应的内容
 @override
 public void onbindviewholder(recyclerview.viewholder holder, int position) {
  viewholder viewholder=(viewholder)holder;
  viewholder.textview.settext(""+getitem(position).getnews_title());
  glide.with(mcontext).load(getitem(position).getpic_url()).into(viewholder.ima);
 }

 //手写的重置数据的方法
 public void setdata(arraylist<newsdata.databean> list){
  if (list!=null&&list.size()>0){
   mlist.addall(list);
  }

 }
 //手写的获得视图的方法
 public newsdata.databean getitem(int position){
  return mlist.get(position);

 };

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


 //定义的viewholder类继承recyclerview.viewholder 并找到布局中的id
 public class viewholder extends recyclerview.viewholder{
  private textview textview;
  private imageview ima;
  public viewholder(view itemview) {
   super(itemview);
   textview=(textview)itemview.findviewbyid(r.id.tv);
   ima=(imageview)itemview.findviewbyid(r.id.image);
  }
 }
}

 3.主界面(用到recyclerview的主要是initdata()方法和handler): 

public class mainactivity extends appcompatactivity implements imainview{
 private mainpresenter mainp;
 private newsadapter md;
 private recyclerview recy;
 private arraylist<newsdata.databean> mlist=new arraylist<>();
 private string url="http://api.expoon.com/appnews/getnewslist/type/1/p/1";
 private handler hand=new handler(){
  @override
  public void handlemessage(message msg) {
   super.handlemessage(msg);
   gson gson=new gson();
   if (msg.what==1) {
    newsdata newsdata = gson.fromjson(msg.obj.tostring(), newsdata.class);
    mlist=(arraylist<newsdata.databean>) newsdata.getdata();

    md.setdata((arraylist<newsdata.databean>) mlist);
    md.notifydatasetchanged();
   }
  }
 };
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initview();
  initdata();
  initdatafromserver();
 }

 private void initview() {
  recy=(recyclerview)findviewbyid(r.id.recy);
 }

 private void initdatafromserver() {
  //加载网络数据,找p层去要
  mainp.loaddatafromserver(url);

 }

 private void initdata() {
  mainp=new mainpresenter(this);
  //设置适配器
  md=new newsadapter(this);
  //创建默认的线性layoutmanager
  recy.setlayoutmanager(new linearlayoutmanager(this));
  recy.setadapter(md);
 }

 //重写的imainview类接口的两个方法
 @override
 public void successfulcallback(string str) {
  message msg = message.obtain();
  msg.what=1;
  msg.obj=str;
  hand.sendmessage(msg);
 }

 @override
 public void errcallback(int code, string msg) {

 }
}

效果图:

Android RecyclerView实现数据列表展示效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。