最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
基于RecyclerView实现横向GridView效果
时间:2022-06-25 22:53:39 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了RecyclerView实现横向GridView效果展示的具体代码,供大家参考,具体内容如下
要使用RecyclerView,首先要在build.gradle文件中添加依赖compile 'com.android.support:appcompat-v7:24.1.0'
效果图
布局如下
使用方法:
package com.dxx.recycleviewtestdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView rv = (RecyclerView) findViewById(R.id.rv); rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//设置布局管理器 rv.setAdapter(new MyRVAdapter(this)); } }
其Adapter要继承RecyclerView.Adapter,在Adapter中药先定义ViewHolder,并继承RecyclerView.ViewHolder;如:
public class ViewHolder extends RecyclerView.ViewHolder{ public ViewHolder(View itemView) { super(itemView); } ImageView rv_item_image; TextView rv_item_tv; }
在onCreateViewHolder进行初始化操作,在onBindViewHolder中对各种事件进行处理,getItemCount返回的是 RecyclerView的长度,其布局与代码如下:
package com.dxx.recycleviewtestdemo; 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.ImageView; import android.widget.TextView; /** * Created by duxiaxing on 2016/7/27. */ public class MyRVAdapter extends RecyclerView.Adapter{ private Context context; public MyRVAdapter(Context context){ this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.layout_rv_item,parent,false); ViewHolder holder = new ViewHolder(view); holder.rv_item_image = (ImageView) view.findViewById(R.id.rv_item_image); holder.rv_item_tv = (TextView) view.findViewById(R.id.rv_item_tv); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.rv_item_tv.setText(position + ""); } @Override public int getItemCount() { return 9; } public class ViewHolder extends RecyclerView.ViewHolder{ public ViewHolder(View itemView) { super(itemView); } ImageView rv_item_image; TextView rv_item_tv; } }
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22