安卓:与不同的项目列表视图XML布局 [英] Android: xml layout for a listview with different items

查看:231
本文介绍了安卓:与不同的项目列表视图XML布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读本教程 http://android.amberfog.com/?p=296 。我想创建一个列表视图weith不同类型的行。我知道如何创建适配器,但对于XML的布局?所以我definire像这样的一个XML布局:

 < ListView控件/>

< TextView的机器人:ID =@ + ID / ID1/>

< TextView的机器人:ID =@ + ID / ID2/>

< ImageView的机器人:ID =@ + ID / ID3/>

< TextView的机器人:ID =@ + ID / ID4/>
 

会不会是(性能),如果可能一排使用的布局只是一些元素(只有一些textviews)和另一行可能使用的其他元素的问题?我不明白,如果我的是正确的方式来定义XML或者如果我要创建不同的布局为每种类型的行。

感谢你在前进

编辑:现在我有一个零点异常

从适配器的Java code:

  @覆盖

公共查看getView(INT位置,查看convertView,ViewGroup中父){

ViewHolder支架=无效;
整型= getItemViewType(位置);

如果(convertView == NULL){
    持有人=新ViewHolder();

    convertView = mInflater.inflate(R.layout.listview_main,NULL);
    holder.textView_title =(TextView中)convertView.findViewById(R.id.listview1);

    convertView.setTag(保持器);
} 其他 {
    支架=(ViewHolder)convertView.getTag();
}

** holder.textView_title.setText(AAAA); // **零点例外这里

返回convertView;
 

}

 类ViewHolder {
    公众的TextView textView_title;
}
 

XML 1:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:ID =@ + ID / main_layout
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:方向=垂直
              机器人:重力=左
              机器人:layout_margin =0dp>

    <  - 安卓!后台=#0094ff - >

    <的ListView
            机器人:ID =@ ID / Android的:清单
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:fastScrollEnabled =真
            机器人:scrollbarStyle =insideInset
            机器人:textFilterEnabled =假
            机器人:分隔=@空
            机器人:layout_margin =0dp
            机器人:paddingTop =0dp
            机器人:paddingBottom会=0dp
            机器人:以下属性来=15dp
            机器人:paddingRight =22dp/>



< / LinearLayout中>
 

XML2

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:重力=左
    机器人:layout_margin =0dp>

    <的TextView
        机器人:ID =@ + ID / ListView1的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingTop =7DP
        机器人:paddingBottom会=0dp
        机器人:以下属性来=0dp
        机器人:paddingRight =0dp
        机器人:TEXTSIZE =18sp
        机器人:文字颜色=#000000
        机器人:行=1>
    < / TextView的>

< / LinearLayout中>
 

解决方案

您需要重写 getViewItemType getViewTypeCount 。您还需要有自定义布局。

getItemViewType(INT位置) - 返回一个布局类型,您应该使用基于位置信息

您应该看看在链接的视频。

http://www.youtube.com/watch?v=wDBM6wVEO70

 私有静态最终诠释TYPE_ITEM1 = 0;
私有静态最终诠释TYPE_ITEM2 = 1;
私有静态最终诠释TYPE_ITEM3 = 2;
 

然后

  int型的;
@覆盖
公众诠释getItemViewType(INT位置){

    如果(位置== 0){
        TYPE = TYPE_ITEM1;
    }否则,如果(位置== 1){
        TYPE = TYPE_ITEM2;
    }
    其他
    {
         TYPE = TYPE_ITEM3;
    }
    返回类型;
}

 @覆盖
 公众诠释getViewTypeCount(){
        返回3;
 }
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
LayoutInflater充气= NULL;
整型= getItemViewType(位置);
  //而不是其他人的话,你可以使用情况
   如果(行== NULL){
    如果(类型== FIRST_TYPE){
            // infalte TYPE1布局
      }
    如果(类型== SECOND_TYPE){
            // infalte 2型布局
    }  其他 {
            // infalte normaltype布局
 }
}
 

I'm reading this tutorial http://android.amberfog.com/?p=296 . I'd like to create a Listview weith different types of rows. I understand how to create the adapter, but what about the xml layout? So I definire an xml layout like this one:

<ListView/>

<TextView android:id="@+id/id1" />

<TextView android:id="@+id/id2" />

<ImageView android:id="@+id/id3" />

<TextView android:id="@+id/id4" />

Will it be a problem (for performance) if maybe one row uses just some elements (only some textviews) of the layout and another row maybe uses other elements? I don't understand if mine is the right way to define the xml or if I have to create different layout for each type of row.

Thank you in advance

EDIT: now I'm having a null point exception.

java code from the adapter:

@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>

解决方案

You need to override getViewItemType and getViewTypeCount. You will also need to have custom layouts.

getItemViewType(int position) - returns information which layout type you should use based on position.

You should have a look at the video in the link.

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; 

Then

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
 }
} 

这篇关于安卓:与不同的项目列表视图XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