【问题标题】:How to add footer button in horizontal listview如何在水平列表视图中添加页脚按钮
【发布时间】:2013-05-29 18:41:43
【问题描述】:

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

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

如何在列表视图页脚中添加按钮? 这是我的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>

【问题讨论】:

    标签: android listview footer


    【解决方案1】:
    1. 覆盖列表视图适配器的getCount() 以返回data.size()+1 元素;
    2. 在 getView() 中插入如下内容:

      if (位置 == data.size()){ 返回 getFooterView();
      }

    getFooterView() 中使用按钮膨胀适当的布局。

    【讨论】:

    • 另外看看这个帖子:stackoverflow.com/questions/6924047/…
    • 哈哈,只是想说不太明白你的意思..谢谢你的附加链接,我试着先学习。 ^^
    • 但我完全不使用列表视图,我该如何应用 listView.addFooterView(footer); 在我的情况下? >.vogella.de/articles/AndroidListView/article.html#headerfooter
    • 不明白你的意思,在你提到的vogella链接中是一个例子,对你不起作用吗?
    • 是的.. 或者我不知道如何应用我的情况,因为我的情况似乎有点不同,我只使用一个 xml 来添加行水平列表视图。
    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多