如何在列表视图中为奇数和偶数位置膨胀视图
How to inflate view in list view for odd and even position
如何在列表视图中为奇数和偶数膨胀视图position.I想在偶数位置显示左对齐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 = 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>
我是 android 开发的新手,请帮忙。
你需要override
这两个base adapter
方法
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//return 0 or 1 according to your view
}
并在 getView()
int type = getItemViewType(position);
并根据 type
膨胀你的观点。
你也可以按照这个简化 example。
问题出在您的适配器的 getView()
方法中。它总是 returns rowView
即 left aligned XML
。
解法:
- 将适配器的
getView()
参数 view
重命名为 rowView
。
- 在你的
else
条件 inflate
布局 R.layout.chatbubble1
到 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;
}
希望对你有所帮助~
如何在列表视图中为奇数和偶数膨胀视图position.I想在偶数位置显示左对齐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 = 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>
我是 android 开发的新手,请帮忙。
你需要override
这两个base adapter
方法
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//return 0 or 1 according to your view
}
并在 getView()
int type = getItemViewType(position);
并根据 type
膨胀你的观点。
你也可以按照这个简化 example。
问题出在您的适配器的 getView()
方法中。它总是 returns rowView
即 left aligned XML
。
解法:
- 将适配器的
getView()
参数view
重命名为rowView
。 - 在你的
else
条件inflate
布局R.layout.chatbubble1
到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;
}
希望对你有所帮助~