简体   繁体   中英

List View Footer is leaving spaces and not getting removed

I am using list view footer which is just a button. Whenever i am updating the list with load more button it add extra space at the bottom. Also i am trying to remove the footer when all d data is loaded.

Initialing the button in oncreate

btnLoadMore = new Button(getActivity());
        btnLoadMore.setText("Load More");
        btnLoadMore.setWidth(700);
        btnLoadMore.setHeight(30); 

Then in post Execute

                    mainAdapter = new SimpleAdapter(  
                            getActivity(), mainfooditems,
                            R.layout.list_mealiteml, new String[] {KEY_FLFOODNAME,KEY_FLCALORIES,KEY_FLFOODID,KEY_FLFAT,KEY_FLCARBS,KEY_FLPROTEIN,KEY_FLBRAND,KEY_FLSERVING}, new int[] { 
                                R.id.nameone,R.id.uiduid,R.id.mtvfoodid,R.id.fatoutputval,R.id.carbsoutval,R.id.proteinsoutval,R.id.tvbrandnameone,R.id.tvservingone });
                    // updating listview
                    listMain.addFooterView(btnLoadMore);
                    listMain.setAdapter(mainAdapter);
                    mainAdapter.notifyDataSetChanged();
state3();

And state3 is as follows:

public void state3(){
        search.clearFocus();

        setListViewHeightBasedOnChildren(listMain);
        setListViewHeightBasedOnChildren(listCustom);
        setListViewHeightBasedOnChildren(listRecent);
        tvError.setVisibility(View.INVISIBLE);
        tvMessage.setVisibility(View.INVISIBLE);
        finalfoodidint=Integer.parseInt(finalfoodid);
        if(finalfoodidint > oldlastid){

            System.out.println("1.5 + " + finalfoodidint);
            System.out.println("1.6 + " + oldlastid);

        }
        else if(finalfoodidint == oldlastid){
            //listMain.removeFooterView(btnLoadMore);
            listMain.removeFooterView(btnLoadMore);
            System.out.println("1.7 + " + finalfoodidint);
            System.out.println("1.8 + " + oldlastid);
        }
        else{
            listMain.removeFooterView(btnLoadMore);
            System.out.println("1.9 + " +finalfoodidint);
            System.out.println("1.10 + " +  oldlastid);
        }
    }

它出现

Try calling notifyDataSetChanged() on the list when you remove an header. Try adding this line at the end of public void state3()

listMain.getAdapter().notifyDataSetChanged();

I changed my code in public void state3 to

public void state3(){
        search.clearFocus();
        listMain.removeFooterView(btnLoadMore); 
        tvError.setVisibility(View.INVISIBLE);
        tvMessage.setVisibility(View.INVISIBLE);
        finalfoodidint=Integer.parseInt(finalfoodid);
        if(finalfoodidint > oldlastid){

            listMain.addFooterView(btnLoadMore);

            System.out.println("1.5 + " + finalfoodidint);
            System.out.println("1.6 + " + oldlastid);

        }
        else if(finalfoodidint == oldlastid){
            //listMain.removeFooterView(btnLoadMore);
            listMain.removeFooterView(btnLoadMore);
            System.out.println("1.7 + " + finalfoodidint);
            System.out.println("1.8 + " + oldlastid);
        }
        else{
            listMain.removeFooterView(btnLoadMore);
            System.out.println("1.9 + " +finalfoodidint);
            System.out.println("1.10 + " +  oldlastid);

        }
        listMain.setAdapter(mainAdapter);
        mainAdapter.notifyDataSetChanged();
        setListViewHeightBasedOnChildren(listMain);
        setListViewHeightBasedOnChildren(listCustom);
        setListViewHeightBasedOnChildren(listRecent);
    }

Also changed post execute to

mainAdapter = new SimpleAdapter(  
                                getActivity(), mainfooditems,
                                R.layout.list_mealiteml, new String[] {KEY_FLFOODNAME,KEY_FLCALORIES,KEY_FLFOODID,KEY_FLFAT,KEY_FLCARBS,KEY_FLPROTEIN,KEY_FLBRAND,KEY_FLSERVING}, new int[] { 
                                    R.id.nameone,R.id.uiduid,R.id.mtvfoodid,R.id.fatoutputval,R.id.carbsoutval,R.id.proteinsoutval,R.id.tvbrandnameone,R.id.tvservingone });
                        // updating listview

                        state3();

change your "setListViewHeightBasedOnChildren" method like below.

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM