Fragmentation Guidelines for Multiple Lists

I plan to have 3 fragmented files contained in one action. The goal is that you select a conversation option from the first list, then go to the execution list based on what you clicked on the conversation list and then on the start list, depending on what you click, it will go to the final list filming. Should this happen in the fragments themselves (for example, I have one) or cause an operation to process the transmitted data back and forth for the fragments?

public class OptionsActivity extends Activity { protected TalkFragment talk; protected RunFragment run; protected EatFragment eat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); talk = new TalkFragment(); run = new RunFragment(); eat = new EatFragment(); } } public class TalkFragment extends ListFragment { private Cursor mCursor; int mCurCheckPosition = 0; @Override public void onActivityCreated(Bundle savedState) { super.onActivityCreated(savedState); } @Override public void onListItemClick(ListView l, View v, int pos, long id) { mCurCheckPosition = pos; // We can display everything in-place with fragments. // Have the list highlight this item and show the data. getListView().setItemChecked(pos, true); // Check what fragment is shown, replace if needed. RunFragment run_frag = (RunFragment) getFragmentManager().findFragmentById(R.id.fragment_run); if (run_frag == null || run_frag.getShownIndex() != pos) { run_frag = RunFragment.newInstance(pos); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.details, details); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } } 

These are obviously just fragments, but you get the idea. If I do this, I'm not sure how to pass some parameters correctly. Ideally, RunFragment will know what to display based on the identifier of the item clicked in TalkFragment. Should they go through Activity?

+4
source share
1 answer

As I usually do this, the Activity needs to be a traffic coordinator for processing fragments. In your onListItemClick implementation, you can simply indicate the activity what it wants to do:

 public class OptionsActivity extends Activity { protected TalkFragment talk; protected RunFragment run; protected EatFragment eat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); talk = new TalkFragment(); run = new RunFragment(); eat = new EatFragment(); } public void showRunFragment() { showFragment(R.id.fragment_run); } public void showEatFragment() { showFragment(R.id.fragment_eat); } public void showFragment(int fragmentId) { // Check what fragment is shown, replace if needed. ... } } public class TalkFragment extends ListFragment { private Cursor mCursor; int mCurCheckPosition = 0; @Override public void onActivityCreated(Bundle savedState) { super.onActivityCreated(savedState); } @Override public void onListItemClick(ListView l, View v, int pos, long id) { mCurCheckPosition = pos; // We can display everything in-place with fragments. // Have the list highlight this item and show the data. getListView().setItemChecked(pos, true); getActivity().showRunFragment() } } 
+2
source

Source: https://habr.com/ru/post/1394764/


All Articles