Change between fragment navigation drawer viewpagerindicator - android

I have a Navigation Drawer and a ViewPagerIndicator inside the content_frame. I want to change between fragment`s from the navigation drawer but the problem is that switch between tabs ViewPager works but when I switch to another option that you have to remove the ViewPager and put a new fragment puts this fragment under the ViewPager.
Attached photo so you can see.
1) Here you can already see the new fragment below ViewPager.It can be distinguished by the text shown is Prueba
Attached code
Activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
Method for switching between fragment.
/** Main content by replacing fragments
* #param position
*/
private void selectItem(int position) {
//Code
Fragment fragment = new TryFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
mPager.setCurrentItem(0);
break;
case 1:
mPager.setCurrentItem(1);
break;
case 2:
ft.replace(R.id.content_frame, fragment);
break;
}
ft.commit();
mAdapter.notifyDataSetChanged();
//Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
Class TryFragment
class TryFragment extends Fragment
{
public static final String ARG_PLANET_NUMBER = "planet_number";
public TryFragment()
{
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText("Prueba");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
Log.d("TestFragment", "Estoy en el fragment");
return layout;
}
}
I do not know if I have to destroy the viewpager to show the new fragment or a problem of hierarchy in the xml.

Your ViewPager must be Fragment, and it should be placed to layout.content_frame, so when you calling ft.replace(R.id.content_frame, fragment); your ViewPager fragment will be replaced.

You should not use ViewPager and NavigationDrawer at the same time. You can see Roman Nurik's answer in this G+ post:
https://plus.google.com/u/1/+DavidTaSmoochibi/posts/8dWEkFcbTFX
"You shouldn't use navigation drawers with action bar tabs. If you're aiming for a UI similar to that of Google Play Music, you should implement tabs manually (and beware of how this looks on tablet—you don't want full-width tab bars on tablets). Also make sure to check out the Structure in Android App Design session from this year's Google I/O for a detailed run-through of the various interface patterns available for exposing app hierarchy."

Related

Android- FragmentTransaction.replace() works only once

I'm using a navigation drawer along with a SwipeRefreshLayout for the main content and when the user selects a menu item in the navigation drawer, I want to replace the fragment inside the SwipeRefreshLayout with another fragment.
This is what my onNavigationItemSelected() looks like:
// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
selectedNavItem = item.getItemId();
if(selectedNavItem == R.id.nav_files){
filesFragment = new FilesFragment();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.swipeLayout,filesFragment,"files");
transaction.commit();
} else if(selectedNavItem == R.id.nav_accounts){
accountsFragment = new AccountsFragment();
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.swipeLayout,accountsFragment,"accounts");
transaction.commit();
}
return true;
But this never works. When I click an item in the nav drawer, the fragment is replaced by a blank screen. My onCreate method also uses FragmentTransaction.replace() but that seems to work fine.
I also tried FragmentTransaction.remove() and then FragmentTransaction.add() but even that doesn't seem to work.
Edit: Layout files:
Layout of the content view of the navigation drawer:
<android.support.v4.widget.SwipeRefreshLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.harshallele.cloudpool.MainActivity"
tools:showIn="#layout/app_bar_main"
android:id="#+id/swipeLayout">
</android.support.v4.widget.SwipeRefreshLayout>
This is included inside another layout file which contains a CoordinatorLayout containing the toolbar.That file, in turn, is inside the main layout file of the activity inside a android.support.v4.widget.DrawerLayout
(Basically, this is the Navigation Drawer Activity provided by Android Studio when adding an Activity)
Layout for FilesFragment:
<FrameLayout 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"
tools:context=".fragments.FilesFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/fileListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
<ProgressBar
android:id="#+id/itemsLoadingProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminateOnly="true"
android:visibility="invisible"
/>
</FrameLayout>
Layout for AccountsFragment(this is just the default blank fragment, because i haven't finished this yet):
<FrameLayout 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"
tools:context="com.harshallele.cloudpool.fragments.AccountsFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
Edit 2:
AccountsFragment:
public class AccountsFragment extends Fragment {
public AccountsFragment() {
// 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_accounts, container, false);
}
}
With just this bit of code we can't help you so much. The problem can be that:
The FilesFragment and AccountsFragment aren't initialized in the right way;
The Layout with id swipeLayout can have visibility = gone;
The Layout of FilesFragment and AccountsFragment can be empty;
This are just some of the infinite reason why your code doesn't work properly so please share more code about the two Fragments and relative XML.

How to add fragment inside a fragment of ViewPager with same id where each fragment added in view pager is same?

I am trying to add a fragment inside another fragment which is added in view pager. All fragment inside view pager are same. I am just viewing them with swipe. On button click, i add a fragment inside our main fragment by using add fragment. On doing this, this new fragment is being added to last fragment of view pager instead of current one.
Reason which seems to me is when we look for fragment, we still have last fragment active and that fragment adds this new fragment on top of it.
Everything works fine for first
public void onAddFragmentClick(View view) {
if (!isInfoShown) {
FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();
fragment = new MainFragment();
fragmentTransaction.replace(R.id.fragment, fragment, "detail fragment");
fragmentTransaction.commit();
isInfoShown = true;
} else {
FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
isInfoShown = false;
}
}
Main Fragment which is added in viewpager is
<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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.wedm.wedme.ui.fragment.PictureScreenFragment"
android:id="#+id/fragment"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</RelativeLayout>
For nested fragments you should use the child fragment manager of the fragment where you want to add fragments. Hope this helps.

Load content of Navigation drawer item and ViewPagerStrip item on same container - Android

I am developing an application, It has an Activity(MainActivity.java) ,I integrated Navigation Drawer over it ,when I click navigation drawer item then the content of navigation drawer item loads on the <framelayout /> ,after that I integrated tabs at bottom on the same page (MainActivity.java) using ViewPagerStrip ,Now Problem is that :
When I click on Navigation drawer item and tabs item the content of Navigation Drawer item and ViewPagerStrip item is not being loaded on the same container ,How can I load content of both (tabs item and drawer item ) on the same container (Framelayout or ViewPager) .
I have following code in MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set activity_main.xml
setContentView(R.layout.activity_main);
//Create tabs at bottom using ViewPagerStrip
mCustomPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), this);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
//Create NavigationDrawer .
---------------------------------------
---------------------------------------
---------------------------------------
}
//Click on Drawer menus
private class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayDrawerItem(position);
}
}
private void displayDrawerItem(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
//Other fragments goes here .
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
I have following code in my activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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="wrap_content"
>
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="5dp"
android:paddingBottom="5dp" />
</android.support.v4.view.ViewPager>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
My own efforts
I tried to solve this problem with different-2 approaches
1 . I put <ViewPager > inside <framelayout > and tried to load click events of navigation item and tabs item on the ViewPager .
2 . I removed <framelayout > and tried to load click events of navigation and tabs items both on the ViewPager .
3 . I follow many Stackoverflow links ,tutorials regarding ViewPagerStrip and DrawerLayout , tried to search how to load click events of drawer item and tabs item on the same container .
Edit
public class CustomPagerAdapter extends FragmentPagerAdapter {
Context mContext;
private String[] tabs = {"Trails", "Breweries", "Near By", "Events"};
public CustomPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
mContext = context;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
// Create fragment object
android.support.v4.app.Fragment fragment = new DemoFragment();
// Attach some data to the fragment
// that we'll use to populate our fragment layouts
Bundle args = new Bundle();
args.putString("page_name", tabs[position]);
// Set the arguments on the fragment
// that will be fetched in the
// DemoFragment#onCreateView
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return tabs.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}}
public class DemoFragment extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout resource that'll be returned
View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
// Get the arguments that was supplied when
// the fragment was instantiated in the
// CustomPagerAdapter
Bundle args = getArguments();
((TextView) rootView.findViewById(R.id.textView)).setText(args.getString("page_name"));
return rootView;
}}
See xml layout below:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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="wrap_content">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_gravity="bottom"
android:background="#33b5e5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="38dp"
android:visibility="gone"></FrameLayout>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
Here when you click to tab set the frame_container visibility gone and when you click on navigation drawer item make the frame_container visibility visible.
Hope this will help.

Cardslib Fragment not responding when interacted in Navigation Drawer

The fragment I'm having problems with is using a CardListView from the Cardslib library. I added the fragment dynamically to the container and everything is working as expected, except it isn't responding when it is interacted with. This means it doesn't scroll, I can't press on any of the cards, and anything else related to the problem.
Adding fragment to container in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// Code here...
NoteFragment nf = new NoteFragment();
// nf.setArguments(getIntent().getExtras());
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.add(R.id.container, nf);
tx.commit();
}
Replaces container fragment when Navigation Drawer is used in MainActivity. Default case selected is NoteFragment.
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch(position) {
case 0:
onSectionAttached(0);
fragment = new NoteFragment();
break;
case 1:
onSectionAttached(1);
fragment = new ArchiveFragment();
break;
case 2:
onSectionAttached(2);
fragment = new TrashFragment();
break;
}
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.replace(R.id.container, fragment);
tx.commit();
}
NoteFragment returning inflated layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// CardListView Code Here...
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_note, container, false);
}
Container XML (activity_main.xml):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Fragment XML (fragment_note.xml):
<FrameLayout 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"
tools:context=".fragments.NoteFragment">
<it.gmariotti.cardslib.library.view.CardListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:id="#+id/CardList"/>
</FrameLayout>
Any help is appreciated.
I fixed it! The Cardslib piece of code in onCreateView() was moved to onStart(), and it fixed the unresponsiveness.

Android: How to use a ViewPager + action bar tab correctly in a Fragment?

This is my activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/listMainMenu"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/color_list_mainmenu_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_item_mainmenu_selector_background"
android:background="#color/color_list_mainmenu_background"/>
</android.support.v4.widget.DrawerLayout>
So in my MainActivity class I can show my main menu and I can show a new Fragment:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
String title = null;
switch (position) {
case 1:
fragment = new WorkoutStoreFragment();
title = getString(R.string.mainmenu_item_workout);
break;
....
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameContainer, fragment).commit();
// update selected item and title, then close the drawer
this.listMainMenu.setItemChecked(position, true);
this.listMainMenu.setSelection(position);
setTitle(title);
this.drawerLayout.closeDrawer(this.listMainMenu);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
But in my WorkoutStoreFragment class (extend v4.support.Fragment) I want to use a viewPager with a TabPageIndicator class to show some different other Fragments. But it doesn't work.
In general, my view architecture is it correct ?
Thanks by advance guys!

Categories

Resources