Android中怎么利用RecyclerView实现数据列表展示效果,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
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实现数据列表展示效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。