如何在另一个片段的tabhost中单击Gridview后替换当前片段?

时间:2015-01-18 01:04:13

标签: android gridview android-fragments

我有这个问题。我有一个带有ViewFlipper的Fragment和一个带有三个标签的TabHost,在第一个标签中我有一个Gridview with Items。我想通过单击gridView的项目将当前片段替换为另一个片段。 我写的相关示例代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.princ_fragment_2, container,
            false);


    //mTabHost = rootView.getTabHost();
    mTabHost = (TabHost)rootView.findViewById(R.id.tabhost);
    mTabHost.setup();
    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Turnario").setContent(R.id.gridView));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("CarSharing").setContent(R.id.textview2));
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Altro").setContent(R.id.textview3));
    mTabHost.setCurrentTab(0);


    mContext = this.getActivity();
    mViewFlipper = (ViewFlipper) rootView.findViewById(R.id.ViewFlipper01);
    mViewFlipper.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }
    });

    **gridView = (GridView) rootView.findViewById(R.id.gridView);
    customGridAdapter = new GridViewAdapter(this.getActivity(), R.layout.row_grid, getData());
    gridView.setAdapter(customGridAdapter);

    gridView.setOnItemClickListener(new GridView.OnItemClickListener() {


        ***public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
               Toast.makeText(getActivity(),null, Toast.LENGTH_SHORT).show();
            Log.v(TAG,"GRIDVIEW ITEM TOUCH");
            }
        });***
    return rootView;
}**

我调试了这段代码,但onItemClick()事件没有执行。

谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

if(position == Constants.GRID_POSITION_ZERO){

                FragmentHelper fragmentHelper = FragmentHelper.getInstance();

                fragmentHelper.replaceFragment(R.id.fragment_holder, new LiveEventFragment(), ((HomeActivity) getActivity()), "Live_Event");
       }

fragment helper是一个可以管理所有片段操作的类