[英]Android left and right alignment of listview
我想像IM应用一样在RelativeLayout
内左右对齐列表视图。
我的问题是事情变得随机了。 有时,当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>
您必须重写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)
在这里,您的位置有不同的布局,那么您每次都要充气
更改
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.