Android 如何在列表视图中为奇偶位置充气

Android 如何在列表视图中为奇偶位置充气,android,xml,android-arrayadapter,Android,Xml,Android Arrayadapter,如何在列表视图中为奇数和偶数位置膨胀视图。我想在偶数位置显示左对齐xml,在奇数位置显示右对齐xml This is my code: public View getView(final int position, View view, ViewGroup parent) { LayoutInflater inflator = context.getLayoutInflater(); if ((position % 2) == 0) { rowView

如何在列表视图中为奇数和偶数位置膨胀视图。我想在偶数位置显示左对齐xml,在奇数位置显示右对齐xml

This is my code:  

 public View getView(final int position, View view, ViewGroup parent)
{
    LayoutInflater inflator = context.getLayoutInflater();
    if ((position % 2) == 0) {

         rowView = inflator.inflate(R.layout.chatbubble, null, true);

        TextView txtv_msg;
        txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
        txtv_msg.setText(""+menus[position]);
    }
    else {
        rowView1 = inflator.inflate(R.layout.chatbubble1, null, true);

        TextView txtv_msg;
        txtv_msg = (TextView) rowView1.findViewById(R.id.message_text);
        txtv_msg.setText(""+menus[position]);

    }

    return rowView;
}
但对于所有位置,我的数据项都是左对齐的

This is my xml  :
for left :
     <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/bubble_layout_parent"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
      android:layout_margin="10dp"
       android:padding="5dp">

    <LinearLayout
     android:id="@+id/bubble_layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/bubble1">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxEms="12"
        android:layout_gravity="center"
        android:text="Hi! new message"
        android:textColor="@android:color/primary_text_light" />
   </LinearLayout>

  </LinearLayout>

  for right :
      <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/bubble_layout_parent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="10dp"
      android:padding="5dp"
      android:layout_gravity="right">

    <LinearLayout
     android:id="@+id/bubble_layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/bubble1">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxEms="12"
        android:layout_gravity="center"
        android:text="Hi! new message"
        android:textColor="@android:color/primary_text_light" />
       </LinearLayout>

       </LinearLayout>
这是我的xml:
左图:
右翼:

我是android开发新手,请帮助。

您需要
覆盖
这两种
基本适配器
方法

@Override
public int getViewTypeCount() {
  return 2;
} 


@Override
public int getItemViewType(int position) {
  //return 0 or 1 according to your view
}
getView()中

并根据
类型
对视图进行充气。
您也可以这样做。

问题在于适配器的
getView()
方法。它总是返回
rowView
,即
左对齐的XML

解决方案:

public View getView(final int position, View rowView, ViewGroup parent)
{
    LayoutInflater inflator = context.getLayoutInflater();
    if ((position % 2) == 0) {
        rowView = inflator.inflate(R.layout.chatbubble, null, true);

        TextView txtv_msg;
        txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
        txtv_msg.setText("" + menus[position]);
    }
    else {
        rowView = inflator.inflate(R.layout.chatbubble1, null, true);

        TextView txtv_msg;
        txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
        txtv_msg.setText("" + menus[position]);

    }

    return rowView;
}
  • 将适配器的
    getView()
    参数
    view
    重命名为
    rowView
  • 在您的
    else
    条件下
    充气
    layout
    R.layout.chatbulble1
    rowView
    而不是
    rowView1
    ,并将
    rowView1.findViewById()
    更改为
    rowView.findViewById()
  • 以下是完整的工作代码:

    public View getView(final int position, View rowView, ViewGroup parent)
    {
        LayoutInflater inflator = context.getLayoutInflater();
        if ((position % 2) == 0) {
            rowView = inflator.inflate(R.layout.chatbubble, null, true);
    
            TextView txtv_msg;
            txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
            txtv_msg.setText("" + menus[position]);
        }
        else {
            rowView = inflator.inflate(R.layout.chatbubble1, null, true);
    
            TextView txtv_msg;
            txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
            txtv_msg.setText("" + menus[position]);
    
        }
    
        return rowView;
    }
    

    希望这会有所帮助~

    因为您在父布局上有
    android:layout\u width=“wrap\u content”
    。尝试将其更改为
    match_parent
    ,并在气泡布局非parent上设置
    layout_gravity