Last Item on Listview is cut off

11,614

Solution 1

Use RecyclerView instead of ListView. It will work for sure. Scrolling of the toolbar strips the bottom space of your layout. ListView won't work perfectly with app:layout_behavior="@string/appbar_scrolling_view_behavior". If you want scrolling and view full items ,use RecyclerView or NestedScrollView instead of ListView.

If you remove app:layout_behavior="@string/appbar_scrolling_view_behavior" and

app:layout_scrollFlags="scroll|enterAlways"  

of toolbar you can see ListView with full items.

So, use RecyclerView instead of ListView with app:layout_behavior="@string/appbar_scrolling_view_behavior" and app:layout_scrollFlags="scroll|enterAlways". It will works with scrolling and layout behavior.

Solution 2

Put in a line like android:paddingBottom="20dp" into your ListView item in your Fragment XML file. Adjust the 20dp value until it looks the way you want it to.

For some strange reason it just fixes the problem, and it doesn't have to do with the size of the ListView being changed. Somehow the small padding forces the ListView to layout in a different way.

Solution 3

Adding below line in your RecyclerView should solve the issue.

android:layout_marginBottom="?attr/actionBarSize"
Share:
11,614
Christopher Zhou
Author by

Christopher Zhou

Updated on July 21, 2022

Comments

  • Christopher Zhou
    Christopher Zhou almost 2 years

    So in my Android app I have where the main activity has tabs and in each tab there is a fragment containing a listview. But for some reason the last item in the listview always gets cut off. I have looked around for solutions but I havent found any yet. Any suggestions?

    Main Activity layout

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"
                app:tabGravity="fill"/>
    
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
    </android.support.design.widget.CoordinatorLayout>
    

    Fragment with ListView

    <?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:orientation="vertical">
    
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            style="@style/Widget.AppCompat.ProgressBar" />
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
    
            <ListView
                android:id="@+id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                style="@style/Widget.AppCompat.ProgressBar"></ListView>
    
        </LinearLayout>
    </RelativeLayout>
    

    List Item 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="wrap_content"
        android:padding="2dp"
        android:layout_margin="5dp">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/title"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="normal"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/author"
            android:textSize="15sp"
            android:typeface="normal"
            android:layout_below="@+id/publishdate"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/publishdate"
            android:textSize="15sp"
            android:typeface="normal"
            android:layout_below="@+id/title"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    
    </LinearLayout>
    

    List Adapter Class

    package com.czhou.dailyprincetoniannewspaper.adapters;
    
    import android.content.ClipData;
    import android.content.Context;
    import android.graphics.Paint;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    
    import com.czhou.dailyprincetoniannewspaper.NewspaperMetaObject;
    import com.czhou.dailyprincetoniannewspaper.R;
    
    import java.util.List;
    
    public class NewsListAdapter extends ArrayAdapter<NewspaperMetaObject> {
    
        static class ViewHolder {
            TextView author;
            TextView publishdate;
            TextView title;
        }
    
        private LayoutInflater inflater;
        List<NewspaperMetaObject> newsitems;
        public NewsListAdapter(Context context, List<NewspaperMetaObject> items) {
            super(context, R.layout.newslistitem, items);
            this.newsitems = items;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
                 ViewHolder viewHolder;
    
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.newslistitem, parent, false);
    
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.author = (TextView) convertView.findViewById(R.id.author);
                viewHolder.publishdate = (TextView) convertView.findViewById(R.id.publishdate);
                convertView.setTag(viewHolder);
    
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
    
            viewHolder.title.setText(newsitems.get(position).getArticleTitle());
            viewHolder.title.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
            viewHolder.publishdate.setText(newsitems.get(position).getArticlePublishDate());
            viewHolder.author.setText(newsitems.get(position).getArticleAuthor());
            System.out.println(viewHolder.publishdate.getText());
    
            return convertView;
        }
    
    }
    

    Main Activity Class

    package com.czhou.dailyprincetoniannewspaper;
    
    import android.os.Bundle;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.RelativeLayout;
    
    import java.util.ArrayList;
    import java.util.List;
    /**
     * Created by czhou on 11/21/2015.
     */
    public class MainActivity extends AppCompatActivity {
    
        private Toolbar toolbar;
        private TabLayout tabLayout;
        private ViewPager viewPager;
        private ProgressBar mProgressBar;
        private CoordinatorLayout coordinatorLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
    
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);
    
            tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
        }
    
        private void setupViewPager(ViewPager viewPager){
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
            adapter.addFragment(new PUNewsFragment(), "News");
            adapter.addFragment(new PUSportsFragment(), "Sports");
    
            viewPager.setAdapter(adapter);
        }
    
        class ViewPagerAdapter extends FragmentPagerAdapter {
            private final List<Fragment> mFragmentList = new ArrayList<>();
            private final List<String> mFragmentTitleList = new ArrayList<>();
    
            public ViewPagerAdapter(FragmentManager manager) {
                super(manager);
            }
    
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }
    
            @Override
            public int getCount() {
                return mFragmentList.size();
            }
    
            public void addFragment(Fragment fragment, String title) {
                mFragmentList.add(fragment);
                mFragmentTitleList.add(title);
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentTitleList.get(position);
            }
        }
    }
    
  • BlackBox
    BlackBox almost 8 years
    Maybe people can argue RecyclerView is better... but this actually is a perfect quickfix to the issue without having to refactor too much stuff for a single UI bug. Saved me!
  • Gi0rgi0s
    Gi0rgi0s almost 8 years
    Glad to hear it and I agree with you. ListView isn't deprecated for a reason. It's a useful widget for a simple vertical list.
  • AndroidGuy
    AndroidGuy over 7 years
    good suggestion. I am facing the same issue, but in my case, I am using a Expandable list view & what I find is that implementing Recycler view with Expandable list view is a bit tough. I do not want to use custom libraries at this point of time. Any solution comes to your mind.? I tried adding paddingBottom, but that does not work.
  • InsanityOnABun
    InsanityOnABun about 6 years
    This is not practical for the vast majority of apps, as the action bar is a standard UI component for Android apps.