Android 列表如何呈现两边显示信息
时间: 2023-06-13 15:02:56 浏览: 56
Android 列表可以使用自定义 Adapter 实现两边显示信息的效果。在自定义 Adapter 中,可以定义两个布局文件,分别用于显示左边和右边的信息。然后在 getView() 方法中,根据不同的数据位置,选择不同的布局文件进行显示。
具体实现步骤如下:
1. 定义两个布局文件,分别用于显示左边和右边的信息。
2. 自定义 Adapter,在 getView() 方法中根据数据位置选择不同的布局文件进行显示。
3. 在布局文件中使用 LinearLayout 控件,将左边和右边的信息放置在不同的子控件中。
4. 在代码中设置 LinearLayout 控件的权重,使左边和右边的信息分别占据布局的一半。
下面是一个简单的示例代码:
```
public class MyAdapter extends BaseAdapter {
private List<String> mData;
public MyAdapter(List<String> data) {
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
if (position % 2 == 0) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_left, parent, false);
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_right, parent, false);
}
holder.textView = convertView.findViewById(R.id.text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
static class ViewHolder {
TextView textView;
}
}
```
在布局文件中,可以使用 LinearLayout 控件实现左边和右边的布局:
```
<!-- 左边的布局 -->
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left text" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left text" />
</LinearLayout>
<!-- 右边的布局 -->
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="right text" />
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="right text" />
</LinearLayout>
```
在代码中,根据数据位置选择不同的布局文件进行显示:
```
if (position % 2 == 0) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_left, parent, false);
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_right, parent, false);
}
```
最后,设置 LinearLayout 控件的权重,使左边和右边的信息分别占据布局的一半:
```
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left text" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left text" />
</LinearLayout>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)