android-fragments ViewPager未显示任何内容- android

ztyzrc3y  于 2022-11-14  发布在  Android
关注(0)|答案(5)|浏览(179)

我需要一些建议。ViewPager只显示白色页面。我已经搜索了很多,但我无法找到我的情况下的解决方案。
我试着做一个简单的按钮,但是我得到了相同的输出。我认为这不是图像问题。这是我的代码。

public class IntroduceArticleFragment extends Fragment {

    View view;
    String depart;
    String path;
    int num;

    CustomPagerAdapter customPagerAdapter;
    ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_introduce_article, container, false);
        view.setBackgroundColor(Color.WHITE);

        depart = getArguments().getString("depart");
        create();

        //customPagerAdapter = new CustomPagerAdapter(getActivity());
        viewPager = (ViewPager)view.findViewById(R.id.pager);
        viewPager.setAdapter(customPagerAdapter);

        return view;
    }

    void create(){
        DBHelper dbHelper = new DBHelper(getActivity());
        path = dbHelper.getContentsPath(depart);
        num = dbHelper.getNum(depart);

        customPagerAdapter = new CustomPagerAdapter(getActivity());
        customPagerAdapter.mResources = new int[num];
        for(int i=0;i<num;i++) {
            String uri = "@drawable/" + path + "_" + Integer.toString(i);
            int imageResource = getActivity().getResources().getIdentifier(uri, "drawabale", getActivity().getPackageName());
            customPagerAdapter.mResources[i] = imageResource;
        }
    }
}

public class CustomPagerAdapter extends PagerAdapter {
    Context mContext;

    public int[] mResources;

    public CustomPagerAdapter(Context context) {
        super();
        mContext = context;
    }

    @Override
    public int getCount() {
        return mResources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = mLayoutInflater.inflate(R.layout.pager_item_0, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0);
        imageView.setImageResource(mResources[position]);

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }

    @Override
    public void startUpdate(ViewGroup container) {
        super.startUpdate(container);
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
    }
}

这是ArticleFragment的xml代码

<?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="match_parent">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:visibility="visible">
                </android.support.v4.view.ViewPager>

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

这是XML代码

<?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="match_parent">

        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:id="@+id/pagerimageView_0" />
    </LinearLayout>
7dl7o3gd

7dl7o3gd1#

好的...首先你必须将ScrollView的子节点的android:layout_height设置为wrap_content
第二,考虑到你不能将ViewPagerandroid:layout_height设置为wrap_content。除非你使用自定义的ViewPager。例如,你可以检查this answer
通过在ScrollView中将android:fillViewport设置为true,ScrollView的子对象的高度将自动设置为match_parent,直到其高度高于屏幕。
我不明白为什么你把ViewPager的高度设置为0dp,并期望它能显示一些东西!
现在,您的布局应该如下所示:

<?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="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_pranet"
                android:visibility="visible">
            </android.support.v4.view.ViewPager>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
xzv2uavs

xzv2uavs2#

我花了半天时间解决这个问题,发现我的ViewPager与我的应用程序中另一个模块中的另一个ViewPager具有相同的ID,所以我只是给了它另一个ID。

zpqajqem

zpqajqem3#

你完全搞错了,用这个代码

import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;

    public class CustomPagerAdapter extends FragmentStatePagerAdapter {

       public int[] mResources;

        public NewsAdapter(FragmentManager fm, int[] mResources) {
            super(fm);

            this.mNumOfTabs = mResources.length();
            this.mResources = mResources;
        }


        @Override
        public Fragment getItem(int position) {
            return ContentFragment.newInstance(mResources[position]);
        } 

        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }


public class ContentFragment extends Fragment  {

    private Context context;

    private int resource_id;

    public static ContentFragment newInstance(int resourdid) {
        Bundle args = new Bundle();
        args.putInt("resource_id", resourdid);
        ContentFragment fragment = new ContentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resource_id   = getArguments().getInt("resource_id");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(in.newswallet.R.layout.content_row, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initalize();
    }

    private void initalize(){
        View itemView = getView();
        ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0);
        imageView.setImageResource(resource_id);    
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }



}
czq61nw1

czq61nw14#

<android.support.v4.view.ViewPager     xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:visibility="visible">
            </android.support.v4.view.ViewPager>

尝试将**android:layout_height =“0dp”更改为某个值,如android:layout_height =“50dp”**或
为布局给予权重android:layout_weight =“1”

yyyllmsg

yyyllmsg5#

变更:

container.removeView((LinearLayout) object);

具有:

container.removeView((ConstraintLayout) object);

相关问题