首页 > 解决方案 > Viewpager 内容未使用 ScrollView 在片段中显示,但显示滚动条

问题描述

我的问题不一样。使用下面的 xml 布局,我可以在活动中流畅地显示 viewpager 页面。滚动视图和内容显示都没有问题。但是当我在导航抽屉的片段中使用相同的代码时,然后scroll bar显示但内容不显示。空白页显示。

我用过android:fillViewport="true"。但没有结果。下面是图像。我也使用了固定高度scrollview。但没有结果。

在此处输入图像描述

这是主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </android.support.v4.view.ViewPager>
</LinearLayout>

这是子布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:clickable="true"
    android:focusable="true"
    android:scrollbars="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text1"
            android:textSize="20sp"/>
    </LinearLayout>
</ScrollView>

这是主要的java

public class SwipeNav extends Fragment {
    private MyPagerAdapter myPagerAdapter;
    private ViewPager viewPager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.nav_swipe, container, false);
            myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getContext());
            viewPager = (ViewPager) rootView.findViewById(R.id.pager);
            viewPager.setAdapter(myPagerAdapter);
            return rootView;
        }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

这是 MyPagerAdapter 类

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    private Context context;

    public MyPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context= context;
    }

    @Override
    public Fragment getItem(int i) {
        ArrayList<String> listItem = new ArrayList<String>();
        SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(context).getWritableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM ARATRIKA", new String[]{});
        if (cursor.getCount() == 0) {
        } else {
            while (cursor.moveToNext()) {
                listItem.add(cursor.getString(0));
            }
        }
        Object[] mStringArray = listItem.toArray();
        Fragment fragment = new AFragment();
        Bundle args = new Bundle();
        args.putString(AFragment.ARG_OBJECT, (String)mStringArray[i]);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getCount() {
        ////
        ArrayList<String> listItem = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(context).getWritableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM ARATRIKA", new String[]{});
        while (cursor.moveToNext()) {
            listItem.add(cursor.getString(cursor.getColumnIndex("NAME")));
        }
       return listItem.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {

        //return tabTitleArray[position];
        return "OBJECT " + (position + 1);
    }
}

标签: androidandroid-fragmentsscrollandroid-viewpager

解决方案


我的问题解决了。清单中的 android:hardwareAccelerated 是“真实的”。我改成“假”

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        <application android:hardwareAccelerated="false"/>
 </manifest>

无论如何感谢你们帮助我解决问题。


推荐阅读