我在左侧Fragment Activity上有一组按钮。我还创建了两个片段,webview
并ViewPagerFragment
带有 3 个ImageFragments
。问题是当我尝试用 Viewpager 替换 webview 时,我无法再滑动,只ImageFragment
显示 1 个。我不能再扫过它们了。
这是我的 ImageFragment
public class ImageFragment extends Fragment {
private int imageResourceId;
OnClickListener listener;
public MyAdapter mAdapter;
public ViewPager mPager;
public ImageFragment(){
}
public static Fragment newInstance() {
// TODO Auto-generated method stub
ImageFragment mg = new ImageFragment();
return mg;
}
public ImageFragment(int imageResourceId) {
this.imageResourceId = imageResourceId;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "ImageFragment");
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_layout, container, false);
View view2 = inflater.inflate(R.layout.pagerview,null);
mPager = (ViewPager) view.findViewById(R.id.pager);
Log.d("ImageFragment","mPager = "+mPager.toString());
mAdapter = new MyAdapterDummy().new MyAdapter(getFragmentManager());
new setAdapterTask().execute();
ImageView imageView1 = (ImageView) view.findViewById(R.id.imageView1);
ImageView imageView2 = (ImageView) view.findViewById(R.id.imageView2);
ImageView imageView3 = (ImageView) view.findViewById(R.id.imageView3);
ImageView imageView4 = (ImageView) view.findViewById(R.id.imageView4);
ImageView imageView5 = (ImageView) view.findViewById(R.id.imageView5);
ImageView imageView6 = (ImageView) view.findViewById(R.id.imageView6);
imageView1.setImageResource(imageResourceId);
imageView2.setImageResource(imageResourceId);
imageView3.setImageResource(imageResourceId);
imageView4.setImageResource(imageResourceId);
imageView5.setImageResource(imageResourceId);
imageView6.setImageResource(imageResourceId);
return view;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
protected void onPostExecute(Void result) {
mPager.setAdapter(mAdapter);
}
}
这是适配器
public class MyAdapterDummy extends FragmentActivity {
static String TAG = "ViewPagerExample";
MyAdapter mAdapter;
ViewPager mPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager mgr = getSupportFragmentManager();
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
public int getCount() {
return 3;
}
public Fragment getItem(int position) {
Log.d(TAG, "Item Position : " + position);
switch (position) {
case 0:
return new ImageFragment(R.drawable.ic_launcher);
case 1:
return new ImageFragment(R.drawable.ic_launcher);
case 2:
return new ImageFragment(R.drawable.ic_launcher);
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
String str = "Top Listings";
return str;
}
}
}
任何帮助,将不胜感激。
谢谢你