Vertical LinearLayoutManager Crashes App - android

So I am using two RecyclerViews in a fragment in my android app. One of them scrolls horizontally and the other scrolls vertically. Yet for some strange reason the vertical one always crashes with the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference
Here's how I set up the RecyclerViews:
#InjectView(R.id.nearby_recycler)
RecyclerView nearbyRecycler;
RecyclerView.LayoutManager nearbyLayoutManager;
#InjectView(R.id.buddies_recycler)
RecyclerView buddiesRecycler;
RecyclerView.LayoutManager buddiesLayoutManager;
public static HomeFragment newInstance(){
return new HomeFragment();
}
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.inject(this, root);
nearbyLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
nearbyRecycler.setLayoutManager(nearbyLayoutManager);
nearbyRecycler.setAdapter(buddiesAdapter);
buddiesLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
buddiesRecycler.setLayoutManager(buddiesLayoutManager);
buddiesRecycler.setAdapter(buddiesAdapter);
return root;
}
The RecyclerViews in fragment_home.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/nearby_recycler"
android:layout_width="match_parent"
android:layout_height="#dimen/icon_large"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/buddies_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
As soon as I switch the orientation to Horizontal, the RecyclerView crashes as soon as I scroll it. Any suggestions?

I figured it out! So I'm completely unsure about why this was an issue, but the root of the problem was that my fragment was inside a view pager. I took it out of the view pager for now and added the fragment through fragment transaction.
My previous method of adding the fragment was this way.
MainActivity.java
HomeFragment homeFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
homeFragment = HomeFragment.newInstance();
mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return homeFragment;
}
#Override
public int getCount() {
return 1;
}
};
}
activity_main.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"
android:background="#android:color/white"
tools:context=".ui.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_below="#id/toolbar1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
I changed it to this:
MainActivity.java
HomeFragment homeFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, homeFragment)
.commit();
}
}
activity_main.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"
android:background="#android:color/white"
tools:context=".ui.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

Related

ViewPager inside Fragment doest show content

