简体   繁体   中英

Android left and right alignment of listview

I want to align the listview left and right inside a RelativeLayout as done in IM apps.

My Problem is that things are getting random. Sometimes when m.getme() == 0 then its msg_row_he.xml layout loaded but sometimes its msg_row_me.xml loaded. And the align of listview's are changing at every scroll. Can anyone figure out what is going on ?

msgAdapter.java (getView Method)

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 (right aligned)

<?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 (left aligned)

<?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>

You have to override getItemViewType(int position) and getViewTypeCount()

Lets say you have two types of layout (left and right) then

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

and

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

In your getView method change this condition

if(m.getme() == 0)

to

if(getItemViewType(position)==0)

Here you are have different layout for the position then you have to inflate in everytime

Change

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); 
} }

To

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM