【问题标题】:ViewPager not showing anything - androidViewPager 没有显示任何东西 - android
【发布时间】:2017-02-05 03:49:26
【问题描述】:

我需要一些建议。 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>

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    好的...首先您必须将ScrollView 的孩子的android:layout_height 设置为wrap_content

    其次,考虑到不能将ViewPagerandroid:layout_height 设置为wrap_content。除非您使用自定义 ViewPager。例如,您可以查看this answer

    通过在ScrollView 中将android:fillViewport 设置为true,ScrollView 的Child 的高度会自动设置为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>
    

    【讨论】:

      【解决方案2】:

      我花了半天时间解决它,并发现我的 ViewPager 与我应用程序中另一个模块中的另一个 ViewPager 具有相同的 id,所以我只是给了它另一个 id。我不知道为什么它很重要,但就是这样。

      【讨论】:

        【解决方案3】:

        你错了,使用这个代码

            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;
            }
        
        
        
        
        
        }
        

        【讨论】:

        • 如果您能提及为什么 OP“搞错了”并解释为什么您的解决方案有效,那就太好了!
        • 我知道我处理 mResources 的方式有些问题。但我不明白为什么需要扩展片段。你能帮我更多吗?
        【解决方案4】:
        <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"

        【讨论】:

        • 谢谢。我解决了问题。
        【解决方案5】:

        变化:

        container.removeView((LinearLayout) object);
        

        与:

        container.removeView((ConstraintLayout) object);
        

        【讨论】:

        • 尼尼科。谢谢,但请务必在您的答案中添加解释。 How to Answer。亲切的问候。
        • 哦,是的,对不起,但我英语不好:(
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多