1

我想知道什么?
我对是否可以在 android 中的空 ListView 中添加页脚有点好奇。我检查了 ListView 页脚的文档,但他们没有提到与此相关的任何内容。所以只是想知道这是否可能?

为什么我需要这个?
实际上我的应用程序中有edittext(搜索框),它将接受用户输入的字符。基于这个输入的文本,我将在 DB 中进行一些搜索并在 ListView 中显示匹配结果。我打算做的是在 ListView 底部显示一个按钮(类似于页脚),它将为用户提供扩展的搜索选项。

一些代码供参考:

    @Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}

@Override
public void afterTextChanged(Editable s)
{
    strGuestCardToSerach = s.toString();

    if(strGuestCardToSerach.equals(""))
    {
        if(listview.getFooterViewsCount() > 0)
        {
            listview.removeFooterView(footerView);
            listviewAdapter.notifyDataSetChanged();
        }
    }
    else
    {
        if(listview.getFooterViewsCount() == 0)
        {
            footerView = inflater.inflate(R.layout.footer_view_layout, null);
            listview.addFooterView(footerView);
            listview.setAdapter(recentGuestAdapter);
            listviewAdapter.notifyDataSetChanged();
        }
    }
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
}  

@Edit:
一旦您在 ListView 中至少有一个条目,它就可以工作。所以只是想了解是否可以将页脚添加到空的 ListView。

提前致谢。

4

4 回答 4

2

我发布这个以防万一有人在寻找与我类似的功能。答案是否定的,您不能将页脚视图添加到空的 ListView。为了实现类似的功能,您将创建自己的布局并将其显式添加到 ListView 并管理其状态。

鲁佩什

于 2013-04-03T09:49:45.137 回答
1

我想您在 ListView 上使用 ListAdapter?只需将“页脚项”添加到您的 ArrayList/ 任何列表项。在 getView 方法中,检查该项目是否是您的页脚,然后在您拥有按钮或其他任何内容的位置设置自定义布局。只是问我是否不清楚我的意思。

于 2013-03-08T12:15:26.000 回答
0

要在列表为空时实现此场景:创建适配器并为其分配布局:

  1. 当列表不为空时,使用布局为其分配适配器。
  2. 当列表为空时,分配列表一个虚拟适配器并具有一个初始值。
  3. 不要在 getview 中为虚拟布局分配值或将虚拟视图的大小设置为 0 dp。所以你可以实现这个场景......进一步询问我已经解决了这个场景
于 2014-11-26T06:46:24.410 回答
0
public void addFooterView (View v)
Added in API level 1
Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Parameters
v
The view to add.
于 2014-11-26T11:08:02.180 回答