如何用viewpager片段替换片段

时间:2013-01-03 12:37:32

标签: android android-fragmentactivity

左侧有一组按钮片段活动。我还创建了两个片段webviewViewPagerFragment,其中包含3个ImageFragments。 问题是,当我尝试用Viewpager替换webview 时,我再也无法刷卡,只显示1 ImageFragment。我再也无法扫过它们了。

这是我的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;
    }



}

}

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

我解决了这个......

您必须将ViewPager转换为片段。我知道它是一个ViewGroup但仍然。 然后,您可以使用onclick中的标准替换方法来替换片段。

以下是包含更改的代码。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    l.setItemChecked(position, true);
    Fragment df = DetailsFragment.newInstance();
    Fragment page = FragmentPagerAdapterClass.newInstance();

    if (listener != null) {
        if (position == 0) {
            Log.d(TAG, "Position = " + position);


            getFragmentManager().beginTransaction().remove(DetailsFragment.newInstance());
            getFragmentManager().beginTransaction()
            .replace(R.id.layout_detail, FragmentPagerAdapterClass.newInstance()).commit();
            getFragmentManager().executePendingTransactions();

        } else {
            Log.d(TAG, "Position = " + position);

            McDonalds c = EU.get(position);
            String url = getString(c.url);

            new DetailsFragment().setUrl(url);

               getFragmentManager().beginTransaction().remove(FragmentPagerAdapterClass.newInstance());
            getFragmentManager().beginTransaction()
                .replace(R.id.layout_detail, DetailsFragment.newInstance()).commit();
            getFragmentManager().executePendingTransactions();

            // listener.onCountrySelected(EU.get(position));
        }
    }
}

无论如何......对于你所支持的所有人来说:#x。