2

我在左侧Fragment Activity上有一组按钮。我还创建了两个片段,webviewViewPagerFragment带有 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;
    }



}

}

任何帮助,将不胜感激。

谢谢你

4

1 回答 1

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));
        }
    }
}

无论如何....谢谢您的所有支持:)

于 2013-01-05T05:16:22.927 回答