【问题标题】:How to add a footer in ListView?如何在 ListView 中添加页脚?
【发布时间】:2011-05-14 23:51:11
【问题描述】:

我正在开发一个应用程序,在我的应用程序中,我正在使用 Listview 使用 dom 解析来显示数据,我想在 listview 中添加页脚,当我单击页脚时,其他更多数据添加到列表视图,我附加了图像,我想要那个设计和处理,请参考image1和imgae2。我在红色矩形中提到了页脚

Fig1-Footer like "More News"

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

【问题讨论】:

    标签: android android-listview footer


    【解决方案1】:

    创建一个包含要设置为页脚的文本的页脚视图布局,然后尝试

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

    【讨论】:

    • 这很好,但是如果您希望页脚在屏幕上固定,而不是仅在滚动到底部时可见怎么办?如果您希望即使列表为空也能看到页脚怎么办?请参阅stackoverflow.com/questions/12353701/… 上的完整问题。
    • 我在使这段代码工作时遇到了一些问题并解决了它们。以下是 addFooterView() 文档中的说明 NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.
    • @gcl1 在这种情况下,这不是页脚,只是布局中位于列表视图下方的普通元素。只是,你知道,把它放在列表视图下面吗?
    • 只是对@demonsten 大喊一声:如果在添加视图之前调用 setAdapter(),你会遇到麻烦。
    • 如果适配器中没有数据,则页脚未附加(显示)。那么即使适配器中没有数据,我们如何显示页脚呢?谢谢
    【解决方案2】:

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

    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 的支持实例进行包装。

    Documentation

    【讨论】:

      【解决方案3】:

      我知道这是一个非常古老的问题,但我在这里搜索了我的方式,发现提供的答案不是 100% 令人满意,因为正如 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>
      

      【讨论】:

        【解决方案4】:

        如果 ListView 是 ListActivity 的子项:

        getListView().addFooterView(
            getLayoutInflater().inflate(R.layout.footer_view, null)
        );
        

        (在 onCreate() 内)

        【讨论】:

          【解决方案5】:

          您要在其中添加列表视图页脚的活动,并且我还在列表视图页脚单​​击时生成了一个事件。

            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>
          

          【讨论】:

            【解决方案6】:

            在这个问题中,最佳答案对我不起作用。之后我找到了这个方法来显示列表视图页脚,

            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

            【讨论】:

              【解决方案7】:

              你可以使用stackLayout,在这个布局里面你可以放一个list一个frame,例如:

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

              这是结果:

              【讨论】:

                猜你喜欢
                • 2012-01-30
                • 2011-03-14
                • 2011-03-25
                • 2015-03-04
                • 1970-01-01
                • 2012-05-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多