如何在水平列表视图中添加页脚按钮

时间:2013-06-03 14:56:35

标签: android listview footer

我想在listview的页脚中添加一个按钮,我正在使用水平列表视图,适配器类

public ItemAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
        super(context, layout, c, from, to,0);
        this.layout = layout;
        inflator= LayoutInflater.from(context);

    }

我返回newView和bindView:

    public View newView(Context context, Cursor cursor, ViewGroup parent) {     
        View v = inflator.inflate(layout, parent, false);
        TextView footer = (TextView)v.findViewById(R.id.bAdd);
        return v;

    }

    @Override
    public void bindView(View v, final Context context, Cursor c) {
        final int id = c.getInt(c.getColumnIndex(ItemDb.ID));
        final String name = c.getString(c.getColumnIndex(ItemDb.NAME));
        }

和活动类:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        String[] from = new String[] { ItemDb.NAME, ItemDb.CONDITION, ItemDb.EMAIL};
        int[] to = new int[] { R.id.etItemName, R.id.etItemCondition, R.id.etEmail };
        itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to);
        this.setListAdapter(itemAdapter);
    }

如何在listview页脚中添加按钮? 这是我的xml

<?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" >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:background="@drawable/photo_frame"
        android:contentDescription="@string/imagedescription"
        android:src="@drawable/ic_launcher" >
    </ImageView>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/condition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/name"
            android:layout_marginLeft="10dp"
            android:text="@string/condition"
            android:textColor="#000"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/condition"
            android:layout_marginLeft="10dp"
            android:text="@string/email"
            android:textColor="#000"
            android:textSize="14sp" />

    </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

  1. 覆盖listview适配器的getCount()以返回data.size()+1元素;
  2. 在getView()中插入如下内容:

    if(position == data.size()){     return getFooterView();
    }

  3. getFooterView()中使用按钮对适当的布局进行充气。

相关问题