我正在阅读本教程http://android.amberfog.com/?p=296。我想创建一个不同类型的行的Listview。我理解如何创建适配器,但xml布局呢?所以我定义了像这样的xml布局:
<ListView/>
<TextView android:id="@+id/id1" />
<TextView android:id="@+id/id2" />
<ImageView android:id="@+id/id3" />
<TextView android:id="@+id/id4" />
如果一行只使用布局的一些元素(仅一些文本视图)而另一行可能使用其他元素,那么它是否会出现问题(性能)?我不明白我的是否是定义xml的正确方法,或者我是否必须为每种类型的行创建不同的布局。
提前谢谢
编辑:现在我有一个零点异常。
来自适配器的java代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listview_main, null);
holder.textView_title = (TextView)convertView.findViewById(R.id.listview1);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
**holder.textView_title.setText("aaaa");** //NULL POINT EXCEPTION HERE
return convertView;
}
class ViewHolder {
public TextView textView_title;
}
xml 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="0dp">
<!-- android:background="#0094ff" -->
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true"
android:scrollbarStyle="insideInset"
android:textFilterEnabled="false"
android:divider="@null"
android:layout_margin="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:paddingLeft="15dp"
android:paddingRight="22dp"/>
</LinearLayout>
XML2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="0dp">
<TextView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:textSize="18sp"
android:textColor="#000000"
android:lines="1">
</TextView>
</LinearLayout>
答案 0 :(得分:13)
您需要覆盖getViewItemType
和getViewTypeCount
。您还需要具有自定义布局。
getItemViewType(int position)
- 根据位置返回您应该使用的布局类型的信息。
您应该查看链接中的视频。
http://www.youtube.com/watch?v=wDBM6wVEO70
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;
然后
int type;
@Override
public int getItemViewType(int position) {
if (position== 0){
type = TYPE_ITEM1;
} else if (position == 1){
type = TYPE_ITEM2;
}
else
{
type= TYPE_ITEM3 ;
}
return type;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
// instead of if else you can use a case
if (row == null) {
if (type == FIRST_TYPE) {
//infalte layout of type1
}
if (type == SECOND_TYPE) {
//infalte layout of type2
} else {
//infalte layout of normaltype
}
}
答案 1 :(得分:3)
@Override
public int getItemViewType(int position) {
if (position % 4 == 0) {
return 0;
} else if (position % 4 == 1) {
return 1;
} else if (position % 4 == 2) {
return 2;
}
return 3;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case 0: {
convertView = mInflater.inflate(R.layout.element1, null);
break;
}
case 1: {
convertView = mInflater.inflate(R.layout.element2, null);
break;
}
case 2: {
convertView = mInflater.inflate(R.layout.element3, null);
break;
}
case 3: {
convertView = mInflater.inflate(R.layout.element4, null);
break;
}
}
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById(R.id.text1);
holder.txt2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String rowItem = null;
rowItem = rowItems[position];
holder.txt1.setText(rowItem);
rowItem = rowItems[position+1];
holder.txt1.setText(rowItem);
return convertView;
}
private class ViewHolder {
TextView txt1, txt2;
}
答案 2 :(得分:2)
这可能是一种错误的方法。如果ListView中只有一个组件,那么使用简单的适配器,否则使用带有单独XML的自定义适配器作为列表行。
示例代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listhistory);
initcomponents();
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
for (int i = 1; i < 20; i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put("date", "" + i + "/13");
hmap.put("restaurant", "Restaurant" + i);
hmap.put("distance", "" + (i * 100) + "kms");
alist.add(hmap);
}
final CustomListAdapter adapter = new CustomListAdapter(this,
R.layout.listitemhistory, alist);
list.setAdapter(adapter);
}
private void initcomponents() {
list = (ListView) findViewById(R.id.history_lst_list);
}
public void backButtonClick(View v) {
finish();
}
class CustomListAdapter extends ArrayAdapter<HashMap<String, String>> {
Context context;
int textViewResourceId;
ArrayList<HashMap<String, String>> alist;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> alist) {
super(context, textViewResourceId);
this.context = context;
this.alist = alist;
this.textViewResourceId = textViewResourceId;
}
public int getCount() {
return alist.size();
}
public View getView(int pos, View convertView, ViewGroup parent) {
Holder holder = null;
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
convertView = inflater.inflate(R.layout.listitemhistory,
parent, false);
holder = new Holder();
holder.date = (TextView) convertView
.findViewById(R.id.listitemhistory_txt_date);
holder.restaurant = (TextView) convertView
.findViewById(R.id.listitemhistory_txt_restaurant);
holder.distance = (TextView) convertView
.findViewById(R.id.listitemhistory_txt_distance);
holder.lin_background = (LinearLayout) convertView
.findViewById(R.id.history_lin_background);
convertView.setTag(holder);
holder = (Holder) convertView.getTag();
holder.date.setText(alist.get(pos).get("date"));
holder.restaurant.setText(alist.get(pos).get("restaurant"));
holder.distance.setText(alist.get(pos).get("distance"));
return convertView;
}
class Holder {
TextView date, restaurant, distance;
LinearLayout lin_background;
}
}
答案 3 :(得分:0)
这不是问题。但我认为你的教程错了。
您需要为行定义布局。
如果您希望您的行能够包含文本或图像,则需要将两个视图添加到布局中。
现在在您的适配器中,您填写列表,您决定要设置哪个项目。
所以假设你有两个字符串和一个图像。
因此,您设置前两行的文本,并将图像添加到第三行。你不必为自己预定义每一行,因为我猜你正在尝试做什么。