im developing an android app which has a bottomnavigation that opens 3 different fragments without a viewpager. One fragment has a viewpager inside it which opens 2 fragments. these 2 fragments are the same fragment and each has a recyclerview inside it. my problem is that when I run the app everything except the viewpager runs as intended. I tried putting the fragment inside the viewpager instead of it and the fragment works. I even tried putting the viewpager in a empty activity which worked as well.
This is my MainActivity which holds the BottomNavigation and its functionality:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private androidx.appcompat.widget.Toolbar toolbar;
private BottomNavigationView navView;
private Fragment fragment1 = new HomeFragment();
private Fragment fragment2 = new SearchFragment();
private Fragment fragment3 = new SettingsFragment();
private FragmentManager fm = getSupportFragmentManager();
private Fragment active = fragment1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.action_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
fm.beginTransaction().add(R.id.main_container, fragment3, "settings").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "search").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "home").commit();
navView = findViewById(R.id.nav_view);
navView.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
fm.beginTransaction().hide(active).show(fragment1).addToBackStack("home").commit();
active = fragment1;
toolbar.setTitle("Home");
return true;
case R.id.navigation_search:
fm.beginTransaction().hide(active).show(fragment2).addToBackStack("search").commit();
active = fragment2;
toolbar.setTitle("Search");
return true;
case R.id.navigation_settings:
fm.beginTransaction().hide(active)
.show(fragment3).addToBackStack("settings").commit();
active = fragment3;
toolbar.setTitle("Settings");
return true;
}
return false;
}
public void setCurrentPage(Fragment fragment, int position) {
if(fm.findFragmentByTag("playlist"+position) != null){
fm.beginTransaction().remove(fragment).hide(active).add(R.id.main_container, fragment, "playlist"+position).addToBackStack(null).commit();
active = fragment;
} else {
fm.beginTransaction().hide(active).add(R.id.main_container, fragment, "playlist"+position).addToBackStack(null).commit();
active = fragment;
}
}
}
This is the MainActivityLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
tools:context=".MainActivity">
<include
layout="#layout/action_bar"
android:id="#+id/action_bar"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:layout_marginBottom="70dp"
android:id="#+id/main_container_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/main_container"/>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu"
app:itemTextColor="#color/bottom_nav_color"
app:itemIconTint="#color/bottom_nav_color"
android:elevation="20dp"/>
</RelativeLayout>
This is my Fragment that implements the ViewPager:
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
PlaylistsPagerAdapter playlistsPagerAdapter = new PlaylistsPagerAdapter(getChildFragmentManager());
playlistsPagerAdapter.addFragment(new PlaylistsFragment(),"private");
playlistsPagerAdapter.addFragment(new PlaylistsFragment(),"public");
ViewPager viewPager = view.findViewById(R.id.pager_home);
viewPager.setAdapter(playlistsPagerAdapter);
TabLayout tabLayout = view.findViewById(R.id.tab_layout_home);
tabLayout.setupWithViewPager(viewPager);
setHasOptionsMenu(false);
}
public class PlaylistsPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> mFragmentList = new ArrayList<>();
ArrayList<String> mFragmentListTitle = new ArrayList<>();
public void addFragment(Fragment playlistsFragment, String title) {
mFragmentListTitle.add(title);
mFragmentList.add(playlistsFragment);
}
public PlaylistsPagerAdapter(FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int i) {
return mFragmentList.get(i);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentListTitle.get(position);
}
}
}
This is the HomeFragmentLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager.widget.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager_home"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout_home"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.viewpager.widget.ViewPager>
This is the PlaylistsFragment:
public class PlaylistsFragment extends Fragment {
private static final String TAG = "PlaylistsFragment";
private ArrayList<String> mPlaylistName = new ArrayList<>();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_playlists, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initNameBitmap(view);
}
private void initNameBitmap(View view){
mPlaylistName.add("Playlist1");
mPlaylistName.add("Playlist2");
mPlaylistName.add("Playlist3");
mPlaylistName.add("Playlist4");
initRecyclerView(view);
}
private void initRecyclerView(View view){
RecyclerView recyclerView = view.findViewById(R.id.recycler_view_playlists);
RecyclerViewAdapterPlaylists recyclerViewAdapterPlaylists = new RecyclerViewAdapterPlaylists(mPlaylistName, view.getContext(),this);
recyclerView.setAdapter(recyclerViewAdapterPlaylists);
recyclerView.setLayoutManager(new LinearLayoutManager(getView().getContext()));
}
}
This is the PlaylistsFragments Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view_playlists"
android:layout_margin="10dp" />
</LinearLayout>
The weird thing is that it worked already. I tried around with onBackPressed and when i implemented this and pressed another View on the BottomNavigation than the back button on the phone and than the Home button it showed as intened:
fm.beginTransaction().hide(active).show(fragmentsHistory.get(fragmentsHistory.size()-1));
active = fragmentsHistory.get(fragmentsHistory.size()-1)
fragmentsHistory.remove(fragmentsHistory.size()-1);
What am I doing wrong?
Appreciate the help!
i think the problem is in your layout
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".ui.plans.PlansFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#DBD8D8" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
your HomeFragmentLayout should be similar to this

Adding scrolllistener to a recyclerview and hide a view on another layout

