How to add a footer in ListView?

前端 未结 7 1557
时光取名叫无心
时光取名叫无心 2020-11-22 09:35

I am developing an application,In my application,I am using Listview for displaying data using dom parsing,I want to footer in listview,when i click footer additional more d

相关标签:
7条回答
  • 2020-11-22 09:45

    I know this is a very old question, but I googled my way here and found the answer provided not 100% satisfying, because as gcl1 mentioned - this way the footer is not really a footer to the screen - it's just an "add-on" to the list.

    Bottom line - for others who may google their way here - I found the following suggestion here: Fixed and always visible footer below ListFragment

    Try doing as follows, where the emphasis is on the button (or any footer element) listed first in the XML - and then the list is added as "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>
    
    0 讨论(0)
  • 2020-11-22 09:45

    you can use a stackLayout, inside of this layout you can put a list a frame, for example:

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

    this is the result:

    0 讨论(0)
  • 2020-11-22 09:55

    Answers here are a bit outdated. Though the code remains the same there are some changes in the behavior.

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

    Info about addFooterView() method

    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.

    Most of the answers above stress very important point -

    addFooterView() must be called before calling setAdapter().This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.

    From Kitkat this has changed.

    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.

    Documentation

    0 讨论(0)
  • 2020-11-22 09:58

    Create a footer view layout consisting of text that you want to set as footer and then try

    View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
    ListView.addFooterView(footerView);
    

    Layout for footer could be something like this:

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

    The activity class could be:

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:01

    If the ListView is a child of the ListActivity:

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

    (inside onCreate())

    0 讨论(0)
  • 2020-11-22 10:06

    In this Question, best answer not work for me. After that i found this method to show listview footer,

    LayoutInflater inflater = getLayoutInflater();
    ViewGroup footerView = (ViewGroup)inflater.inflate(R.layout.footer_layout,listView,false);
    listView.addFooterView(footerView, null, false);
    

    And create new layout call 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>
    

    If not work refer this article hear

    0 讨论(0)
提交回复
热议问题