怎么在Android应用中利用RecyclerView实现一个网格布局-创新互联
本篇文章给大家分享的是有关怎么在Android 应用中利用RecyclerView实现一个网格布局,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
专业领域包括成都做网站、网站设计、外贸营销网站建设、电子商务商城网站建设、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,创新互联公司的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。activity_main.xml
message_main_notice_list.xml
适配器MyRecyclerViewAdapter.java:
package com.example.administrator.recyclerviewtest; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class MyRecyclerViewAdapter extends RecyclerView.Adapter{ private List mList; static class ViewHolder extends RecyclerView.ViewHolder{ View myView; ImageView imageView; TextView title; public ViewHolder(View itemView) { super(itemView); myView = itemView; imageView = (ImageView) itemView.findViewById(R.id.iv_image); title = (TextView) itemView.findViewById(R.id.tv_title); } } public MyRecyclerViewAdapter(List list){ this.mList = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_main_notice_list,null); final ViewHolder holder = new ViewHolder(view); return holder; } //将数据绑定到控件上 @Override public void onBindViewHolder(ViewHolder holder, int position) { ItemBean bean = mList.get(position); holder.imageView.setBackgroundResource(bean.itemImage); holder.title.setText(bean.itemTitle); } @Override public int getItemCount() { return mList.size(); } //下面两个方法提供给页面刷新和加载时调用 public void add(List addMessageList) { //增加数据 int position = mList.size(); mList.addAll(position, addMessageList); notifyItemInserted(position); } public void refresh(List newList) { //刷新数据 mList.removeAll(mList); mList.addAll(newList); notifyDataSetChanged(); } }
主方法:
package com.example.administrator.recyclerviewtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private Listlist; private MyRecyclerViewAdapter myAdapte1r; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList (); for ( int i=0; i< 19;i++){ list.add(new ItemBean( R.mipmap.logo, "Hello", new Date().toString()+"" )); } myAdapte1r = new MyRecyclerViewAdapter(list); recyclerView = (RecyclerView) findViewById(R.id.message_notice_list_item); //纵向线性布局 //LinearLayoutManager layoutManager = new LinearLayoutManager(this); //纵向线性布局 GridLayoutManager layoutManager = new GridLayoutManager(this,2); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(myAdapte1r); } }
以上就是怎么在Android 应用中利用RecyclerView实现一个网格布局,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
新闻标题:怎么在Android应用中利用RecyclerView实现一个网格布局-创新互联
文章转载:http://ybzwz.com/article/dpssgp.html