简体   繁体   English

Android的ListView左右对齐

[英]Android left and right alignment of listview

I want to align the listview left and right inside a RelativeLayout as done in IM apps. 我想像IM应用一样在RelativeLayout内左右对齐列表视图。

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. 有时,当m.getme() == 0将加载其msg_row_he.xml布局,但有时会加载其msg_row_me.xml And the align of listview's are changing at every scroll. 并且listview的对齐方式在每次滚动时都在变化。 Can anyone figure out what is going on ? 谁能知道发生了什么事?

msgAdapter.java (getView Method) 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 (right aligned) 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 (left aligned) 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>

You have to override getItemViewType(int position) and getViewTypeCount() 您必须重写getItemViewType(int position)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 在您的getView方法中更改此条件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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