切换片段

时间:2013-01-03 09:34:15

标签: android android-asynctask android-fragments

我需要更改Fragment中的View pager 我在View pager adapter

中有getItem方法
    @Override
    public Fragment getItem(int position) {

    fragmentPosition = position;
    Log.e("PageAdapter", "Creating fragment at: " + position);
    DefinedValues.thumbnailInfo.put("activity", DefinedValues.images);
    DefinedValues.thumbnailInfo.put("type", "info");
    DefinedValues.thumbnailInfo.put("page", position+ 1 + "");

    String request = Json.stringToJson(DefinedValues.thumbnailInfo);
    DefinedValues.thumbnailInfo.clear();

    StringAsyncRetriever net = new StringAsyncRetriever();
    net.setListener(MyPagerAdapter.this);
    net.execute(request, DefinedValues.GET_DATA);
    Log.d(TAG, "got item @ " + position);

    fragment1 myFragment = new fragment1();
    return myFragment;
}

当AsyncTask完成显示图像的那个

时,我需要更改它

有fragment1类:

    public class fragment1 extends Fragment {

GridView gridView;
public ImageAdapter adapter;
public int position;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        //
        Bundle savedInstanceState) {

    adapter = new ImageAdapter(this.getActivity(), position);


    if (container == null) {
        Log.e("Null container", "Null");
        return null;
    }
    //Log.e("fragment1", "fragment1 reached!");
    View view =  inflater.inflate(R.layout.menu1_fragment, container, false);
    gridView = (GridView)view.findViewById(R.id.gridview1);

    gridView.setAdapter(adapter);
    DefinedValues.adapterContainer.add(adapter);
    //Log.d("Fragment1 adapter count: ", DefinedValues.adapterContainer.size() + "");


/*  gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageView imageView = new ImageView(getActivity());
            LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
                    (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(vp);

            imageView.setImageBitmap(DefinedValues.imageContainer.get(position).getThumbnail());
            getActivity().addContentView(imageView, vp);
        }


    });
    */


    return gridView;
}

那么我怎样才能在asyncTask结束时将gridView添加到片段?

1 个答案:

答案 0 :(得分:0)

您应该让ViewPager处理片段的更改。您可以在getItem中返回正确的片段。只需隐藏该片段中的所有内容,并可选择在FragmentDummy中呈现您正在执行的其他内容。然后,将片段传递给AsyncTask,或者在AsyncTask中通过id / tag获取片段。然后,您可以在任务完成后在片段中显示正确的内容视图,并隐藏片段的虚拟部分。