簡體   English   中英

當在第一個片段上單擊任何 Gridview 視圖項時,如何在同一活動中加載另一個片段

[英]How to load another Fragment in the same activity when any Gridview view item get clicked on First Fragment

我正在開發一個 android 應用程序。 我正在活動中的片段內加載一些視圖。 第一個片段在這個中有 Gridview/ListView。 當單擊 Listview/Gridview 的任何項目時,我想在同一活動中加載另一個 Fragment,而無需重新啟動活動。

我該怎么做?

片段代碼:

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




        }
    });

主要活動代碼:

公共無效replceFrament(片段片段){

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

}

這是一個例子。 這不是一個確切的答案,只是一個提示。

片段類可能如下所示:

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 ...
    }
}

在具有列表視圖/網格視圖的片段中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM