带有PagerAdapter / Fragments的ViewPager没有显示任何内容

时间:2016-07-11 12:36:51

标签: android android-viewpager fragment

在SDK 23下面的Android上的FragmentActivity中,许多片段将作为事务添加。 其中一个内部应该出现一些简单的ViewPager,用于一些图像。 “images Fragment”创建一个带有自定义数组列表的PagerAdapter。 但在下面的情况中,ViewPager中没有呈现View。 没有其他线索和研究帮助过我。所以我在这里尝试了代码, 也许有人可以帮忙。 我的身份:错误的背景,隐藏的内容,错误的视图回归,我没有发现任何东西,并尝试了很多。 将调用instantiateItem(),不会调用destroy和remove方法,并显示带有红色背景的空小部件。香港专业教育学院尝试过更新列表或重新发布寻呼机的功能。原始代码不使用图像资源,但也有textView!

主要活动XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="mw.imageslidertest.MainActivity"
    android:id="@+id/fragmentcontainer">
</RelativeLayout>

MainActivity.java

package mw.imageslidertest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(findViewById(R.id.fragmentcontainer) != null) {
            Fragment container = new images();
            container.setArguments(getIntent().getExtras());
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragmentcontainer, container);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }
}

Fragment命名为“images”XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="medianetwork24.imageslidertest.images">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:visibility="visible"
        android:layout_gravity="center_horizontal"
        android:background="#ff0000" />
</FrameLayout>

images.java

package mw.imageslidertest;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

public class images extends Fragment {
    class myImages {
        int resource;

        public myImages() {
            this.resource = 0;
        }
    }

    ArrayList<myImages> ImagesArray = new ArrayList<>();

    public images() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_images, container, false);

        myImages anImage = new myImages();
        anImage.resource = R.mipmap.ic_launcher;
        ImagesArray.add(anImage);
        myImages anotherImage = new myImages();
        anotherImage.resource = R.mipmap.ic_launcher;
        ImagesArray.add(anotherImage);

        ViewPager mPager = (ViewPager) layout.findViewById(R.id.pager);
        ImageFrameAdapter mImageFrameAdapter = new ImageFrameAdapter(layout.getContext(), ImagesArray);
        mPager.setAdapter(mImageFrameAdapter);

        return layout;
    }

    public class ImageFrameAdapter extends PagerAdapter {
        private final LayoutInflater mInflater;
        private ArrayList<myImages> IMAGES;

        public ImageFrameAdapter(Context context, ArrayList<myImages> IMAGES) {
            super();
            Log.d("LIST", "Create ImageFrameAdapter");
            this.IMAGES = IMAGES;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

        @Override
        public int getCount() {
            return IMAGES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            Log.d("LIST", "Create a pager page instance " + view);

            View imageLayout = mInflater.inflate(R.layout.elements, view, false);

            // assert imageLayout != null;
            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            myImages res = IMAGES.get(position);
            imageView.setImageResource(res.resource);

            view.addView(imageLayout, 0);

            Log.d("LIST", "Created View: " + view);

            return view;
        }

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

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }
}

元素布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:padding="1dip" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        android:scaleType="centerCrop" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HEELLLOEOOEIFOISDPOIDOPIDOPSIODPIP"
        android:id="@+id/textViewId"
        android:layout_gravity="center" />
</FrameLayout>

只有寻呼机小部件的红色背景可见。 没有别的,不是textView我的图像。 THX

0 个答案:

没有答案
相关问题