




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 借款投资合作合同范本
- 公司厂房抵押合同范本
- ktv经营合同范本
- 与商户合同范本
- 亲戚之间租车合同范本
- 劳动合同范本 日语
- 2024年重庆市荣昌区人民医院招聘笔试真题
- 中国监理合同范本
- 中山餐饮合同范本
- 2024年河源市紫金县蓝塘镇招聘考试真题
- 2025寒假开学第一课 课件【1】
- 杭州湾跨海大桥项目案例ppt课件
- (完整版)光荣榜25张模板
- 机电预留预埋工程施工组织设计方案
- 工业催化剂作用原理—金属氧化物催化剂
- 2022年三八妇女节妇女权益保障法律知识竞赛题库及答案(共290题)
- 优秀教材推荐意见(真实的专家意见)
- Of studies原文译文及赏析
- 安全阀基本知识讲义
- QTD01钢质焊接气瓶检验工艺指导书
- 辛弃疾生平简介(课堂PPT)
评论
0/150
提交评论