如何在First Fragment上单击任何Gridview视图项时,在同一活动中加载另一个Fragment

时间:2016-09-01 11:38:11

标签: android android-fragments

我正在开发一个Android应用程序。我正在活动中的片段内加载一些视图。 First Fragment正在使用Gridview / ListView。我想在单击Listview / Gridview的任何项目时在同一活动中加载另一个Fragment,而不重新启动活动。

那么,我怎么能这样做,有人可以帮助我。

非常感谢先进。

3 个答案:

答案 0 :(得分:0)

片段代码:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {



                        YourFragment yf= new YourFragment ();



                      ((HomeActivity) mActivity).replceFrament(groupControlFragment);




        }
    });

MainActivity Code:

public void replceFrament(Fragment fragment){

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment,tag).addToBackStack("back").commit();

}

答案 1 :(得分:0)

这是一个例子。这不是一个确切的答案,只是为了暗示。

Fragment类可能如下所示:

public class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;

    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(position);
    }

}

活动课程如下:

public static class DetailsActivity extends Activity implements FragmentA.OnArticleSelectedListener{

    @Override
    public void onArticleSelected(int position){
        // your implementation ...
    }
}

答案 2 :(得分:0)

在您的片段中包含listview / gridview。

Fragment[] fragment=new Fragment{new MyFragment1(), new MyFragment2(), new MyFragment3()};

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

       View view=inflater.inflate(R.layout.mylayout, container, false);
       ListView listView=(ListView)view.findViewById(R.id.list_id);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    replaceFragment(fragment[i]);
                }
            });
      return view;
}

public void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.containerLayout,fragment);
        fragmentTransaction.addToBackStack(null);           //Optional
        fragmentTransaction.commit();
}