Android fragment replacement - android

I have a fragment called ProjectsFragment, that has a constraintLayout, recyclerView and a FloatingActionButton.
I'm trying to make it when I click the button to go to another fragment.
The new fragment is called NewProjectFragment.
The code that I've got so far is below but it's not being replaced. I know the code is being ran as the log is printed
FloatingActionButton fab = view.findViewById(R.id.floatingActionButton);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NewProjectFragment npf = new NewProjectFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.constraintLayout, npf);
transaction.addToBackStack(null);
transaction.commit();
Log.d("swap", "swap");
}
});
The tabs are setup using the below on the MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
The SimpleFragmentPageAdapter is below
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public SimpleFragmentPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
// This determines the fragment for each tab
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new ProjectsFragment();
} else if (position == 1) {
return new ColoursFragment();
} else if (position == 2) {
return new WishListFragment();
} else if (position == 3) {
return new AboutFragment();
} else {
return new ProjectsFragment();
}
}
// This determines the number of tabs
#Override
public int getCount() {
return 4;
}
// This determines the title for each tab
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
switch (position) {
case 0:
return mContext.getString(R.string.project_tab);
case 1:
return mContext.getString(R.string.colours_tab);
case 2:
return mContext.getString(R.string.wish_list_tab);
case 3:
return mContext.getString(R.string.about_tab);
default:
return null;
}
}
}
The ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" />
</LinearLayout>
And the fragment_projects.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.ProjectsFragment"
android:id="#+id/constraintLayout" >
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:src="#drawable/ic_add_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

transaction.replace(R.id.constraintLayout, npf);
Looks like you're trying to replace into a constraint layout, which smells funny to me. Usually you have a FrameLayout that is a simple container for the fragments. Thus, my guess would be that you are moving the new fragment into the existing fragment's layout, without constraints, so it's not showing up and the first one remains in place.
Make sure you're using the ID of the view in the layout that contains the first fragment (again, usually a FrameLayout in the owning Actvity layout).
If that is not your issue, post your XML file.
Update.
Yeah, as I guessed, you're replacing into a ConstraintLayout and that's not a good idea, as you've seen. Your setup is a bit weird because you have a ViewPager that is managing one set of fragments and you want to push a new one over everything?
I'd suggest you just launch a new Activity instead.

Related

Android - One activity multiple fragments for tabs screen

I want to create an app. First page is a login page, then second page is a tabbad screen. I use BottomNavigationView to create a tab bar.
I want to give toolbar to each page. So, I put a Toolbar in fragment's layout.
But I have some problems:
If I add bottom navigation view to activity, I cannot use any transition animation from login to tab bar page.
If I add bottom navigation view to fragment and replace fragment by using childFragmentManager when bottom navigation view item is selected, childFragmentManager doesn't hide parent fragment's toolbar.
For second case, parent Fragment toolbar overlays child fragment toolbar.
getChildFragmentManager().beginTransaction().replace(R.id.rootFragmentLayout, fragment).commit();
Fragment layout is below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:orientation="vertical"
android:id="#+id/rootFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/toolBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolBarLayout"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
</LinearLayout>
</RelativeLayout>
If i understood it correctly, one Activity will house multiple fragments with only one toolbar. What you can do is use ViewPager to change between fragments. You'll need a ViewPager on your xml below your toolbar and then you can create a adapter that switches fragments for you.
In this example you supposedly have buttons to switch your fragments, as i call it btn1, btn2 and btn3 on the code
activity.xml
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/your_toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Activity.java
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class mActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ButterKnife.bind(this);
adapter = new MyPagerAdapter(getSupportFragmentManager());
container.setAdapter(adapter);
container.addOnPageChangeListener(this);
// This means 3 fragments, change the number as you like
container.setOffscreenPageLimit(3);
container.setCurrentItem(1);
}
// Use this method so your toolbar views switch the fragments
#OnClick({R.id.btn1, R.id.btn2, R.id.btn3})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn1:
container.setCurrentItem(0, true);
break;
case R.id.btn2:
container.setCurrentItem(1, true);
break;
case R.id.btn3:
container.setCurrentItem(2, true);
break;
}
}
}
PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return new YourFragmentClass1();
case 1:
return new YourFragmentClass2();
case 2:
return new YourFragmentClass3();
default:
return new YourFragmentClass1();
}
}
#Override
public int getCount() {
return 3;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}

BottomNavigation initial setCurrentItem not showing fragment and relaunching fragment fails

I have a Main Activity which uses a TabSelectedListener to show fragments for the AHBottomNavigation menu. The fragment called "FirstFragment" contains a FragmentPagerAdapter which allows the user to swipe between two tabs, each of which have its own fragment, called FirstTabInFirstFragmentFragment and SecondTabInFirstFragmentFragment (renamed for simplicity).
My issue is that:
a). When the Main Activity is launched, the "First" Bottom Navigation menu item is selected, however the "FirstFragment" is not launched. So, it shows the proper item selected with a blank screen. It only launches the First Fragment if I tap on the menu item again.
b). Once the FirstFragment has been properly launched and is being shown on screen (by the temporary fix done in a), if I select a different menu item (i.e. to navigate to SecondFragment) and then select the FirstFragment's menu item again, the two tabs within it are blank. Also, the sliding between the fragments for the two tabs does not work and gets "stuck" so you have to pull it all the way to one side or all the way to the other.
Hopefully I have explained my problem clearly - if there is anything I'm missing, I can provide more details.
Note that I am using com.aurelhubert.ahbottomnavigation.AHBottomNavigation
Here are the relevant files:
MainActivity.java:
public class MainActivity extends AppCompatActivity{
private AHBottomNavigationAdapter navigationAdapter;
private AHBottomNavigationViewPager viewPager;
private AHBottomNavigation bottomNavigation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add menu items to bar
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
this.createNavItems();
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
#Override
public boolean onTabSelected(int position, boolean wasSelected) {
//show fragment
if (position==0)
{
FirstFragment firstFragment=new FirstFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id,firstFragment).commit();
}else if (position==1)
{
SecondFragment secondFragment=new SecondFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id, secondFragment).commit();
}else if (position==2)
{
ThirdFragment thirdFragment=new ThirdFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id,thirdFragment).commit();
}else{
FourthFragment fourthFragment=new FourthFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id,fourthFragment).commit();
}
return true;
}
});
}
private void createNavItems(){
navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.navigation);
navigationAdapter.setupWithBottomNavigation(bottomNavigation);
// set current item
bottomNavigation.setCurrentItem(0);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</android.support.design.widget.CoordinatorLayout>
FirstFragment.java:
public class FirstFragment extends Fragment {
private FragmentActivity mContext;
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
//setContentView(R.layout.activity_first);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) getView().findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
// using getFragmentManager() will work too
FirstFragmentPagerAdapter adapter = new FirstFragmentPagerAdapter(mContext.getSupportFragmentManager(), mContext);
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
/**
* Override to set context. This context is used for getSupportFragmentManager in onCreateView
* #param activity
*/
#Override
public void onAttach(Activity activity) {
mContext=(FragmentActivity) activity;
super.onAttach(activity);
}
}
fragment_first.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabBackground="#color/firstTabBackground"
app:tabIndicatorColor="#color/firstTabIndicatorColor"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
FirstFragmentPagerAdapter.java:
public class FirstFragmentPagerAdapter extends FragmentPagerAdapter {
private Context context;
public FirstFragmentPagerAdapter(FragmentManager fm, Context mContext){
super(fm);
context = mContext;
}
/**
* The ViewPager asks the adapter for the fragment at a given position,
* i.e. for the 1st fragment, the ViewPager asks for the fragment at
* position 1.
* #param position
* #return
*/
#Override
public Fragment getItem(int position){
if (position == 0){
return new FirstTabInFirstFragmentFragment();
}
else{
return new SecondTabInFirstFragmentFragment();
}
}
/**
* On launch, the ViewPager asks the adapter how many pages there will be.
* Here, our adapter returns how many pages there will be.
* #return
*/
#Override
public int getCount() {return 2;}
#Override
public CharSequence getPageTitle(int position) {
switch(position){
case 0:
return context.getResources().getString(R.string.first_tab_in_first_fragment_page_title);
case 1:
return context.getResources().getString(R.string.second_tab_in_first_fragment_page_title);
default:
return null;
}
}
}
I was able to solve this in two steps. First, in my Main Activiy method createNavItems, in addition to doing a bottomNavigation.setCurrentItem(0);, I also had to manually update the CoordinatorLayout (which has ID content_id) with the default fragment:
getSupportFragmentManager().beginTransaction().replace(R.id.content_id, new FirstFragment()).commit();
Next, to solve the TabLayout issues I had to change this line:
FirstFragmentPagerAdapter adapter = new FirstFragmentPagerAdapter(mContext.getSupportFragmentManager(), mContext);
to
FirstFragmentPagerAdapter adapter = new FirstFragmentPagerAdapter(getChildFragmentManager(), mContext);
The reason for this is that when nesting Fragments inside of other Fragments with ViewPager, getFragmentManager() is for managing Fragments within a Fragment whereas mContext.getSupportFragmentManager() is used for Activities.
Firstly, Inorder to initially load the frstfragment on startup, u have to define the frstfragments view as default view to mainactivitys view.
Secondly, you need to have a framelayout to load the views for each fragment when selecting bottomnavigation menu.. here you are simply replacing the parent layout of mainactivity, which is a wrong concept, i believe.
So you need to create a child framelayout to load fragment views for each bottomnavigation menu items.
edits:
my layout which contains bottomnavigation,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_home"
android:keepScreenOn="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_toolbar_home_search">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#drawable/btn_style_white"
android:layout_marginRight="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_search"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Search 18000+ products"
android:id="#+id/tv_search_home"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ll_toolbar_home_search"
android:layout_above="#+id/navigation"
android:id="#+id/frame_layout_fragment"
android:background="#color/white">
</FrameLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
and java class for that activity( only method that contains bottomnavigation manipulations) is follows:
private void BottomView1() {
navigation = (AHBottomNavigation) findViewById(R.id.navigation);
AHBottomNavigationItem item1 = new AHBottomNavigationItem("Home", R.drawable.ic_home, R.color.ash);
AHBottomNavigationItem item2 = new AHBottomNavigationItem("Category", R.drawable.ic_category, R.color.ash);
AHBottomNavigationItem item3 = new AHBottomNavigationItem("Search", R.drawable.ic_search, R.color.ash);
AHBottomNavigationItem item4 = new AHBottomNavigationItem("Offers", R.drawable.ic_offers, R.color.ash);
AHBottomNavigationItem item5 = new AHBottomNavigationItem("Cart", R.drawable.ic_cart, R.color.ash);
navigation.addItem(item1);
navigation.addItem(item2);
navigation.addItem(item3);
navigation.addItem(item4);
navigation.addItem(item5);
navigation.setCurrentItem(getResources().getColor(R.color.colorPrimaryDark));
navigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
setCartNotification();
navigation.setAccentColor(getResources().getColor(R.color.colorPrimaryDark));
navigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
#Override
public boolean onTabSelected(int position, boolean wasSelected) {
Intent ii ;
Fragment selectedFragment = null;
if(position==0){
selectedFragment = HomeFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout_fragment, selectedFragment);
transaction.commit();
} else if(position==1){
ii = new Intent(HomeActivity.this, ShopByCategoryActivity.class);
startActivity(ii);
} else if(position==2){
ii = new Intent(HomeActivity.this, SearchActivity.class);
startActivity(ii);
} else if(position==3){
ii = new Intent(HomeActivity.this, OffersActivity.class);
startActivity(ii);
} else if(position==4){
ii = new Intent(HomeActivity.this, MyCartActivity.class);
startActivity(ii);
}
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout_fragment, HomeFragment.newInstance());
transaction.commit();
}

