在StackOverflow中彻底搜索后...... 我正在使用ViewPager来显示片段,每个片段都应该显示它的页面。 例如: |的 0 |的 1 |的 2 | 3 |的 4 |的 5 | 但我得到的不是正确的顺序,有时候没有显示任何东西。 例如: | 1 | -swipe right- | < 空屏幕> | -swipe right- |< 空屏幕> | -swipe left- |的 0 |
Heres我的代码: MainActivitiy.java:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.calendarGridPager);
mPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
}
}
MyFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment{
private static final String ARG_PAGE = "page";
private int mPageNumber;
private TextView mTextView;
public static MyFragment newInstance(int pageNumber) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mPageNumber = getArguments().getInt(ARG_PAGE);
return inflater.inflate(R.layout.calendar_month_grid, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTextView = (TextView) getActivity().findViewById(R.id.fragmentDataTextView);
mTextView.setText(String.valueOf(mPageNumber));
}
public int getPageNumber() {
return mPageNumber;
}
}
MyFragmentPageAdapter.java:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private static final int NUM_ITEMS = 6;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment item = (Fragment) MyFragment.newInstance(position);
return item;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return super.isViewFromObject(view, object);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
}
activity_main.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"
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="com.example.trashproject.MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/calendarGridPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
calendar_month_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/fragmentDataTextView"
android:textSize="80sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:1)
我得到了解决方案您只需更改onCreateView(....)
MyFragment
就好了。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root= inflater.inflate(R.layout.calendar_month_grid, container, false);
mPageNumber = getArguments().getInt(ARG_PAGE);
System.out.println("in onCreateView: "+mPageNumber);
mTextView = (TextView)root.findViewById(R.id.fragmentDataTextView);
mTextView.setText(String.valueOf(mPageNumber));
return root;
}
并从onActivityCreated(....)