版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中RecyclerViewUI滚动控件的示例分析
1基本用法compile
'com.android.support:recyclerview-v7:24.2.1'<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>public
class
CatAdapter
extends
RecyclerView.Adapter<CatAdapter.ViewHolder>
{
private
List<Cat>
cats;
static
class
ViewHolder
extends
RecyclerView.ViewHolder
{
ImageView
image;
TextView
name;
public
ViewHolder(View
view)
{
super(view);
image
=
(ImageView)
view.findViewById(R.id.image);
name
=
(TextView)
view.findViewById(R.);
}
}
public
CatAdapter(List<Cat>
cats)
{
this.cats
=
cats;
}
@Override
public
ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_item,
parent,
false);
return
new
ViewHolder(view);
}
@Override
public
void
onBindViewHolder(ViewHolder
holder,
int
position)
{
Cat
cat
=
cats.get(position);
holder.image.setImageResource(cat.getImageId());
.setText(cat.getName());
}
@Override
public
int
getItemCount()
{
return
cats.size();
}
}public
class
MainActivity
extends
AppCompatActivity
{
private
List<Cat>
cats
=
new
ArrayList<>();
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
RecyclerView
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
LinearLayoutManager
layoutManager=new
LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
CatAdapter
adapter=new
CatAdapter(cats);
recyclerView.setAdapter(adapter);
}
/**
*
初始化数据
*/
private
void
init()
{
cats.add(new
Cat("暹罗猫",
R.drawable.cat1));
cats.add(new
Cat("布偶猫",
R.drawable.cat2));
cats.add(new
Cat("苏格兰折耳猫",
R.drawable.cat3));
cats.add(new
Cat("英国短毛猫",
R.drawable.cat4));
cats.add(new
Cat("波斯猫",
R.drawable.cat5));
cats.add(new
Cat("俄罗斯蓝猫",
R.drawable.cat6));
cats.add(new
Cat("美国短毛猫",
R.drawable.cat7));
cats.add(new
Cat("异国短毛猫",
R.drawable.cat8));
cats.add(new
Cat("挪威森林猫",
R.drawable.cat9));
cats.add(new
Cat("孟买猫",
R.drawable.cat10));
cats.add(new
Cat("缅因猫",
R.drawable.cat11));
cats.add(new
Cat("埃及猫",
R.drawable.cat12));
}
}/upload/information/20200623/125/124108.jpg2横向滚动<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
</LinearLayout>@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
...
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
...
}/upload/information/20200623/125/124109.jpg3瀑布流布局<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
/>
</LinearLayout>@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
RecyclerView
recyclerView
=
(RecyclerView)
findViewById(R.id.recycler_view);
StaggeredGridLayoutManager
layoutManager=new
StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
CatAdapter
adapter
=
new
CatAdapter(cats);
recyclerView.setAdapter(adapter);
}/upload/information/20200623/125/124110.jpg4点击事件static
class
ViewHolder
extends
RecyclerView.ViewHolder
{
View
catView;
ImageView
image;
TextView
name;
public
ViewHolder(View
view)
{
super(view);
catView
=
view;
image
=
(ImageView)
view.findViewById(R.id.image);
name
=
(TextView)
view.findViewById(R.);
Log.d(TAG,
"ViewHolder:
image:"
+
image);
Log.d(TAG,
"ViewHolder:
name:"
+
name);
}
}
public
CatAdapter(List<Cat>
cats)
{
this.cats
=
cats;
}
@Override
public
ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_item,
parent,
false);
final
ViewHolder
holder
=
new
ViewHolder(view);
holder.catView.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
int
position
=
holder.getAdapterPosition();
Cat
cat
=
cats.get(position);
Toast.makeText(v.getContext(),
"你点击了
View
"
+
cat.getName(),
Toast.LENGTH_SHORT).show();
}
});
holder.image.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
in
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 课程设计实施过程指导【课件文档】
- 提升网络安全应急响应能力
- 飞天梦想课件
- 云计算容量规划手册
- 软件开发流程管理方法介绍
- 数据结构算法设计测试试题及真题
- 2026年统编版高中语文写作能力练习试题
- 2025年监理工程师考试报名指南试题及真题
- 商业物业设施维护与保养手册
- 企业内部控制审计评估手册
- 2026内蒙古地质矿产集团有限公司社会招聘65人备考题库附答案详解(a卷)
- 2026年常州工业职业技术学院单招综合素质考试模拟测试卷附答案解析
- 泸州白酒行业分析报告
- 蒙古族服饰概览
- django基于深度学习的旅游系统设计与实现-论文13000字
- 《采煤机》课件-第二章 采煤机截割部
- 民营企业工作作风存在的问题及整改措施
- (完整版)陆河客家请神书
- 教学大纲-跨境电子商务法律法规
- 上海市历年中考语文现代文之议论文阅读6篇(含答案)(2003-2022)
- 重症感染治疗指南
评论
0/150
提交评论