Android 如何在列表视图中为奇偶位置充气
如何在列表视图中为奇数和偶数位置膨胀视图。我想在偶数位置显示左对齐xml,在奇数位置显示右对齐xmlAndroid 如何在列表视图中为奇偶位置充气,android,xml,android-arrayadapter,Android,Xml,Android Arrayadapter,如何在列表视图中为奇数和偶数位置膨胀视图。我想在偶数位置显示左对齐xml,在奇数位置显示右对齐xml This is my code: public View getView(final int position, View view, ViewGroup parent) { LayoutInflater inflator = context.getLayoutInflater(); if ((position % 2) == 0) { rowView
This is my code:
public View getView(final int position, View view, ViewGroup parent)
{
LayoutInflater inflator = context.getLayoutInflater();
if ((position % 2) == 0) {
rowView = inflator.inflate(R.layout.chatbubble, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
txtv_msg.setText(""+menus[position]);
}
else {
rowView1 = inflator.inflate(R.layout.chatbubble1, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView1.findViewById(R.id.message_text);
txtv_msg.setText(""+menus[position]);
}
return rowView;
}
但对于所有位置,我的数据项都是左对齐的
This is my xml :
for left :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bubble_layout_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp">
<LinearLayout
android:id="@+id/bubble_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble1">
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="12"
android:layout_gravity="center"
android:text="Hi! new message"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
for right :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bubble_layout_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp"
android:layout_gravity="right">
<LinearLayout
android:id="@+id/bubble_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble1">
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="12"
android:layout_gravity="center"
android:text="Hi! new message"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
这是我的xml:
左图:
右翼:
我是android开发新手,请帮助。您需要
覆盖这两种基本适配器方法
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//return 0 or 1 according to your view
}
在getView()中
并根据类型
对视图进行充气。
您也可以这样做。问题在于适配器的getView()
方法。它总是返回rowView
,即左对齐的XML
解决方案:
public View getView(final int position, View rowView, ViewGroup parent)
{
LayoutInflater inflator = context.getLayoutInflater();
if ((position % 2) == 0) {
rowView = inflator.inflate(R.layout.chatbubble, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
txtv_msg.setText("" + menus[position]);
}
else {
rowView = inflator.inflate(R.layout.chatbubble1, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
txtv_msg.setText("" + menus[position]);
}
return rowView;
}
将适配器的getView()
参数view
重命名为rowView
在您的else
条件下充气layoutR.layout.chatbulble1
到rowView
而不是rowView1
,并将rowView1.findViewById()
更改为rowView.findViewById()
以下是完整的工作代码:
public View getView(final int position, View rowView, ViewGroup parent)
{
LayoutInflater inflator = context.getLayoutInflater();
if ((position % 2) == 0) {
rowView = inflator.inflate(R.layout.chatbubble, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
txtv_msg.setText("" + menus[position]);
}
else {
rowView = inflator.inflate(R.layout.chatbubble1, null, true);
TextView txtv_msg;
txtv_msg = (TextView) rowView.findViewById(R.id.message_text);
txtv_msg.setText("" + menus[position]);
}
return rowView;
}
希望这会有所帮助~因为您在父布局上有android:layout\u width=“wrap\u content”
。尝试将其更改为match_parent
,并在气泡布局非parent上设置layout_gravity
。