How can I add blank space to the end of a ListView?

后端 未结 5 1969
一生所求
一生所求 2021-01-04 05:26

I have a number of elements in a ListView that scroll off the screen.

I would like there to be blank space at the end of the View. That is, the user should be able

相关标签:
5条回答
  • 2021-01-04 05:38
    1. Inflate any layout of your choice (this could be an XML of and ImageView with no drawable and with set height and width of your choice)
    2. Measure the screen height and create new LayoutParams and set the height of it to 1/2 of the screen height
    3. Set the new layout params on your inflated view
    4. Use the ListView's addFooterView() method to add that view to the bottom of your list (there is also an addHeaderView())

    Code to measure screen height

     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
     Display display = wm.getDefaultDisplay();
     int screenHeight = display.getHeight();
    

    Code to set half screen height:

     View layout = inflater.inflate(R.layout.mylistviewfooter, container, false);
     ViewGroup.LayoutParams lp = layout.getLayoutParams();
     lp.height = screenHeight/2;
     layout.setLayoutParams(lp);
     myListView.addFooterView(layout);
    

    An Aside: When you add a footer or header view to any listview, it has to be done before adding the adapter. Also, if you need to get your adapter class after doing this you will need to know calling the listview's adapter by getAdapter() will return an instance of HeaderViewListAdapter in which you will need to call its getWrappedAdapter method Something like this :

     MyAdapterClassInstance myAdapter = (MyAdapterClassInstance) ((HeaderViewListAdapter) myListView.getAdapter()).getWrappedAdapter();
    
    0 讨论(0)
  • 2021-01-04 05:39

    Try the followings:

    View footer = new View(getActivity());
    footer.setLayoutParams( new AbsListView.LayoutParams( LayoutParams.FILL_PARENT, 100 )); 
    // 100 is the height, you can change it.
    mListView.addFooterView(footer, null, false);
    
    0 讨论(0)
  • 2021-01-04 05:47

    this 100% works. in adapter set your code like this

       //in getCount
       @Override
       public int getCount() {
       return ArrayList.size()+1;
          }
        //in getview make your code like this
        public View getView(final int i, View view, ViewGroup viewGroup) {
    
        view = inflter.inflate(R.layout.yourlayout, null);
    
        if(i<getCount()-1) {
         //your code
        }
        else{
        ViewGroup itemContainer =(ViewGroup) view.findViewById(R.id.container);
                    itemContainer.setVisibility(View.INVISIBLE);
        }
    
        Return view;
        }
    
    0 讨论(0)
  • 2021-01-04 05:57

    if you have multiple listviews in your app, create an xml of a footer, something like this:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="200dp"
    android:layout_width="match_parent"
    android:layout_height="200dp"></LinearLayout>
    

    and then in the code, use this:

    listView.addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.empty200, null));
    
    0 讨论(0)
  • 2021-01-04 06:01

    The accepted answer is too complicated, and addFooterView is not for this kind of thing. The proper and simpler way is to set the paddingTop and paddingBottom, and you need to set clipToPadding to "false". In your list view or grid view, add the following:

        android:paddingTop="100dp"
        android:paddingBottom="100dp"
        android:clipToPadding="false"
    

    You'll get blank space at the top and the bottom that moves with your finger scroll.

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