当前位置:网站首页>横向listview的最佳实现——RecycleView
横向listview的最佳实现——RecycleView
2022-07-28 16:38:00 【两块三刀】
转载注明出处:http://blog.csdn.net/sange77/article/details/73753037
前些天,要写一个横向的列表,想着是将平时竖向的listview 横着来弄,挺麻烦的。后来,发现RecycleView这货就可以实现,体验效果也是很好!
##看效果
##上代码
- gradle 中引用
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
- Activity
public class CustomViewActivity extends Activity {
private RecyclerView recyclerView; private MyRecyclerViewAdapter mAdapter = new MyRecyclerViewAdapter(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_view); initView(); } private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.rv_custom_view); recyclerView.setAdapter(mAdapter); LinearLayoutManager llm = new LinearLayoutManager(this); llm.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置为横向的布局 recyclerView.setLayoutManager(llm); recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));// 设置分割线 } 注意:LinearLayoutManager.HORIZONTAL要和DividerItemDecoration.HORIZONTAL保持一直,才会有正常的分割线效果
- Activity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal">
<android.support.v7.widget.RecyclerView android:id="@+id/rv_custom_view" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
- MyRecyclerViewAdapter
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) {
} @Override public int getItemCount() {
return 9; } class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView); } } } - item_recyclerview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试文字" android:textSize="18sp" android:textStyle="bold"/>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是详情内容....."/>
</LinearLayout>
**item 要不要适应全屏 控制大小, 主要改item_recyclerview.xml中根布局的android:layout_width和android:layout_height **
ok, 就这么多!
转载注明出处:http://blog.csdn.net/sange77/article/details/73753037
边栏推荐
猜你喜欢
![[advanced C language] - analyze the storage of micro data in memory [i]](/img/6a/ac723cee2543cd2403a7e58d556c8e.png)
[advanced C language] - analyze the storage of micro data in memory [i]

How to install PS filter plug-in

uniapp本地打包权限配置问题

Idea error running 'application' command line is too long solution

Openpcd installation process record

3D point cloud processing series - ---- PCA

Collection集合

Understanding of virtual (virtual method) in C and its difference from abstract (abstract method)

2.2- data type

从0到1:基于云开发的投票小程序开发笔记
随机推荐
Branch and loop statements
【无标题】
Compilation principle learning notes 2 (Introduction to syntax analysis)
3.2- random numbers
mysql5.7压缩包安装教程
Mmdetection3d (3) -- network output
其他电脑连接本地mysql
视频号小白起号操作指南
Tensorflow2.0 (XII) -- realize simple RNN and LSTM networks
Electrotechnics self study notes 1.20
centos8按照docker官网创wordpress+mysql报错解决
Electrotechnics digital circuit self study notes 1.24
【p5.js】实战临摹——国际象棋盘
[p5.js learning notes] mouse interaction event
概率函数P(x)、概率分布函数F(x)与概率密度函数f(x)的区别
wordpress提示建立数据库连接时出错
QT programming serial port assistant
es6 Promise
[p5.js] actual copy - chess board
Point cloud processing - KD tree