具有不同布局的 ListView 行

Posted

技术标签:

【中文标题】具有不同布局的 ListView 行【英文标题】:ListView rows with different layouts 【发布时间】:2012-07-23 14:45:24 【问题描述】:

我想要实现的是为每一行设置不同的布局。我ArrayAdapter中的代码如下:

@Override
    public int getCount() 
        return contentList.size();
    

    @Override
    public MyContents getItem(int position) 
        return content.get(position);
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public int getViewTypeCount() 
        // TODO Auto-generated method stub
        return 2;
    

    @Override
    public int getItemViewType(int position) 
        // TODO Auto-generated method stub
        return super.getItemViewType(position);
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        final MyContents entry = content.get(position);

        View row = convertView;
        LayoutInflater inflater = null;
        boolean _haschild_ = entry.testBoolean();
        if (row == null) 
            if (entry.testBoolean()) 
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater
                        .inflate(R.layout.contents_layout, null);
             else 
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.test_layout, null);
            
         

        if (entry.testBoolean()) 
            TextView txtView = (TextView) row
                    .findViewById(R.id.txtView);
            //.....
         else 
            ((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
                    .setText("test: " + position); //**LOE**
        


        return row;
    

*test_layout.xml* 的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_test"
    android:layout_
    android:layout_
    android:layout_margin="12dp"
    android:background="@color/grey_five"
    android:orientation="vertical"
    android:padding="@dimen/padding_small" >

    <TextView
        android:id="@+id/txtView_test"
        android:layout_
        android:layout_
        android:layout_alignParentLeft="true"
        android:text="@string/lorem__ipsum"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/grey" />


</RelativeLayout>

但是每当我执行它时,它都会抛出一个NullPointerException(检查带有注释的行 //LOE)。

有什么建议吗?过去一天我一直在为此苦苦挣扎。

【问题讨论】:

【参考方案1】:

出现NullPointerException 是因为您没有实现getItemViewType。现在您无论如何都返回第一种类型的行布局,这是一个没有 *txtView_test* TextView) 的布局。你的代码应该是这样的:

// some fields
public static final int FIRST_TYPE = 0;
public static final int SECOND_TYPE = 1;

@Override
public int getItemViewType(int position) 
     MyContents item = getItem(position);
     if (item.testBoolean()) 
          return FIRST_TYPE;
      else 
          return SECOND_TYPE; 
      


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    final MyContents entry = content.get(position);
    View row = convertView;
    LayoutInflater inflater = null;
    int type = getItemViewType(position);
    boolean _haschild_ = entry.testBoolean();
    if (row == null) 
        if (type == FIRST_TYPE) 
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater
                    .inflate(R.layout.contents_layout, null);
         else 
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.test_layout, null);
        
     
    if (type == FIRST_TYPE) 
        TextView txtView = (TextView) row
                .findViewById(R.id.txtView);
        //.....
     else 
        ((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
                .setText("test: " + position); //**LOE**
    

【讨论】:

【参考方案2】:

使用,

LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.test_layout, parent, false);

愿它解决你的问题。

【讨论】:

【参考方案3】:

试试这个 :::

convertView = getLayoutInflater().inflate(R.layout.my_row_layout, null);


TextView txtName = (TextView)convertView.findViewById(R.id.txtMyTextView);

txtName.setText("Hi,,,,. Hows You..."); 

【讨论】:

以上是关于具有不同布局的 ListView 行的主要内容,如果未能解决你的问题,请参考以下文章

ListView: setvisibility() 强制刷新所有行

关于理解布局单位

颤振键盘隐藏嵌套列表视图中的文本字段

具有自定义行布局的 ListView - Android

具有多种布局的Android ListView

手把手教你做视频播放器