这里写目录标题
- 常用控件
- TextView
- activity_main.xml
- Button
- activity_main.xml
- MainActivity.java
- EditText
- activity_main.xml
- MainActivity.java
- ImageView
- activity_main.xml
- MainActivity.java
- ProgessBar
- activity_main.xml
- MainActivity.java
- AlertDialog
- MainActivity.java
- ProgressDialog
- MainActivity.java
- 4种基本布局
- 线性布局:LinearLayout
- activity_layout.xml
- 相对布局:RelativeLayout
- 相对父布局定位
- acyivity_main.xml
- 相对控件进行定位
- acyivity_main.xml
- 其他
- 帧布局:FlameLayout
- activity_main.xml
- 自定义控件
- 引入布局
- title.xml
- activity_main.xml
- MainActivity.java
- 创建自定义控件
- TitleLayout.java
- activity_main.xml
- android:layout_gravity和android:gravity的区别
- padding与layout_margin的区别
- ListView
- ListView简单用法
- MainActivity.java
- activity_main.java
- 定制ListView界面
- Fruitl类
- fruit_item.xml
- FruitAdapter.java
- MainActivity.java
- 提升ListView的运行效率
- FruitAdapter.java
- Listview点击事件
- MainActivity.java
- RecyclerView
- 基本用法
- activity_main.xml
- FruitAdapter.java
- MainActivity.java
- 横向滚动
- fruit_item.xml
- MainActivity.java
- 瀑布流布局+点击事件
- fruit_item.xml
- MainaActivity.java
- FruitAdapter.java
常用控件
TextView
设置文字长宽、对齐、大小、颜色
-
android:gravity
:指定文字的对齐方式,可选值有top、 bottom、left、right.center等,可以用“”来同时指定多个值,这里我们指定的center
,效果等同于center.vertical|center_horizontal,表示文字在垂直和水平方向都居中对齐。 -
android:textSize
:设置文字大小。以sp作为单位 -
android:textColor
:设置颜色
activity_main.xml
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#E91E63"
android:text="This is TextView" />
Button
设置按钮
android:textAllCaps="false":
由于系统会对Button中的所有英文字母自动进行大写转换,这样改可以避免
activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false"/>
MainActivity.java
用实现接口的方式注册监听器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this); //实现接口方式注册
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
//Toast语句
Toast.makeText(MainActivity.this, "你点击了Button", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
EditText
activity_main.xml
文字输入
-
android:hint="在此输入"
:指定一段提示性文本。这是一个人性化的设置,输入信息前显示在框内,一旦输入信息便消失 -
android:maxLines
:设置最长行数,避免文本过长导致的界面不美观
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="在此输入"
android:maxLines="2"/>
MainActivity.java
结合Button完成一些功能:点击按钮获取EditText中输入的内容。
首先通过findViewById()
方法得到EditText的实例,然后在按钮的点击事件里调用EditText的getText()
方法获取到输入的内容,再调用toString()
方法转换成字符串,最后还是老方法,使用Toast将输入的内容显示出来。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.edit_text);
button.setOnClickListener(this); //实现接口方式注册
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
//点击显示文本信息
String inputText = editText.getText().toString();
Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
ImageView
放置图片
activity_main.xml
-
android:src
给ImageView指定一张图片。由于图片的宽和高都是未知的,所以将ImageView的宽和高都设定为wrap_content,这样就保证了不管图片的尺寸是多少,图片都可以完整地展示出来。
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img_1"/>
MainActivity.java
结合Button完成一些功能:点击按钮改变图片
private ImageView imageView;
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
//动态更改图片
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
ProgessBar
显示进度条,表示正在加载一些东西
activity_main.xml
圆形进度条
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
水平进度条+进度条最大值
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"/>
MainActivity.java
- 在按钮的点击事件中,我们通过
getVisibility()
方法来判断ProgressBar是否可见,如果可见就将ProgressBar隐藏掉,如果不可见就将ProgressBar显示出来。 - Android控件的可见属性。visible、invisible和 gone。visible表示控件是可见的,这个值是默认值,不指定android:visibility时,控件都是可见的。invisible表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了。gone 则表示控件不仅不可见,而且不再占用任何屏幕空间。
- 我们还可以通过代码来设置控件的可见性,使用的是
setVisibility()
方法,可以传入View.VISIBLE
、View.INVISIBLE
和View.GONE
这3种值。
case R.id.button:
//显示进度条
if(progressBar.getVisibility() == View.GONE){
progressBar.setVisibility(View.VISIBLE);
}else{
progressBar.setVisibility(View.GONE);
}
break;
更改进度条进度
case R.id.button:
//动态更改进度条
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);
break;
AlertDialog
弹出对话框,防止误删重要信息
MainActivity.java
首先通过AlertDialog.Builder创建一个AlertDialog
的实例,然后可以为这个对话框设置标题、内容、可否取消等属性,接下来调用setPositiveButton()
方法为对话框设置确定按钮的点击事件,调用setNegativeButton()
方法设置取消按钮的点击事件,最后调用show()
方法将对话框显示出来。
switch (view.getId()){
case R.id.button:
//弹出对话框
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setCancelable(false);
dialog.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.show();
break;
default:
break;
}
ProgressDialog
弹出对话框,显示进度条
MainActivity.java
注意,如果在setCancelable()
中传人了false,表示 ProgressDialog是不能通过Back键取消掉的,当数据加载完成后必须要调用ProgressDialog的dismiss()
方法来关闭对话框,否则ProgressDialog将会一直存在。
switch (view.getId()){
case R.id.button:
//弹出对话框,显示进度条
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default:
break;
}
4种基本布局
线性布局:LinearLayout
activity_layout.xml
android:orientation
:"horizontal"水平,"vertical"竖直
- 注意:如果是横向排列,这内部的控件就绝对不能将宽度指定为match_parent,因为这样的话,单独一个控件就会将整个水平方向占满,其他的控件就没有可放置的位置了。同样的道理,如果是纵向排列,内部的控件就不能将高度指定为match_parent。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</LinearLayout>
android:layout_gravity
:指定控件在布局中的对齐方式。
- 注意:横向分布时,只有垂直方向的对齐方式才生效;同理纵向分布时,水平方向的对齐方式才生效
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="按钮1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="按钮2"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="按钮3"/>
</LinearLayout>
android:layout_weight
:使用比例控制控件大小。此时控件的宽度不再由android:layout_width
控制,所以值可以设置为"0dp"。android:layout_weight中的值会相加得出总值再按各自的比例划分。也可以将一个设为wrap_content,另一个就是剩余空间中的满屏。
- 注意:哪个写在前,哪个就显示在前。所以要让text显示在Button前面就要写在前面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="说点啥吧"/>
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />
</LinearLayout>
相对布局:RelativeLayout
相对父布局定位
android:layout_alignParentTop
:上android:layout_centerInParent
:中android:layout_alignParentBottom
:下android:layout_alignParentLeft
:左android:layout_alignParentRight
:右
acyivity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button2 "/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button3" />
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button5" />
</RelativeLayout>
相对控件进行定位
android:layout_above
:上android:layout_below
:下android:layout_toLeftOf
:左android:layout_toRightOf
:右
acyivity_main.xml
包围Btton3:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="Button1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="Button2 "/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button3" />
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="Button4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="Button5" />
</RelativeLayout>
其他
android:layout_alignLeft
:左边缘与另一个控件左边缘对齐android:layout_alignParentRight
:右边缘与另一个控件右边缘对齐android:layout_alignParentTop
:上对齐上android:layout_alignBottom
:下对齐下
帧布局:FlameLayout
所有控件默认摆在左上角
activity_main.xml
- 即使没有任何照片,也能用
@mipmap
访问ic_launcher这张图 -
android:layout_gravity
:指定控件在布局中的对齐方式
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:hint="This is a TextView"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
自定义控件
一般我们的程序中可能有很多个活动都需要这样的标题栏,如果在每个活动的布局中都编写一遍同样的标题栏代码,明显就会导致代码的大量重复。这个时候我们就可以使用引人布局的方式来解决这个问题,新建一个布局title.xml。
引入布局
title.xml
android:layout_margin
:指定控件在上下左右方向上偏移的距离,当然也可以使用android:layout_marginLeft
或android:layout _marginTop
等属性来单独指定控件在某个方向上偏移的距离。
- 出现修改不了Button颜色,参考:android中button修改不了背景颜色
- 注意:图片不要放太大的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/back_bg">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_back"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#EFCECE"
android:text="Back"
android:textColor="#BC3131"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title_text"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_edit"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back"
android:text="Edit"
android:textColor="#fff"/>
</LinearLayout>
activity_main.xml
include语句
:将标题栏布局引入进来
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/title"/>
</LinearLayout>
MainActivity.java
getSupportActionBar()
:获得ActionBar实例,调用hidn()
将标题栏隐藏起来
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.hide();
}
}
创建自定义控件
- 重写LinearLayout 中带有两个参数的构造函数,在布局中引入
TitleLayout
控件就会调用这个构造函数。 - 在构造函数中需要对标题栏布局进行动态加载,这就要借助
LayoutInflater
来实现了。通过LayoutInflater的from()
方法可以构建出一个LayoutInflater对象,然后调用inflate()
方法就可以动态加载一个布局文件,inflate()方法接收两个参数:
- 第一个参数是要加载的布局文件的id,这里我们传入R.layout.title,
- 第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为TitleLayout,于是直接传入this。
- 编辑点击事件
TitleLayout.java
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context);
LayoutInflater.from(context).inflate(R.layout.title,this);
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleView = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});
titleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"你点击了Edit",Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_gravity和android:gravity的区别
android:gravity
针对控件里的元素来说android:layout_gravity
针对控件本身而言
padding与layout_margin的区别
android:layout_margin
:设置view的上下左右边框的额外空间android:padding
:设置内容相对view的边框的距离
ListView
ListView简单用法
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Peach","Pineapple","Strawberry","Cherry","Peach"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
activity_main.java
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
定制ListView界面
Fruitl类
public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name = name;
this.imageId = imageId;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
}
fruit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
</LinearLayout>
FruitAdapter.java
自定义适配器
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position); //获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
public FruitAdapter(@NonNull Context context, int resource, @NonNull List<Fruit> objects) {
super(context, resource, objects);
resourceId = resource;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定制ListView
initFruits(); //初始化水果数据
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for(int i = 0;i < 2; i++){
Fruit apple = new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);
Fruit orange = new Fruit("Orange",R.drawable.orange);
fruitList.add(orange);
Fruit banana = new Fruit("Banana",R.drawable.banana);
fruitList.add(banana);
Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear",R.drawable.pear);
fruitList.add(pear);
Fruit grape = new Fruit("Grape",R.drawable.grape);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple);
fruitList.add(pineapple);
Fruit cherry = new Fruit("Cherry",R.drawable.cherry);
fruitList.add(cherry);
Fruit peach = new Fruit("Peach",R.drawable.peach);
fruitList.add(peach);
}
}
}
提升ListView的运行效率
FruitAdapter.java
-
getView()
中的conbertView
:用于将之前加载好的项目进行缓存,以便之后进行重用 -
ViewHolder
:对控件的实例进行缓存
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position); //获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ViewHolder viewHolder;
//快速滚动
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView)view.findViewById(R.id.fruit_name);
view.setTag(viewHolder); //将ViewHolder存入View
}else{
view = convertView;
viewHolder = (ViewHolder) view.getTag(); //重新获取ViewHolder
//提升运行效率
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
public FruitAdapter(@NonNull Context context, int resource, @NonNull List<Fruit> objects) {
super(context, resource, objects);
resourceId = resource;
}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
Listview点击事件
MainActivity.java
initFruits(); //初始化水果数据
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fruit fruit = fruitList.get(i);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
RecyclerView
基本用法
activity_main.xml
直接调用androidx.recyclerview.widget.RecyclerView即可
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
FruitAdapter.java
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList = fruitList;
}
@Override
public FruitAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mFruitList.size();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); //初始化水果数据
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for(int i = 0;i < 2; i++){
Fruit apple = new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);
Fruit orange = new Fruit("Orange",R.drawable.orange);
fruitList.add(orange);
Fruit banana = new Fruit("Banana",R.drawable.banana);
fruitList.add(banana);
Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear",R.drawable.pear);
fruitList.add(pear);
Fruit grape = new Fruit("Grape",R.drawable.grape);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple);
fruitList.add(pineapple);
Fruit cherry = new Fruit("Cherry",R.drawable.cherry);
fruitList.add(cherry);
Fruit peach = new Fruit("Peach",R.drawable.peach);
fruitList.add(peach);
}
}
}
横向滚动
fruit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
</LinearLayout>
MainActivity.java
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); //让布局横向排列
recyclerView.setLayoutManager(layoutManager);
瀑布流布局+点击事件
fruit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"/>
</LinearLayout>
MainaActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); //初始化水果数据
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for(int i = 0;i < 2; i++){
Fruit apple = new Fruit(getRandomLengthName("Apple"),R.drawable.apple);
fruitList.add(apple);
Fruit orange = new Fruit(getRandomLengthName("Orange"),R.drawable.orange);
fruitList.add(orange);
Fruit banana = new Fruit(getRandomLengthName("Banana"),R.drawable.banana);
fruitList.add(banana);
Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"),R.drawable.watermelon);
fruitList.add(watermelon);
Fruit pear = new Fruit(getRandomLengthName("Pear"),R.drawable.pear);
fruitList.add(pear);
Fruit grape = new Fruit(getRandomLengthName("Grape"),R.drawable.grape);
fruitList.add(grape);
Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"),R.drawable.pineapple);
fruitList.add(pineapple);
Fruit cherry = new Fruit(getRandomLengthName("Cherry"),R.drawable.cherry);
fruitList.add(cherry);
Fruit peach = new Fruit(getRandomLengthName("Peach"),R.drawable.peach);
fruitList.add(peach);
}
}
private String getRandomLengthName(String name) {
Random random = new Random();
int length = random.nextInt(20) + 1; //1-20随机数
StringBuilder builder = new StringBuilder();
for(int i = 0;i <length; i++){
builder.append(name);
}
return builder.toString();
}
}
FruitAdapter.java
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
View fruitView;
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View itemView) {
super(itemView);
fruitView = itemView;
fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList = fruitList;
}
@Override
public FruitAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
final ViewHolder holder = new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(view.getContext(),"you clicked View"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(view.getContext(),"you clicked Image"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return holder;
}