Android使用tools:listitem属性使xml布局预览时可以显示istView和RecyclerView的item布局

一、问题描述
最近在Check团队成员代码的时候,发现大部分团队使用RecyclerView和ListView的布局文件,都没有很友好的展示出这个布局对应的item布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:scrollbars="none" />

</android.support.constraint.ConstraintLayout>


完全不知道这个界面即将展示出来到底是什么样的效果,如何可以使该布局有一个直观的预览效果呢?

二、tools:listitem 介绍

2.1 使用 属性 tools:itemCount 和 tools:listitem 给RecyclerView添加直观预览效果


因此我建议团队成员如果这个RecyclerView和ListView的布局内容比较简单的话,可以使用tools:listitem来指定item项对应的布局文件,给这个布局加个一个直观的预览效果,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:scrollbars="none"
        tools:itemCount="5"
        tools:listitem="@layout/item_list" />

</android.support.constraint.ConstraintLayout>


layout/item_list.xml 代码如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center_vertical">
    <!-- 英文首字母 或者显示汉字笔画数 -->
    <TextView
        android:id="@+id/sortTitle"
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:background="#f5f5f5"
        android:paddingLeft="16dp"
        android:gravity="center_vertical"
        tools:text="A"
        android:textColor="#888888"/>

    <!--地区名称-->
    <TextView
        android:id="@+id/country_region_name"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_below="@id/sortTitle"
        android:layout_marginLeft="16dp"
        android:gravity="center_vertical"
        tools:text="中国"
        android:textSize="16sp"
        android:textColor="#222222"/>

    <!-- 地区代码 -->
    <TextView
        android:id="@+id/areaCode"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_below="@id/sortTitle"
        android:layout_alignParentRight="true"
        android:layout_marginRight="50dp"
        android:gravity="center_vertical"
        tools:text="+86"
        android:textSize="16sp"
        android:textColor="#888888"/>

    <!--item分割线-->
    <View
        android:id="@+id/area_split_line"
        android:layout_below="@id/country_region_name"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginLeft="16dp"
        android:background="#dfdfdf"/>
</RelativeLayout>


上面有两个属性 tools:itemCount 和 tools:listitem,下面来介绍下这两个属性

2.2 tools:listitem 介绍


这个属性可以直接指示 RecyclerView和AdapterView的子类(比如:ListView、GridView)的item的布局文件,这样就可以在布局文件中直接有个很直观的预览效果。

这些属性不适用于Android Studio 2.2中的ListView,但这在2.3中已修复在RecyclerView中使用

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:scrollbars="none"
        tools:itemCount="5"
        tools:listitem="@layout/item_list" />

</android.support.constraint.ConstraintLayout>


在ListView中使用

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:scrollbars="none"
        tools:listitem="@layout/item_list" />

</android.support.constraint.ConstraintLayout>


2.3 tools:itemCount 介绍

对于给定的RecyclerView,此属性指定布局编辑器应在“预览”窗口中呈现的项目数。不同的数目,预览的个数不一样。

tools:itemCount 指定为5时


tools:itemCount 指定为9时

这个属性似乎在ListView上无效


2.4 tools:listheader 和 tools:listfooter 属性

RecyclerView 现在仅支持了以上两个 tools属性:tools:itemCount 和 tools:listitem

而对于AdapterView 的子类如 ListView, GridView 等, 还支持 tools:listheader 和 tools:listfooter 属性

如下所示:

layout/activity_listview.xml 代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:scrollbars="vertical"
        tools:listfooter="@layout/footer_list"
        tools:listheader="@layout/header_list"
        tools:listitem="@layout/item_list" />

</RelativeLayout>


添加了一个 Header的预览效果,指定header为layout/header_list.xml

layout/header_list.xml 代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center_vertical">
    <!-- 英文首字母 或者显示汉字笔画数 -->
    <TextView
        android:id="@+id/sortTitle"
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:background="#f5f5f5"
        android:paddingLeft="16dp"
        android:gravity="center_vertical"
        tools:text="Header Header Header Header"
        android:textColor="#888888"/>
</RelativeLayout>

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个使用RecyclerView的基本范例,它展示了一个简单的列表,列表中的每项都包含一个文本和一个图像: 1. 首先,在你的Android Studio项目中添加RecyclerView依赖: ``` implementation 'androidx.recyclerview:recyclerview:1.1.0' ``` 2. 创建一个RecyclerView布局文件和一个列表项布局文件。在这个例子中,我们将使用LinearLayoutManager来实现垂直滚动的列表。 `activity_main.xml`: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" /> ``` `list_item.xml`: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <ImageView android:id="@+id/imageView" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/ic_launcher_background" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:text="List Item" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> ``` 3. 创建一个列表项ViewHolder类,用于绑定列表项布局文件中的View。 `ListItemViewHolder.java`: ``` import android.view.View; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class ListItemViewHolder extends RecyclerView.ViewHolder { private ImageView imageView; private TextView textView; public ListItemViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.imageView); textView = itemView.findViewById(R.id.textView); } public void bind(ListItem item) { imageView.setImageResource(item.getImageResource()); textView.setText(item.getText()); } } ``` 4. 创建一个列表项数据类,用于存储每一个列表项的数据。 `ListItem.java`: ``` public class ListItem { private int imageResource; private String text; public ListItem(int imageResource, String text) { this.imageResource = imageResource; this.text = text; } public int getImageResource() { return imageResource; } public String getText() { return text; } } ``` 5. 创建一个RecyclerView适配器类,用于绑定列表项数据和ViewHolder。 `ListAdapter.java`: ``` import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class ListAdapter extends RecyclerView.Adapter<ListItemViewHolder> { private List<ListItem> listItems; public ListAdapter(List<ListItem> listItems) { this.listItems = listItems; } @NonNull @Override public ListItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item, parent, false); return new ListItemViewHolder(view); } @Override public void onBindViewHolder(@NonNull ListItemViewHolder holder, int position) { holder.bind(listItems.get(position)); } @Override public int getItemCount() { return listItems.size(); } } ``` 6. 在MainActivity中使用RecyclerView并设置适配器。 `MainActivity.java`: ``` import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); List<ListItem> listItems = new ArrayList<>(); listItems.add(new ListItem(R.drawable.ic_launcher_background, "List Item 1")); listItems.add(new ListItem(R.drawable.ic_launcher_background, "List Item 2")); listItems.add(new ListItem(R.drawable.ic_launcher_background, "List Item 3")); ListAdapter adapter = new ListAdapter(listItems); recyclerView.setAdapter(adapter); } } ``` 这样就完成了一个简单的使用RecyclerView的列表。当你运行这个应用程序,你应该可以看到一个包含三个列表项的列表,每一个列表项都包含一个图像和一个文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值