本文实例为大家分享了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实现数据列表展示效果全部内容,希望文章能够帮你解决Android RecyclerView实现数据列表展示效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)