版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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国际签证员面试题及答案
- 2026华为面试题及答案大全集
- 2026技能岗面试题型及答案
- 2026近期市场面试题及答案
- 2026秋新教科版科学五年级上册教学课件:第三单元 第4课 测量力的大小 有2个微课视频
- 人工智能在银行合规审查中的应用-第2篇
- 河北省张家口市2025-2026学年高一下学期7月期末语文试题(含答案)
- 人工智能在理赔中的应用-第15篇
- 2026年南京市下关区住房和城乡建设局人员招聘笔试备考题库及答案详解
- 2026年西安盛弘电气有限公司招聘(5人)考试参考题库及答案详解
- 交警培训课件 辅警
- 出口数据加密与解密技术解析
- 食品配送投诉处理管理制度
- 《羊饲养与疫病防》课件
- 美容美体艺术-大学专业介绍
- DB13-T 5931-2024 珍珠棉生产企业安全生产技术条件
- 2024年全国各地中考语文真题分类汇编【第二辑】专题09 散文阅读(含答案)
- DL-T5842-2021110kV~750kV架空输电线路铁塔基础施工工艺导则
- 医院培训课件:《小儿荨麻疹的护理查房》
- 蔬菜栽培学总论-期末考试-考研复试
- 学生缓期考试申请表(模板)
评论
0/150
提交评论