0

也有类似的问题,但我还没有看到一个答案这个非常直接的问题:你如何设置一个自定义列表视图混合的布局?Android:使用删除按钮设置ListView

我试图建立一个列表视图,其中每一行有以下几点:

初级文本行,二次的(子)文本行,和删除按钮。另一种方法是让每行在可见空间的页眉或页脚处使用删除按钮进行选择(但显然不在可滚动空间中)。说实话,在这一点上更容易一些是可取的。

我剩下的就是设置一个辅助布局xml来定义每一行元素,但在里面我正在寻找另一个布局来将按钮与文本布局的垂直方向分开。

这里是我当前的代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <ListView android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:choiceMode="multipleChoice" 
    android:id="@+id/android:list"/> 
</LinearLayout> 

每一行的布局是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView android:id="@+id/line_a" 
    android:textSize="14dp" 
    android:textColor="#FFFFFF" 
    android:textStyle="italic" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/line_b" 
    android:textSize="12dp" 
    android:textColor="#BFFF00" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

这个选项可以设置每行一个不错的多色两级文本在ListView 。

那么接下来呢?要在右侧添加一个按钮,我需要使用右侧的按钮来设置另一个布局(网格可能是?2列1行?)。另一种替代方法就像是用水平方向在另一个线性布局中包裹现有的垂直线性布局,是的?

难道这不会让任何人感到尴尬和痛苦吗? :)

回答

2

创建一个ListView的ArrayAdapter:

listView = (ListView) findViewById(R.id.listView); 
listViewArrayAdapter listViewArrayAdapter = new listViewArrayAdapter(this, values); 
listView.setAdapter(listViewArrayAdapter); 

然后创建一个内部类:

public class ListViewArrayAdapter extends ArrayAdapter<Value> { 

    Context context; 
    Value[] values; 

    public ListViewArrayAdapter(Context context, Value[] values) { 
     super(context, R.layout.layout_with_list, values); 
     this.values = values; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.list_item, parent, false); 

     TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView); 
     titleTextView.setText(values[position].getName()); 
      // If you have buttons in your list_item.xml, you can inflate them an then create a listener for them 

     return convertView; 

    } 
} 
+0

这就是答案。 – 2013-04-09 21:37:41

+0

谢谢阿里。我假设我膨胀的“list_item”布局对应于我上面共享的行布局? – Raevik 2013-04-10 13:02:47

0

为什么不`吨您使用相对布局,我必须让这个3H前。这是我的ItemRow代码。我认为更棘手的是处理这个按钮的onClick方法。在我的代码中,第二个图像是删除按钮,图像白色的onClick方法。我在iPhone

延长一个ArrayAdapter,因为我想只有在编辑模式下此图片是可见的,如删除联系人项目
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layout_item" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<ImageView android:id="@+id/image_list_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:contentDescription="@string/image_view" 
    android:src="@drawable/clock_white" /> 
<TextView android:id="@+id/text_list_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_toRightOf="@id/image_list_icon" 
    android:paddingLeft="5dp" 
    android:text="@string/hello_world" /> 
<ImageView android:id="@+id/image_list_remove" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:contentDescription="@string/image_view" 
    android:src="@drawable/remove" /> 
<TextView android:id="@+id/text_list_time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_toLeftOf="@id/image_list_remove" 
    android:paddingRight="10dp" 
    android:text="@string/text_x" />