Android Tabs in middle of layout

In the middle of my layout I want to have 2 tabs to choose between viewing 2 different lists in the second half of the screen. How can I do this?
Here is an image illustrating what I wish to achieve. This image has a questions tab and an answers tab in the middle of the screen. Depending on which tab you select it shows a different listview:
I wish to achieve this exact same thing.
I tried doing it with TabHost widget, but for the life of me I couldn't get rid of the title bar (I tried selecting themes with no title bar, tried setting the theme in the manifest to a no title bar one)
I also tried make action bar tabs, but those were at the top of the screen....
How can I create tabs in the middle of my screen like in the image?
I had to do the same today! I tried with PagerTabStrip, because I thought you can't use TabLayout if it's not with a Toolbar. Turns out I was wrong, and it's even used in Google iosched. So you can put a TabLayout + ViewPager wherever you want.
I know you want to do some very custom tabs, which would be easier to customize if they were buttons, but in my opinion it's better to use TabLayout + ViewPager, since it makes things more scalable.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"></LinearLayout>
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/top_layout"
android:layout_weight="1">
<android.support.design.widget.TabLayout
android:id="#+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="60dp"
app:tabGravity="fill"
app:tabIndicatorColor="#color/colorAccentSecondary"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorAccentSecondary"
app:tabTextAppearance="#style/TournamentWaitingTimerTabTextAppearance" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/pager_header"></android.support.v4.view.ViewPager>
</RelativeLayout>
</LinearLayout>
MainActivity class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// Set the ViewPagerAdapter into ViewPager
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new LeftFragment(), "Players");
adapter.addFrag(new RightFragment(), "Prizes");
viewPager.setAdapter(adapter);
TabLayout mTabLayout = (TabLayout) findViewById(R.id.pager_header);
mTabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
You can change the adapter you use, it doesn't matter.
To customize tabs, I'd take a look at this amazing tutorial.
You can try using a ViewPager with PagerTabStrip
You can add fragments as page tabs by implementing a FragmentPagerAdapter
In your layout xml file, you should be able to move around the ViewPager to anywhere in your layout like you would most other components. I haven't tried centering it, but I have placed things above and below it, and it behaves as you would expect.
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
</android.support.v4.view.ViewPager>
FragmentPagerAdapter example
public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
{
final int PAGE_COUNT = 2;
private final String[] PAGE_TITLES =
{
"Fragment1",
"Fragment2"
};
public FragmentPagerAdapter()
{
super(getSupportFragmentManager());
}
#Override
public int getCount()
{
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position)
{
return PAGE_TITLES[position];
}
#Override
public Fragment getItem(int position)
{
switch(position)
{
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return null;
}
}
}
and in your main Acivity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mCustomPagerAdapter = new FragmentPagerAdapter();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
...
}

Replacing fragments in one activity

I am able to add a fragment to my fragmentlayout, but when I'm trying to replace the fragment with a new one the screen is blank. I can add a new fragment but then it's not scrollable. I'm also not able not remove a fragment.
Does someone know what I'm doing wrong? I red a lot about it and I feel like I have tried everything..
The framelayout is defined static:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>'
And this is the method for switching fragments:
void TabOnTabSelected (object sender, ActionBar.TabEventArgs tabEventArgs)
{
ActionBar.Tab tab = (ActionBar.Tab)sender;
var fragmentTransaction = FragmentManager.BeginTransaction ();
switch (tab.Text) {
case "vandaag":
if (currentFragment != null) {
fragmentTransaction.Remove (FragmentManager.FindFragmentByTag ("gister"));
}
if (fragmentToday == null) {
fragmentToday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (0).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentToday, "vandaag");
currentFragment = fragmentToday;
break;
case "gister":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentYesterday == null) {
fragmentYesterday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (-1).ToString ("d-M-yyyy"));
}
currentFragment = fragmentYesterday;
fragmentTransaction.Add (Resource.Id.fragment_container, currentFragment, "gister");
break;
case "morgen":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentTomorrow == null) {
fragmentTomorrow = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (+1).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentTomorrow);
currentFragment = fragmentTomorrow;
break;
}
fragmentTransaction.Commit ();
}
XML of the framelayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list" />
</LinearLayout>
Huge thanks in advance!
You can try using the .replace method. If you do not wish to be able to move back to the previous fragment than you can remove addToBackStack().
This may need to be altered slightly to fit your application.
FragmentManager fragmentManager = getFragmentManager();
Fragment mFragment = new DetailFragment();
mFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.fragment_container, mFragment).addToBackStack("DETAILFRAGMENT").commit();
POSSIBLE SECOND SOLUTION:
Check to see how your Fragment layout XML is. Possibly try using a FrameLayout as the parent.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.modup.fragment.DetailFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>
I have open fragments from navigation view as items are selected from it.(I have use DataBinding Concept)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.k15.projectkhyatisalesapp.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:titleTextColor="#ffffff"
android:background="#ef6c00"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp"
android:orientation="horizontal">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:id="#+id/activity_main_frame_layout"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/activity_main_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/listnavigation" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</RelativeLayout>
<layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
//Declaration of Variables
private ActivityMainBinding mainBinding; //for binding values in xml
private ActionBarDrawerToggle mDrawableToogle; //This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.
private Fragment fragment;
private Class fragmentClass = null; //pointing to class of fragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(mainBinding.toolbar); //o designate a Toolbar as the action bar for an Activity
setUpDrawer(mainBinding.activityMainNavigation); //for select items in navigationView
mDrawableToogle = setupToogle();
mainBinding.drawerLayout.addDrawerListener(mDrawableToogle);
}
//private method to set navigationView
private void setUpDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
try {
selectItem(item); //call when item is selected from navigationView
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return true;
}
});
}
//items are selected from navigationView
public void selectItem(MenuItem menuItem) throws IllegalAccessException, InstantiationException {
switch (menuItem.getItemId()) {
case R.id.Home:
fragmentClass = Home.class;
break;
case R.id.Product:
fragmentClass = Product.class;
break;
case R.id.Media:
fragmentClass = Media.class;
break;
}
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(mainBinding.activityMainFrameLayout.getId(), fragment).commit(); //change the fragment
setTitle(menuItem.getTitle());
menuItem.setCheckable(true); //for set selected item
mainBinding.drawerLayout.closeDrawers();
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawableToogle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawableToogle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupToogle() {
return new ActionBarDrawerToggle(this, mainBinding.drawerLayout, mainBinding.toolbar, R.string.open, R.string.close);
}
menu/listnavigation.xml is attached to navigation view I contains items of navigation view.
In my code as items are selected from navigation view accordingly fragment is replaced. Here Home, Product, Media are my fragment, you can create any fragment.
Try using fragmentTransaction.replace() instead of fragmentTransaction.add() and comment out code for removing fragments. When you replace the fragment - it gets removed. Also - remember to call: fragmentTransaction.commit() when you are done with replacing fragments.

Update A ListView In One Fragment From A Second Fragment

I have a ViewPager that shows the user 2 fragments.
Fragment 1 contains a button with an onClickListener that adds an entry to the ListView contents in fragment 2. The new entry is a number generated by a random number generator.
Fragment 2 contains a ListView with some random numbers in.
I cannot find a way to refresh the list in fragment 2 from the button listener in fragment 1. How do i do this?
I have tried:
initializing and setting the list adapter in an onResume in
fragment 2.
Calling myViewPager.notifyDataSetChanged from fragment 1.
Neither of the above methods have worked. Any ideas?
Edit: Here is my code:
layout_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
layout_frag_one.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/main_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add Number" />
</RelativeLayout>
layout_frag_two
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
MainActivity
public class SlideActivity extends FragmentActivity {
private final static int FRAGMENT_NUMBER = 2;
private ViewPager pager;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_mainpager);
pager = (ViewPager) findViewById(R.id.mainpager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
super.onCreate(savedInstanceState);
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int index) {
switch(index){
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return new Fragment1();
}
}
public int getCount() {
return FRAGMENT_NUMBER;
}
}
}
You may need to implement some of this to get it working right:
http://developer.android.com/training/basics/fragments/communicating.html
And some more reading here:
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Categories

Resources