2015-02-28 3 views
0

Я хочу выровнять список влево и вправо внутри RelativeLayout, как это сделано в приложениях IM.Ориентация списка влево и вправо списка

Моя проблема в том, что вещи становятся случайными. Иногда, когда m.getme() == 0, тогда загружался его макет msg_row_he.xml, но иногда загружался его msg_row_me.xml. И выравнивание listview меняется при каждом прокрутке. Кто-нибудь может понять, что происходит?

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> 
+0

Попробуйте это - http://javapapers.com/android/android-chat-bubble/ –

ответ

1

Вы должны переопределить 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) 
+0

будет ли это влиять на производительность? –

+0

это не будет, так как ваш переработанный вид всегда будет использоваться – dudego

+0

спасибо, что он работает –

0

Здесь вы имеете другую раскладку для позиции, то вы должны раздуть в каждый раз

Изменение

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

, не используя convertView == null, макет будет накачаться каждый раз, и производительность, следовательно, будет затронута ... – dudego

+0

его работы могут повлиять performace –