Android ViewPager没有显示内容

时间:2016-11-07 03:07:02

标签: java android xml android-fragments android-viewpager

我是Android新手,目前正尝试使用ViewPager和FragmentStatePagerAdapter来实现图像滑块。寻呼机位于片段内,因此我使用嵌套片段。 问题是,ViewPager似乎没有可见的内容。适配器使用imageView创建2个片段,但在测试应用时,它们不会显示出来。我用谷歌搜索了自己,我无法找到解决方案。

有什么想法吗?

代码:

ShowListActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ShowListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_list);
    }

}

activity_show_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_show_list"
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="se.ju.taun15a16.group5.mjilkmjecipes.ShowListActivity">


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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Relative_recipe_entry"
        android:background="#ffffff"
        android:layout_marginBottom="20dp">

        <TextView
            android:text="@string/text_show_list_recipe_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView_recipe_name" />

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ratingBar"
            android:layout_below="@+id/textView_recipe_name"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="12dp"
            android:stepSize="1"
            android:numStars="5" />

        <TextView
            android:text="@string/text_signup_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView_username"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/ratingBar"
            android:layout_alignEnd="@+id/ratingBar" />

    </RelativeLayout>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="se.ju.taun15a16.group5.mjilkmjecipes.CommentFragment"
        android:id="@+id/fragment"
        tools:layout="@layout/recipes_comment" />

</LinearLayout>
</ScrollView>

CommentFragment.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
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;

public class CommentFragment extends Fragment {

    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private static final int IMAGE_LIMIT = 10;

    public CommentFragment() {
        Log.d("GUI","Created CommentFragment");
    }

    public static CommentFragment newInstance() {
        CommentFragment fragment = new CommentFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

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

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPager = (ViewPager) view.findViewById(R.id.imagePager);
        mPager.setBackgroundColor(Color.RED);
        mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Log.d("GUI","GETTING ITEM");
            return new ScreenSlidePageFragment();
        }

        @Override
        public int getCount() {
            return IMAGE_LIMIT;

        }

    }
}

recipes_comment.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray">

        <TextView
            android:text="Username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/usernameLbl"
            tools:text="Username"
            android:textSize="24sp"
            android:textStyle="normal|bold" />

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/recipeRatingBar"
            android:numStars="5"
            android:rating="0"
            android:isIndicator="true" />

        <TextView
            android:text="I am a testcomment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/commentLbl"
            tools:text="I am a testcomment" />

        <android.support.v4.view.ViewPager
            android:id="@+id/imagePager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible" />
    </LinearLayout>
</ScrollView>

ScreenSlidePageFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ScreenSlidePageFragment extends Fragment {

    public ScreenSlidePageFragment() {
        Log.d("GUI","Creating SlideFragment");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup)inflater.inflate(R.layout.recipes_image_slide, container, false);
        return view;
    }
}

recipes_image_slide.xml

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/slideImage" />
</LinearLayout>

0 个答案:

没有答案