smail2133
smail2133

Reputation: 954

Android listview footer view

I have problem with footer view. I make list view with dynamically loading items. When the button clicked in footer view. Footer attaching to list view when it need by call two next methods:

protected void addFooterView() {
        removeFooterView();
        searchListView.post(new Runnable() {
            @Override
            public void run() {
                ListAdapter adapter = searchListView.getAdapter();
                searchListView.setAdapter(null);
                searchListView.addFooterView(footerView);
                searchListView.setAdapter(adapter);
            }
        });
    }

    protected void removeFooterView() {
        searchListView.post(new Runnable() {
            @Override
            public void run() {
                if (searchListView.getFooterViewsCount() > 0) {
                    ListAdapter adapter = searchListView.getAdapter();
                    searchListView.setAdapter(null);

                    while (searchListView.getFooterViewsCount() > 0) {
                        searchListView.removeFooterView(footerView);
                    }

                    searchListView.setAdapter(adapter);
                }
            }
        });
    }

Inflating views:

   private void declareViewAndButton(View view) {
        this.searchListView = (ListView) view.findViewById(R.id.searchListView);
        this.footerView = LayoutInflater.from(getActivity()).inflate(R.layout.search_list_view_footer, searchListView, false);
    }

So, when footer view goes bottom of visible position listview. And go back by scrolling, are footer view height size dynamically changing. How to fix it ?? Or tell me where I can find cause of problem ? Sorry for my English.

Upvotes: 2

Views: 1066

Answers (1)

smail2133
smail2133

Reputation: 954

Start in KITKAT api you can call aading/removing footer and header methods anywhere, setted adapter or not doesn't matter. Like say in a official doc, aading/removing footer and header view need call before set adapter. But this is almost true, if in you case need manage footer but not header. In some device footer work success, on another need single reset adapter, after work normal. I don't know, this is are bug or feature!!

So, after some experiments, I wrote add and remove footer view methods. Works excellent:

protected void addFooterView() {
        removeFooterView();
        searchListView.post(new Runnable() {
            @Override
            public void run() {
                searchListView.addFooterView(footerView);

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                    int lastViewedPosition = searchListView.getFirstVisiblePosition();
                    View v = searchListView.getChildAt(0);
                    int topOffset = (v == null) ? 0 : v.getTop();

                    searchListView.setAdapter(searchAdapter);
                    searchListView.setSelectionFromTop(lastViewedPosition, topOffset);
                }
            }
        });
    }

    protected void removeFooterView() {
        searchListView.post(new Runnable() {
            @Override
            public void run() {
                while (searchListView.getFooterViewsCount() > 0) {
                    searchListView.removeFooterView(footerView);
                }
            }
        });
    }

Tested on 19,16,10 api's.

Upvotes: 1

Related Questions