Android listview的左右对齐

Android left and right alignment of listview

我想像在 IM 应用程序中那样在 RelativeLayout 中左右对齐列表视图。

我的问题是事情变得随机了。有时 m.getme() == 0 然后它的 msg_row_he.xml 布局加载但有时它的 msg_row_me.xml 加载。列表视图的对齐方式在每次滚动时都在变化。任何人都可以弄清楚发生了什么事吗?

msgAdapter.java (getView方法)

public View getView(int position, View convertView, ViewGroup parent) {


    // getting data for the row
    MSList m = listItems.get(position);
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null){
        if(m.getme() == 0){
            convertView = inflater.inflate(R.layout.msg_row_he, null);
        }else{
            convertView = inflater.inflate(R.layout.msg_row_me, null);
        }
    }
    ImageView pp = (ImageView) convertView
            .findViewById(R.id.ind_pp);
    TextView msgcon = (TextView) convertView.findViewById(R.id.ind_msgcon);
    TextView msgtime = (TextView) convertView.findViewById(R.id.ind_msgtime);


    // thumbnail image
     new ImageLoadTask(m.getppUrl(),pp).execute();

    // content
     msgcon.setText(m.getmsgcon());

    // time
     msgtime.setText(String.valueOf(m.getmsgtime()));


    convertView.setTag(m);
    return convertView;
}

msg_row_me.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="match_parent"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >    



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal" >
            <!-- Content -->
            <TextView
                android:id="@+id/ind_msgcon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="#fbfbfb"
                android:textColor="@color/msgcon"
                android:textSize="@dimen/msgcon" />

            <!-- Thumbnail Image -->
            <ImageView
                android:id="@+id/ind_pp"
                android:layout_width="45dp"
                android:layout_height="45dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >
    <!-- Message Time -->
        <TextView
            android:id="@+id/ind_msgtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/msgtime"
            android:textSize="@dimen/msgtime" />
    </LinearLayout>

</LinearLayout>
</RelativeLayout>

msg_row_he.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="match_parent"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >



<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal" >
    <!-- Thumbnail Image -->
        <ImageView
            android:id="@+id/ind_pp"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginRight="8dp" />

    <!-- Content -->

        <TextView
            android:id="@+id/ind_msgcon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="#fbfbfb"
            android:textColor="@color/msgcon"
            android:textSize="@dimen/msgcon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >    
    <!-- Message Time -->
        <TextView
            android:id="@+id/ind_msgtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/msgtime"
            android:textSize="@dimen/msgtime" />
    </LinearLayout>

</LinearLayout>

</RelativeLayout>

在这里你有不同的位置布局然后你必须每次充气

改变

if (convertView == null){
 if(m.getme() == 0){
 convertView = inflater.inflate(R.layout.msg_row_he, null); 
}else{
 convertView = inflater.inflate(R.layout.msg_row_me, null); 
} }

 if(m.getme() == 0){ 
convertView = inflater.inflate(R.layout.msg_row_he, null); 
}else{
 convertView = inflater.inflate(R.layout.msg_row_me, null);
 }

您必须覆盖 getItemViewType(int position)getViewTypeCount()

假设您有两种布局(左布局和右布局),那么

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

@Override
    public int getItemViewType(int position) {
      return listItems.get(position).getme(); // hoping this returns value as 0 and 1   
    }

在您的 getView 方法中更改此条件

if(m.getme() == 0)

if(getItemViewType(position)==0)