Open fragment from inside another fragment [duplicate] - android

This question already has an answer here:
Android: Opening a Fragment from another
(1 answer)
Closed 6 years ago.
I am working inside a Fragment that has a ListView. When the user clicks on a list row, I want to open another fragment that should show another ListView.
This is the method I have for now:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Log.d("Comunidad",topic);
//PASA VALOR SELECCIONADO AL SIGUIENTE FRAGMENT
}
});
What is the best way to open the new Fragment from inside this method?
Thank you.

Add a FrameLayout to your preferred activity's layout to call FragmentA (the fragment to be opened onClick):
<FrameLayout
android:layout_width="match_parent"
android:id="#+id/outer_frame"
android:layout_height="wrap_content">
</FrameLayout>
and then replace outer_frame (FrameLayout) with your FragmentA by doing this:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Log.d("Comunidad",topic);
getSupportFragmentManager().beginTransaction()
.replace(R.id.outer_frame, new FragmentA())
.commit();
}
});

inside onClick of listview
Fragment2 new_frag = new Fragment2();
return new_frag;

// Inside your onClick method
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new MyFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

I might be wrong but i think what you want to do is not possible, because fragments are not intended to be open inside an already opened activity...
What you could do is prepare the activity with the fragment and then change it's content when you want to...
Might be wrong tho...

Related

How can I change view in Fragment which is contained ViewPager?

I'm using ViewPager and TabLayout. And ViewPager is consisted with 4 fragments(for example, I'll call the fragments' name 1, 2, 3, 4). My 2(second fragment) has a ListView, and if I click a list-item, the 2(second fragment) will be changed another view. At that time, another view is not 1, 3 or 4. That is a new view.
It is purpose to make board's category(like, sports-soccer-midfielder). So when I select sport item, the view will show another list-item(like, soccer, baseball, basketball an so on). The category will be consisted with 3 step. So i will prepare second and third view of 2(second fragment).
But I don't know how to change view or fragment. I found some clue to solve this problem in google.
That is
FragmentManager fm = getFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
View2 v2 = new View2(); // View2 is another view
fmt.replace(R.id.view1, v2);
fmt.commit();
I used that code and it succeed to change view. But only a part of view is changed. What is the problem? How can I solve that?
Here is Pic of second fragment
and this is a view when I click list-item. only a part of view is changed.
(I can't attach picture directly, so someone help please ^^ )
And this is my code of second fragment.
public class MatchingFragment extends Fragment {
View2 v2;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.matching_fragment, container, false);
final MatchingListViewAdapter adapter = new MatchingListViewAdapter();
ListView listView = (ListView) view.findViewById(R.id.matching_list1);
listView.setAdapter(adapter);
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.lock), "LOCK", "This is lock icon");
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.setup), "SETUP", "This is setup icon");
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.user), "USER", "This is user icon");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
v2 = new View2();
fmt.replace(R.id.view1, v2);
fmt.commit();
adapter.notifyDataSetChanged();
}
});
return view;
}
}
Thanks.
I think you need to start a activity when you click item on listview. because if you replace new fragment with viewpager how can you go back to previous fragment. and why you create View2() object, what is the purpose of that?
if you want to replace fragment with viewpager use this code for it, don't replace viewpager by viewpager
Fragment fragment = new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.view, fragment);
ft.commit();

Show Fragment like a popup with a Transparent background

I would like to make a Fragment pop-up like an AlertDialog but with a transparent background.
My fragment looks like this:
At the moment I am doing the following:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Player player = (Player)adapterView.getItemAtPosition(i);
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(PlayerProfileFragment.newInstance(null), null);
fragmentTransaction.commit();
}
The fragment doesn't show at all. I guess in order to make it work, I should use the overload add(layoutId, fragment) but that would just place the Fragment in a specific area and not display the Fragment as a "popup".
Any ideas?
The Fragment probably doesn't show because you are not specifying to which layout you want to add it to. The version of add() that you are using, adds the Fragment to a container whose id is 0 as the docs say.
I fixed this issue by creating an interface with the method:
public void addFragment(int viewId, Fragment fragment);
I then implemented that listener in my Activity by having a FragmentTransaction inside it:
public void addFragment(int viewId, Fragment fragment){
FragmentTransaction fragmentTransaction = getSupportFragmentTransaction().beginTran....;
fragmentTransaction.add(viewId, fragment);
fragmentTransaction.commit();
}

replace nested fragment not working

So, I have a pageView with two tabs (fragments). I want to replace the content of one tab for another fragment.
So, that's what I'm trying:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment chatFragment = ChatFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.tab_chat_list_container, chatFragment).commit();
}
But nothing happens, when this code is executed, (already check on debug, it's does get called).
Add getChildFragmentManager().executePendingTransactions()

Android: Launching ListFragments as individual fragments from another activity?

I'm working on an app that uses a navigation drawer, with each menu items intended to launch a particular fragment. And each fragment must be in a list view form. Is there a way to launch the listfragments as normal fragment can be launched?
For instance:
new MyListFragment();
Is there a similar way to launch listfragments from the main activity?
Fragments are never "launched," they are added to Activities. Either add the fragment to the current activity using a FragmentTransaction (perhaps by replacing the content area), or start another activity with that fragment in it.
private void goToRadar(FragmentManager fm){
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flMain, new ChildRadarFragment());
ft.addToBackStack("radar");
ft.commit();
}
You must have a FrameLayout inside the xml-layout of your Activity (here the id = flMain).
You should do the FragmentTransactions when a menu listitem is clicked (=drawer ListView).
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int index,
long arg3) {
// Here the FragmentTransaction
}
});

Not able to update new content in webview in fragment in android

I have added two fragment in layout. One fragment is list view and one fragment is webview. In webview first showing default data. If the user clicks on list view data, I am not able to update new content in webview in fragment in android. Can anybody tell me what the problem is? How do I do this? See below my code. It's working in the emulator but not on the device.
public void onItemClick(AdapterView<?> parent, View view, int posistion,
long id) {
// TODO Auto-generated method stub
checkTablet=isTablet();
if(checkTablet)
{
webFragment=(WebViewFragment1)getSupportFragmentManager().findFragmentById(R.id.webFrag);
//webView=(WebView)getActivity().findViewById(R.id.loadWeb);
//WebViewFragment1 webViewFragment=(WebViewFragment1).findViewById(R.id.loadWeb);
//webFragment=(WebViewFragment1)getActivity().getSupportFragmentManager().findFragmentById(R.id.webFrag);
DataGenerator dataGenerator=new DataGenerator();
String data=dataGenerator.getData(posistion);
System.out.println("data is"+data);
//webView.loadData(data, "html/data", "utf-8");
webFragment.loadWeb.loadData(data, "html/data", "utf-8");
}
});
Thanks
In my opinion you need to add a fragmentTransaction, just like this:
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.webFrag, your fragment);
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commit();

Categories

Resources