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()
Related
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...
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();
}
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
}
});
I'm trying to make a fragment to display tips for the user. The fragment's createView method returns a ViewPager and setup the PagerAdapter through AsyncTask. This fragment is added dinamically in the FragmentActivity when the user press a button. If the user reaches the last item of press the same button again, this fragment is removed from the FragmentActivity. It's important to note that I add the fragment to the FrameLayout idenfified by android.R.id.content.
Ok, the first time I press the button, it works as expected. But the second time I press the button, the ViewPager doesn't appear. When I use the View Hierarchy to see what's happenning, I see the ViewPager in the right place, but with no items. I debugged and noted that the getCount() of the PagerAdapter is called, but the getItem is never called.
This is the createView method of my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tipsView = inflater.inflate(R.layout.tip_manager_layout, container, false);
this.tipPagerAdapter = new TipPagerAdapter(getFragmentManager(), getArguments().getIntArray(TIP_LAYOUT_IDS_KEY));
this.viewPager = (ViewPager) tipsView.findViewById(R.id.tip_pager);
this.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
if(position == (tipPagerAdapter.getCount()-1)){
stop();
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
// Workaround to set the PagerAdapter within a fragment transaction
new SetViewPagerAdapterTask().execute();
return tipsView;
}
I add the fragment this way:
FragmentTransaction fragmentTransaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(android.R.id.content, this, TAG);
fragmentTransaction.commit();
And remove the fragment this way:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(tipManagerFragment);
fragmentTransaction.commit();
This is the layout inflated by the fragment:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tip_pager"
android:layout_width="fill_parent"
android:layout_height="175dp"
android:layout_gravity="center_horizontal|bottom"
android:paddingBottom="2dp" />
Could someone help me with this problem?
EDIT:
For a while, I'm using the show and hide methods of the fragment after adding it to the activity. But it's not the correct approach. I can't understand why I can't add again a fragment with a ViewPager. The ViewPager is added and removed correctly, but the second time, it simply doesn't show anything.
After a long time thinking about my problem, I had the following idea: maybe the problem is related to the fact that I'm removing the fragment that has the ViewPager, but I'm not removing the fragments used by the ViewPager. Then, these fragments continues in the FragmentManager and something strange happens when the new ViewPager tries to use them.
So, I started trying to remove all the fragments. To do this, I created a method in my FragmentPagerAdapter this way:
public void destroy(FragmentTransaction fragmentTransaction){
for(Fragment tipFragment : tipFragments){
fragmentTransaction.remove(tipFragment);
}
}
The adapter is using the tipFragments to create its content. So, I get a fragmentTransaction as parameter and I remove all these fragments. When I want to remove my fragment, I use this code:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
tipPagerAdapter.destroy(fragmentTransaction);
fragmentTransaction.remove(TipManagerFragment.this);
fragmentTransaction.commit();
This way, I remove all the fragments and when I add the fragment which has the ViewPager again, everything works well.
I am making an application for android. I have a Activity and in it two fragmentList. When moving the DPAD left or right, I want to control what position you are positioned in the Fragmentlist below. For example, want to go to position 4 of the first fragmentlist at position 8 of the siguiende press Right on the DPAD.
Try to explain better:
I am making an application for GoogleTV, so keyboard handling is very important. I has two fragmentlist with items. OnItemSelectedListener I'm using so that when a item has the focus, also is pressed. When the fragmentlist1 changes position, the fragmentList2 change. When I'm at the Item 3 in FragmentList2, then I left key pulse. I want to return to item 1 of fragmentLis1. The default is the item 3 for fragmentList1.
Added the code of fragment 1.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
listView.setItemChecked(position, true);
listView.setSelection(position);
changeItem(position);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
listView.setItemChecked(position, true);
listView.setSelection(position);
changeItem(position);
}
public void changeItem(int position){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment1 f1 = new Fragment1();
Fragment2 f2 = new Fragment2();
switch (position) {
case 0:
transaction.replace(R.id.second_fragment, f1);
transaction.addToBackStack(null);
transaction.commit();
break;
case 1:
transaction.replace(R.id.second_fragment, f2);
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}
would something like nextfocusleft between Fragmentlists?
Thanks in advance.
In the Activity, I'd create a member variable for the position in FragmentList1, and wrap it with a getter function that can handle any defaults or special business logic.
Then, I'd create an onKeyUp listener, and if I detect a LEFT from FragmentList2, intercept it and modify as needed.