locked
i want to open a fragment as a detailed one from another fragment "listView" RRS feed

  • Question

  • User371882 posted

    Hi, i have a problem and i hope someone can help me. I have a fragment containing listView and i coded onItemClick to replace the FrameLayout " which contains the listView " with another fragment , and it works fine and the listview is not visible but when i click on an empty area i found that the list is still here but just hidden. what i need is i want to open a detailed fragment from the listView.

    here is part of my code from the AFragment " the one contains the listView ": @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_A, container, false);

    final ListView listView = view.findViewById(R.id.list);
    final ArrayList<ItemInList> items = new ArrayList<ItemInList>();
    items.add(new ItemInList("cat", 0));
    items.add(new ItemInList("dog", 1));
    items.add(new ItemInList("cow", 2));
    items.add(new ItemInList("mice", 3));
    
    ItemAdapter itemAdapter = new ItemAdapter(getActivity(), items);
    
    listView.setAdapter(itemAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    
    Fragment fragment = new FragmentB();
    AFragment.this.getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.AFragContainer, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack(null).commit();
    
    }
    });
    
    return view;
    

    thanks in advance

    Monday, August 6, 2018 12:54 PM

Answers

  • User370268 posted

    This might help you.

        NextFragment nextFrag= new NextFragment(); //NextFragmetn is you detail or BFragmnent.
             getActivity().getSupportFragmentManager().beginTransaction()
             .replace(R.id.Layout_container, nextFrag,"findThisFragment")
             .addToBackStack(null)
             .commit();
    

    Can you go through this link, it might be helpful to you,

    https://stackoverflow.com/questions/8163104/android-replace-the-current-fragment-with-another-fragment

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 7, 2018 6:02 AM

All replies

  • User370268 posted

    This might help you.

        NextFragment nextFrag= new NextFragment(); //NextFragmetn is you detail or BFragmnent.
             getActivity().getSupportFragmentManager().beginTransaction()
             .replace(R.id.Layout_container, nextFrag,"findThisFragment")
             .addToBackStack(null)
             .commit();
    

    Can you go through this link, it might be helpful to you,

    https://stackoverflow.com/questions/8163104/android-replace-the-current-fragment-with-another-fragment

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 7, 2018 6:02 AM
  • User371882 posted

    @NightFury thanks so much for your answer, i did that exactly and it worked just fine for opening the other fragment but as i said in the question the problem was when the new fragment opened the buttons on the previous fragments is hidden but still touchable, so i fixed this by adding listView.setVisibility(view.GONE); and then in the activity which hold the fragments i added the override onBackPressed so i can unhide the listView in the previous fragment again when the user press back " Note that in the main activity i have a TabLayout and viewPager with 3 tabs a , b and c each has a listView in it "as follow: @Override public void onBackPressed() { ListView aListView = findViewById(R.id.alist); ListView bListView = findViewById(R.id.blist); ListView cListView = findViewById(R.id.c_List);

            int count = getFragmentManager().getBackStackEntryCount();
            if (count == 0) {
                Toast.makeText(this, "count = " +  count, Toast.LENGTH_LONG).show();
                super.onBackPressed();
                //additional code
                View view = null;
                aListView.setVisibility(view.VISIBLE);
                bListView.setVisibility(view.VISIBLE);
                cListView.setVisibility(view.VISIBLE);
            } else {
                Toast.makeText(this, "Othercount = " +  count, Toast.LENGTH_LONG).show();
                getFragmentManager().popBackStack();
            }
        }
    

    , Now when i click back on the new fragment so i should return to the listView again it works fine with a and b , but with the c fragment when i press back the app gets an error. if you need more details on my code please tell me and thanks for your help :smiley:

    Wednesday, August 8, 2018 7:23 AM
  • User370268 posted

    @AhmedFaid Happy to help you.

    Cheer's Happy coding.

    Wednesday, August 8, 2018 9:43 AM
  • User370268 posted

    @AhmedFaid said: @NightFury thanks so much for your answer, i did that exactly and it worked just fine for opening the other fragment but as i said in the question the problem was when the new fragment opened the buttons on the previous fragments is hidden but still touchable, so i fixed this by adding listView.setVisibility(view.GONE); and then in the activity which hold the fragments i added the override onBackPressed so i can unhide the listView in the previous fragment again when the user press back " Note that in the main activity i have a TabLayout and viewPager with 3 tabs a , b and c each has a listView in it "as follow: @Override public void onBackPressed() { ListView aListView = findViewById(R.id.alist); ListView bListView = findViewById(R.id.blist); ListView cListView = findViewById(R.id.c_List);

            int count = getFragmentManager().getBackStackEntryCount();
            if (count == 0) {
                Toast.makeText(this, "count = " +  count, Toast.LENGTH_LONG).show();
                super.onBackPressed();
                //additional code
                View view = null;
                aListView.setVisibility(view.VISIBLE);
                bListView.setVisibility(view.VISIBLE);
                cListView.setVisibility(view.VISIBLE);
            } else {
                Toast.makeText(this, "Othercount = " +  count, Toast.LENGTH_LONG).show();
                getFragmentManager().popBackStack();
            }
        }
    

    , Now when i click back on the new fragment so i should return to the listView again it works fine with a and b , but with the c fragment when i press back the app gets an error. if you need more details on my code please tell me and thanks for your help :smiley:

    can you please post exception that your getting. ?

    Wednesday, August 8, 2018 9:46 AM