I have some fragments let's say A, B, and C. I get the corresponding layouts for these fragments using inflate method.
In A and B, I have a Recycler View which I tend to add a Scroll Listener. Also, in fragment C, I have a view (let's say a tablayout) which I want to hide/show it by scrolling the recycler views (which are in A and B).
I am not sure how can I achieve that. Since the tablayout is in the layout of fragment C (and not in A and B).
I was trying to inflate the layout of C in A and B, but it didn't work.
This is the scrolllistener on recycler view (fragments A and B):
View view = inflater.inflate(R.layout.fragment_A, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.myChannelsRecyclerView);
tabLayout = (SmartTabLayout) view.findViewById(R.id.tabs);
recyclerView.addOnScrollListener(new HideShowScrollListener() {
#Override
public void onHide() {
//tabLayout.animate().translationY(250);
//tabLayout return null
}
#Override
public void onShow() {
//tabLayout.animate().translationY(0);
//tabLayout return null again
}
});
I should note that HideShowScrollListener() is a helper method which is written by me.
fragment_A.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">
<android.support.v7.widget.RecyclerView
android:id="#+id/myChannelsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
</RelativeLayout>
main_fragment.xml (fragment C)
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<come.example.CustomViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<smartertablayout.SmartTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:stl_dividerThickness="1dp"
app:stl_defaultTabTextSize="9sp"
app:stl_distributeEvenly="true"
app:stl_indicatorThickness="0dp"/>
</LinearLayout>
Main fragment (fragment C in example) is responsible to change and replace the other fragments (A and B) using a pager. It somehow is the parent of this fragments
//something like this from my suggestion above
class Fragment_A extends Fragemnt{
YourScollListener animateCallback;
RecyclerView recyclerView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_A, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.myChannelsRecyclerView);
recyclerView.addOnScrollListener(new HideShowScrollListener() {
#Override
public void onHide() {
animateCallback.animate();
}
#Override
public void onShow() {
}
});
return view;
}
public void onAttach(Context context) {
super.onAttach(context);
animateCallback = (YourScrollListener) getParentFragment();
}
public interface YourScrollListener{
void animate();
}
}
//main frag
class Main_Fragment extends Fragemnt implements YourScrollListener{
animate(){
//do stuff
}
}

Android Studio - Opening a navigation drawer from a fragment class

I'm trying to open and populate a navigation drawer with a separate fragment by clicking on a button. The drawers themselves are only empty RelativeLayouts declared in activity_main.xml.
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">
<!--Main View-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxx.MainActivity">
</RelativeLayout>
<!--Left drawer-->
<RelativeLayout android:id="#+id/drawer_left"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</RelativeLayout>
<!--Right drawer-->
<RelativeLayout android:id="#+id/drawer_right"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end">
</RelativeLayout>
The main_view gets populated with a fragment in MainActivity, so it will look like this:
MainActivity class:
public class MainActivity extends AppCompatActivity {
android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
//Set fragment
Frag1 frag1 = new Frag1();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container,frag1).commit();
}
Frag1 class:
public class Frag1 extends Fragment {
ImageButton imageButtonOpenDrawer;
DrawerLayout mDrawerLayout;
RelativeLayout mDrawer_left;
android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag1, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View v = getView();
imageButtonOpenDrawer = (ImageButton) v.findViewById(R.id.imageButtonOpenDrawer);
fragmentManager = getFragmentManager();
mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
mDrawer_left = (RelativeLayout) v.findViewById(R.id.drawer_left);
imageButtonOpenDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerFragment drawerFragment = new DrawerFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,drawerFragment).commit();
}
});
}
The same principle works when done from MainActivity class. But when I try to open the drawer from the fragments class, this null pointer error gets thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference
Try to check returns of getView().
I think that frag1's root view is in R.layout.frag1 and it doesn't have drawer_layout and drawer_left.

Changing fragments

I am trying to show different fragments depending on whether GPS is enabled or not. But I am getting this error :-
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1
Please advise me how to do this:
Here is my MainActivity class:
public class MainActivity extends Activity {
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
fr = new FragmentTwo();
getFragment();
} else {
fr = new FragmentOne();
getFragment();
}
}
public void getFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne:-
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
FragmentTwo:-
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
My activity_main:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
And the fragment_one.xml and fragment_two.xml are same just the different text:-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GPS IS ENABLE"/>
</RelativeLayout>
Thanks in advance.
Replace
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
With
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
As static Fragment can not be replace at run time. But you can add or replace fragment in FrameLayout.
You can remove this line
android:name="com.test.FragmentTwo"
<fragment
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Why do I can slide to more than two times while I have only two tabs on Android?

