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();
}
Related
I have a custom circle layout with rotation feature and 4 views as buttons.
On main_activity i have 2 layouts :
-circle layout
-relative layout(Container)
What i want is:
-when the user taps on a button from circle layout, i want the container to be changed with another layout with another views.
This is the initialization of container:
displayLayout = (RelativeLayout) findViewById(R.id.fragment_container);
Initialization of Circle layout:
circleLayout = (CircleLayout) findViewById(R.id.circle_layout);
The tap event occurs on this method:
public void onItemClick(View view) {
displayLayout.removeAllViews();
-- What should i do here? --
}
I want that the new xml (newpage.xml) to be replaced in container.
Any sugestions to be done?
Thanks a lot!
you need to do it with fragments as shown in below, to replace view
check fragments in android(https://developer.android.com/reference/android/app/Fragment.html)
public void onItemClick(View view) {
//displayLayout.removeAllViews();
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
check http://sapandiwakar.in/replacing-fragments/
check example http://notionink.wikidot.com/rajeshbabu
replace code here ,
public void onItemClick(View view) {
// displayLayout.removeAllViews();
circleLayout.setVisibility(View.Gone);
}
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()
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'm working with Fragments for the first time and running into a weird behavior. I have an activity with a Fragment covering the entire view. It contains a ListView. When I tap on an item in the ListView, I want to show a details fragment which contains information about that item.
This is how I'm presenting the new fragment view:
FragmentManager fragmentManager = this.getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
DetailsFragment fragment = new DetailsFragment();
transaction.add(R.id.viewRoot, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack("Details").commit();
R.id.viewRoot is the id of the root layout in the first Fragment's layout xml, so the new Fragment fills the entire screen.
My Problem: When the new fragment is visible and covering the entire screen, any clicks or taps which land on the background (i.e. not hitting a button or textfield on the new fragment) appear to be going through the view and landing on the first fragment. So, I tap on a ListView item, which causes the DetailsFragment to get added to the screen. Then when I tap on the background of the DetailsFragment, that tap falls through and lands on the ListView causing another DetailsFragment to be added for whatever item I happened to hit behind the scenes.
Am I adding my new fragment incorrectly? Why are my clicks/taps falling through?
EDIT for caiuspb:
public class BaseFragment extends Fragment {
public void pushNewFragment(Fragment fragment, String description) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.remove(this);
transaction.add(R.id.viewRoot, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(description).commit();
}
}
All of my fragments extend my BaseFragment class.
I experimented with the suggestions I received, but none of them worked. In the end, my solution was to add an OnTouchListener to the root view of my subfragments which discarded all touches.
View view = inflater.inflate(R.layout.mylayout, container, false);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
There is another post here on StackOverflow about that strange Fragment behaviour. Try to remove your Fragment before you add it because the replace Method seems to be buggy
edit:
Move the Fragment logic into your Activity because your activity contains your Fragments. Today I tried to use the FragmentActivity and Fragments from the Android Support Library. Now I can use the replace method withouht any bugs. This is my approach and this works:
public class DashActivity extends FragmentActivity{
...
private void changeRightFragment(Fragment fragment) {
Log.i(TAG, "Loading Fragment into container: " + fragment.getClass().toString());
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.rightfrag, fragment);
// Add a transition effect
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Add the Fragment to the BackStack so it can be restored by
// pressing the Back button
ft.addToBackStack(null);
ft.commit();
}
...
public void setUserFragment(User user) {
UserFragment fragment = new UserFragment(user);
changeRightFragment(fragment);
}
So if you want to invoke a changeFragment method from inside your Fragment use a callback mechanism to the Activity