简体   繁体   English

Listview上的最后一项被切断

[英]Last Item on Listview is cut off

So in my Android app I have where the main activity has tabs and in each tab there is a fragment containing a listview. 所以在我的Android应用程序中,我有主要活动有标签的地方,每个标签中都有一个包含列表视图的片段。 But for some reason the last item in the listview always gets cut off. 但由于某种原因,listview中的最后一项始终被切断。 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 使用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);
        }
    }
}

Put in a line like android:paddingBottom="20dp" into your ListView item in your Fragment XML file. 将像android:paddingBottom="20dp"放入Fragment XML文件的ListView项中。 Adjust the 20dp value until it looks the way you want it to. 调整20dp值,直到它看起来像你想要的那样。

For some strange reason it just fixes the problem, and it doesn't have to do with the size of the ListView being changed. 由于一些奇怪的原因,它只是解决了问题,而且与更改的ListView的大小没有关系。 Somehow the small padding forces the ListView to layout in a different way. 不知何故,小填充强制ListView以不同的方式布局。

在RecyclerView中添加以下行应该可以解决问题。

android:layout_marginBottom="?attr/actionBarSize"

Use RecyclerView instead of ListView. 使用RecyclerView而不是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" . ListView无法与app:layout_behavior="@string/appbar_scrolling_view_behavior"完美配合app:layout_behavior="@string/appbar_scrolling_view_behavior" If you want scrolling and view full items ,use RecyclerView or NestedScrollView instead of ListView. 如果要滚动并查看完整项目,请使用RecyclerView或NestedScrollView而不是ListView。

If you remove app:layout_behavior="@string/appbar_scrolling_view_behavior" and 如果删除app:layout_behavior="@string/appbar_scrolling_view_behavior"

app:layout_scrollFlags="scroll|enterAlways"  

of toolbar you can see ListView with full items. 您可以在工具栏中看到包含完整项目的ListView。

So, use RecyclerView instead of ListView with app:layout_behavior="@string/appbar_scrolling_view_behavior" and app:layout_scrollFlags="scroll|enterAlways" . 因此,使用RecyclerView而不是ListView与app:layout_behavior="@string/appbar_scrolling_view_behavior"app:layout_scrollFlags="scroll|enterAlways" It will works with scrolling and layout behavior. 它将适用于滚动和布局行为。

For anyone still having this issue, removing the action bar worked for me. 对于仍有此问题的人,删除操作栏对我有用。

Inside your AndroidManifest: 在您的AndroidManifest中:

android:theme="@style/AppTheme.NoActionBar">

Also delete the Action Bar from the layout XML for the Activity. 还要从活动的布局XML中删除操作栏。

And remove this from your Activity class: 并从Activity类中删除它:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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