如何在ListView中添加页脚?

89

我正在开发一个应用程序,在我的应用程序中,我使用Listview通过dom解析显示数据,我想在listview中添加一个页脚,当我点击页脚时,会向listview中添加更多的数据。我附上了图像,请参考图片1和图片2,我想要的设计和过程如图1中红色矩形所示。

图1-像“更多新闻”一样的页脚
alt text

alt text

图2-在listview中添加额外的10条记录

8个回答

209
创建一个页脚视图布局,其中包含您想要设置为页脚的文本,然后尝试。
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);

页脚的布局可以像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="7dip"
    android:paddingBottom="7dip"
    android:orientation="horizontal"
    android:gravity="center">

    <LinearLayout 
        android:id="@+id/footer_layout" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_gravity="center">

    <TextView 
        android:text="@string/footer_text_1" 
        android:id="@+id/footer_1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="14dip" 
        android:textStyle="bold" 
        android:layout_marginRight="5dip" />
    </LinearLayout>
</LinearLayout> 

这个活动类可以是:

public class MyListActivty extends ListActivity {
    private Context context = null;
    private ListView list = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = (ListView)findViewById(android.R.id.list);

        //code to set adapter to populate list
        View footerView =  ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
        list.addFooterView(footerView);
    }
}

8
这个方法不错,但如果你想让页脚固定在屏幕上方,而不仅是滚动到底部才能看到呢?如果列表为空的时候也想让页脚可见呢?请参考这个链接获取完整问题:https://dev59.com/Lmct5IYBdhLWcg3wF5tF。 - gcl1
33
我曾经在编写这段代码时遇到了一些问题,但现在已经解决了。这里是addFooterView()的文档说明:请注意,在调用setAdapter之前调用此方法。这样ListView可以用一个包含页眉和页脚视图的光标来封装提供的光标。 - demosten
13
在这种情况下,那不是页脚,只是你布局中在列表视图下方的普通元素。只需要把它放在列表视图下方即可。 - Davor
2
向@demonsten喊话:如果在添加视图之前调用setAdapter(),你会度过糟糕的时光。 - Ben Ogorek
1
如果适配器中没有数据,则页脚不会附加(显示)。那么,即使适配器中没有数据,我们如何显示页脚呢?谢谢。 - Kalpesh Lakhani
显示剩余2条评论

10

这里的答案有点过时了。尽管代码保持不变,但行为上有一些变化。

public class MyListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
        getListView().addFooterView(footerView);
        setListAdapter(new ArrayAdapter<String>(this, getResources().getStringArray(R.array.news)));
    }
}

关于addFooterView()方法的信息

  

添加一个固定视图出现在列表底部。如果多次调用addFooterView(),则这些视图将按照它们被添加的顺序出现。使用此调用添加的视图可以获取焦点(如果需要)。

以上大部分答案都强调了非常重要的一点-

  

addFooterView()必须在调用setAdapter()之前调用。这样ListView就可以使用一个包含头部和尾部视图的光标来包装提供的光标。

从Kitkat开始,这已经改变了。

  

注意:当首次引入此方法时,只能在使用setAdapter(ListAdapter)设置适配器之前调用此方法。从KITKAT开始,可以随时调用此方法。如果ListView的适配器没有扩展HeaderViewListAdapter,则会将其包装为支持WrapperListAdapter的实例。

文档


9

我知道这是一个非常古老的问题,但我通过谷歌找到了这里,并且发现提供的答案并不完全令人满意,因为正如gcl1所提到的那样 - 这种方式下底部栏不是真正的屏幕底部 - 它只是列表的“附加”。

总之 - 对于其他可能会通过谷歌找到这里的人 - 我在这里找到了以下建议:Fixed and always visible footer below ListFragment

试试按照以下方式操作,其中重点是将按钮(或任何底部元素)列在XML中的第一位,然后将列表添加为“layout_above”:

<RelativeLayout>

<Button android:id="@+id/footer" android:layout_alignParentBottom="true"/> 
<ListView android:id="@android:id/list" **android:layout_above**="@id/footer"> <!-- the list -->

</RelativeLayout>

6
如果ListView是ListActivity的子项:
getListView().addFooterView(
    getLayoutInflater().inflate(R.layout.footer_view, null)
);

(在 onCreate() 中)

2
您想要在列表视图中添加页脚并生成一个在页脚点击时触发的事件。
  public class MainActivity extends Activity
{

        @Override
        protected void onCreate(Bundle savedInstanceState)
         {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView  list_of_f = (ListView) findViewById(R.id.list_of_f);

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.web_view, null);  // i have open a webview on the listview footer

            RelativeLayout  layoutFooter = (RelativeLayout) view.findViewById(R.id.layoutFooter);

            list_of_f.addFooterView(view);

        }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg" >

    <ImageView
        android:id="@+id/dept_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dept_nav" />

    <ListView
        android:id="@+id/list_of_f"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dept_nav"
        android:layout_margin="5dp"
        android:layout_marginTop="10dp"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/transparent" >
    </ListView>

</RelativeLayout>

0
   // adding a footer to list as Log Out
    val view: View =
        (getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
            R.layout.logout_menu, null, false
        )
    //  footer onClick
    view.setOnClickListener {
        doLogout()
        binding.drawerLayout.close()

    }
    expandableListView.addFooterView(view)

0

你可以使用StackLayout,在这个布局内部放置一个列表框架,例如:

<StackLayout VerticalOptions="FillAndExpand">
            <ListView  ItemsSource="{Binding YourList}"
                       CachingStrategy="RecycleElement"
                       HasUnevenRows="True">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <StackLayout  Orientation="Horizontal">
                                <Label Text="{Binding Image, Mode=TwoWay}" />

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Frame BackgroundColor="AliceBlue" HorizontalOptions="FillAndExpand">
                <Button Text="More"></Button>
            </Frame>
        </StackLayout>

这是结果:

enter image description here


0
在这个问题中,最佳答案对我不起作用。之后我找到了这种方法来显示列表视图的页脚。
LayoutInflater inflater = getLayoutInflater();
ViewGroup footerView = (ViewGroup)inflater.inflate(R.layout.footer_layout,listView,false);
listView.addFooterView(footerView, null, false);

并创建名为 footer_layout 的新布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Done"
        android:textStyle="italic"
        android:background="#d6cf55"
        android:padding="10dp"/>
</LinearLayout>

如果不起作用,请参考这篇文章hear


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接