片段未在我的项目中加载

时间:2015-02-10 07:23:55

标签: android android-activity android-fragments android-viewpager

我正在使用带有Circle Page Indicator的ViewPager。

我正在关注此link for sample

下面我提到了我的课程详情。Folder Structure

HelpScreen.java

This Activity had one fragment called HelpScreenFragment.java

    public class HelpScreen extends Activity {

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

        HelpScreenFragment helpScreenFragment = new HelpScreenFragment();
        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction().add(R.id.frame_helpscreen_container,
                        helpScreenFragment);
        transaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.help_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

help_screen.xml

    <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"
    tools:context="${relativePackage}.${activityClass}" >

    <FrameLayout 
        android:id="@+id/frame_helpscreen_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</RelativeLayout>

SlidePagerAdapter.java  这个类扩展了PagerAdapter并用于扩充

    public class SlidePagerAdapter extends PagerAdapter {

    ImageView helpScreenImage;
    List<Integer> drawableBitmap;
    int gallery_grid_Images[] = { R.drawable.sample1, R.drawable.sample2,
            R.drawable.sample3, R.drawable.sample4, R.drawable.sample5, };

    private Context mContext;

    Activity activity;

    public SlidePagerAdapter(Activity mContext) {
        super();
        this.activity = mContext;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.viewpager_layout, container,
                false);
        helpScreenImage = (ImageView) view.findViewById(R.id.helpscreen_image);
        helpScreenImage.setImageResource(gallery_grid_Images[position]);
        ((ViewPager) container).addView(helpScreenImage);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((View)object);
    }

}

viewpager_layout.xml

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

    <ImageView
        android:id="@+id/helpscreen_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

HelpScreenFragment.java

public class HelpScreenFragment extends Fragment {

ViewPager helpScreenPager;

CirclePageIndicator mCirclePageIndicator;

SlidePagerAdapter mSlidePagerAdapter;

Activity activity;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.helpscreen_fragment, container,
            false);

    initializeWidgets(view);


    mSlidePagerAdapter = new SlidePagerAdapter(activity);

    helpScreenPager.setAdapter(mSlidePagerAdapter);
    mCirclePageIndicator.setViewPager(helpScreenPager);

    return view;

}

private void initializeWidgets(View view) {

    helpScreenPager = (ViewPager) view.findViewById(R.id.pager);

    mCirclePageIndicator = (CirclePageIndicator) view
            .findViewById(R.id.page_indicator);
}

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

}

}

在helpscreen_fragment.xml中只包含ViewPager和CirclePageIndicator

helpscreen_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.viewpagerdemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--
         <ViewFlipper
        android:id="@+id/helpscreen_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /> -->

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

    <com.example.viewpagerdemo.CirclePageIndicator
        android:id="@+id/page_indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_pager"
        android:padding="10dip" />

</FrameLayout>

执行项目时,我只得到空白屏幕。 我做错了什么?必须做出哪些改变。 有人可以帮忙吗?

  [1]: http://androidopentutorials.com/android-image-slideshow-using-viewpager/
  [2]: http://i.stack.imgur.com/pSold.jpg

1 个答案:

答案 0 :(得分:1)

下面

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

目前getCount SlidePagerAdapter方法重新调整0,因此ViewPager中无法显示任何项目。从getCount()返回gallery_grid_Images数组大小:

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