I am learning how to use navigation drawer and navigation tab. I created an app that implement a navigation drawer on MainActivity and each item on navigation drawer will replace fragment on MainActivity to the corresponding fragment.
My first fragment provide two navigation tab using SlidingTabLayout and SlidingTabStrip. The other fragments are just a textview.
If I move from the first item to the second item and then back to first item again, my tab still show two tab, but in fact, I can slide more than two times and the content is not shown.
How do I fix this?
What it should be:
After I move to second drawer and then back to first drawer:
What happened after that:
You can see that I can slide more than twice and the content of fragments are not shown.
MainActivity.java
public class MainActivity extends ActionBarActivity {
String titles[] = {"TabsFragment", "TextFragment"};
Fragment fragment[] = {TabsFragment.newInstance(), TextFragment.newInstance()};
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
DrawerLayout drawer;
ActionBarDrawerToggle mDrawerToggle;
static View.OnClickListener drawerItemClickListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
mAdapter = new DrawerAdapter(titles);
drawerItemClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedPos = mRecyclerView.getChildPosition(v);
drawer.closeDrawers();
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_content, fragment[selectedPos])
.commit();
}
};
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
drawer,
toolbar,
R.string.open_drawer,
R.string.close_drawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
supportInvalidateOptionsMenu();
}
};
drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_content, fragment[0])
.commit();
}
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout
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"
android:id="#+id/drawer_layout">
<RelativeLayout
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=".MainActivity">
<android.support.v7.widget.Toolbar
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" />
<FrameLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
TabsFragment.java
public class TabsFragment extends Fragment {
public static Fragment newInstance() {
TabsFragment fragment = new TabsFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tabs, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter(getActivity().getSupportFragmentManager(), getActivity().getApplicationContext()));
SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setDistributeEvenly(true);
mSlidingTabLayout.setViewPager(mViewPager);
}
class SamplePagerAdapter extends FragmentPagerAdapter {
private String[] tabTitles = new String[] {"Tab One", "Tab Two"};
private Context context;
public SamplePagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment[] fragment = {TabOneFragment.newInstance(), TabTwoFragment.newInstance()};
return fragment[position];
}
}
}
fragment_tabs.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"
tools:context=".TabsFragment">
<android.com.drawertab.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/sliding_tabs" />
</RelativeLayout>
TabsOneFragment.java
public class TabOneFragment extends Fragment{
public static TabOneFragment newInstance() {
TabOneFragment fragment = new TabOneFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_one, container, false);
}
}
fragment_tabs_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="TAB ONE" />
</LinearLayout>
TabsTwoFragment.java
public class TabTwoFragment extends Fragment {
public static TabTwoFragment newInstance() {
TabTwoFragment fragment = new TabTwoFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_two, container, false);
}
}
fragment_tabs_two.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="TAB TWO" />
</LinearLayout>
TextFragment.java
public class TextFragment extends Fragment {
public static Fragment newInstance() {
TextFragment fragment = new TextFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_text, container, false);
}
}
fragment_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="FRAGMENT TEXT" />
</LinearLayout>
DrawerAdapter.java
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
private String mNavTitles[];
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView,int ViewType) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.rowText);
itemView.setOnClickListener(MainActivity.drawerItemClickListener);
}
}
public DrawerAdapter(String[] titles) {
mNavTitles = titles;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_item_row, parent, false);
ViewHolder vhItem = new ViewHolder(v, viewType);
return vhItem;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mNavTitles[position]);
}
#Override
public int getItemCount() {
return mNavTitles.length;
}
}
drawer_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingTop="4dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/rowText" />
</LinearLayout>
If you are correct about sliding more times than twice, then you need to set the override method getCount correctly (I realize you know). That's the only issue you need to worry about.
private String[] tabTitles = new String[] {"Tab One", "Tab Two"};
...
#Override
public int getCount() {
return tabTitles.length;
}
Note: I declared tabTitles differently as an array of String "String[]", widely accepted. The other style "tabTitles[]" may not work well with its methods like length.
Looking at your code more closely, I suspect objects TabOneFragment and TabTwoFragment is not working well. Normally override getItem returns the same static Fragment. And of course, I do not see that declaration or code for it.
My suggested try is (from your code):
#Override
public android.support.v4.app.Fragment getItem(int position) {
return TabsFragment.newInstance(position);
}
Note: With the above code, I don't see any use for TabOneFragment and TabTwoFragment, referenced in getItem(int position) in SamplePagerAdapter class. The TextView UI in layout fragment_tab_one.xml is only used for the tab texts. For those texts, the override getPageTitle(int position) can be used and is already coded.

Categories

Resources