Implementing sliding tabs in a fragment - android

I'm looking at a guide on how to use TabLayout. This is how they show to attach the PagerAdapter class.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
However, their TabLayout is in an Activity, and mine is in a Fragment. I'm having trouble adapting their code to my use case, because I don't fully understand the practical differences between activities and fragments. Right now, when I try and swipe between tabs, nothing happens. Here is my code:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
#InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
#Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}

I figured it out!!!
I had my xml layout file for the fragment set to RelativeLayout instead of LinearLayout, so my ViewPager wasn't on the screen, which meant I couldn't swipe. It's the simple mistakes that get me

Related

Android TabLayout and ViewPager Fragment using RxJava

Let's say that I have a ViewPager that will have 2 Fragments. From those Fragments, I create a PublishSubject which I would like to subscribe to within the MainActivity. However, the issue is during the onCreate(), I would not be able to grab that specific PublishSubject from the Fragment since it seems like it hasn't been created yet. Therefore, I end up with an NPE if I tried to make a subscription for it.
Is there a way to pass the PublishSubject once the Fragment is created by the ViewPager to the MainActivity? Or should I be trying to do something else instead. I've tried looking through other issues, but I couldn't find anything that is similar with something like this.
Some Code to illustrate what I mean:
Fragment
public class ExampleFragment extends Fragment {
PublishSubject<SomeObject> mPublishSubject = PublishSubject.create();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
// Regular fragment stuff here
}
public PublishSubject<SomeObject> getPublishSubject() {
return mPublishSubject;
}
}
ViewPager
public class ExamplePagerAdapter extends FragmentStatePagerAdapter {
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ExampleFragment();
case 1:
return new SomeOtherFragment();
default:
return null;
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private PublishSubject<SomeObject> mSubjectFromTheFragment;
private ExamplePagerAdapter mPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("tab1"));
tabLayout.addTab(tabLayout.newTab().setText("tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
mViewPager.setAdapter(mPagerAdapter);
// Set listener
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
// Code to implement the methods
});
// When or where do I set PublishSubject
mPublishSubject = ?
// The subscription
mPublishSubject.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(/** Some new Action1<> etc. **/ )
}
}
Any help or pointing me to some kind of direction is really appreciated!

No Id found viewpager

I have an app with 3 tabs. A tab has a fragment with a button to go another view within the same tab. My implementation is simple. I have main_activity with view pager. Here is snippet of main_activity.xml.
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Below the MainActivity.java
public class MainActivity extends AppCompatActivity {
public SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mViewPager);
//Assigns the ViePager to TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
//Defines the number of tabs by setting appropriate fragment and tab name.
private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirsTab(),"TAB1");
adapter.addFragment(new SecondTab(),"TAB2");
adapter.addFragment(new ThirdTab(),"TAB3");
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
When I replace the FirstTab with FirsTabSubFragment with getChildFragmentManager() I get a "no view found" error.
java.lang.IllegalArgumentException: No view found for id 0x7f0d0073 (com.none.app:id/pager) for fragment FirsTabSubFragment{607b4a6 #0 id=0x7f0d0073}
Below the code for FirtTab.java
public class FirstTab extends Fragment{
private Button btnScan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scan_layout, container, false);
btnScan = (Button) view.findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
getChildFragmentManager()
.beginTransaction()
.replace(R.id.pager, new FirsTabSubFragment())
.addToBackStack(null)
.commit();
}
});
return view;
}
A replacement with a getFragmentmanager() results in a empty view. Can anyone advice me on this topic?

Android: how to update recyclerview inside fragment which is inside viewpager upon button click

I have a recyclerview inside a fragment which is inside viewpager. I have button outside viewpager. How to update the recyclerview upon clicking the button.
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to save some things and do the changes in the recyclerview of tab 0;
}
});
}
}
The framgment code in tab 0 of the viewpager, in which i am using recyclerview is as below:
public class Udupiwhattosee extends Fragment {
private RecyclerView rv;
private List<Udupiwhattoseearray> udupiwhattoseearray;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_1,container,false);
v.setLayoutManager(new LinearLayoutManager(getActivity()));
v.setHasFixedSize(true);
initializeData();
WhattoseeRVAdapter adapter = new WhattoseeRVAdapter(udupiwhattoseearray,container.getContext());
v.setAdapter(adapter);
return v;
}
private void initializeData(){
udupiwhattoseearray = new ArrayList<>();
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Sri Krishna Temple", "Udupi, Karanata,INDIA", R.drawable.udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 2", "Udupi, Karanata,INDIA", R.drawable.udup_krishna_math_gopura_udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Place 3", "Udupi, Karanata,INDIA", R.drawable.udupi_rath));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 4", "Udupi, Karanata,INDIA", R.drawable.udupi_window));
}
recycler_view.post(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
HERE you will see the working of interface ,try this hope its gonna work fine

Android how to hide tab from TabLayout

I need to hide first tab. First page should work but when user select it, it should be seems like on tabs is selected. How I can do this?
I found some solutions with TabHost and it useless to me.
public class TabFragmentClients extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 5 ;
FinanceClients FinanceClients;
public ClientsFragment clientsFragment;
public FinanceFragment financeFragment;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
final View x = inflater.inflate(R.layout.tab_layout_clients,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
tabLayout = (TabLayout) findViewById(R.id.tabs);
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition).setVisibility(View.GONE);//hides the tab
Did you try this?
tabLayout.setupWithViewPager(viewPager);
tabLayout.removeTabAt(0);

onClickListener (buttons) to switch view of ViewPager using Fragments

I'm developing an application, which consists of a FragmentActivity with three Fragments. I want to go from the first fragment to the third and last one with "next" buttons. For that, I'm using a viewPager and pagerAdpater (I disabled the paging/swiping on the viewpager). My "next" buttons have been implemented in their own fragments. I have one xml layout for each fragment and one for the fragmentActivity containing the viewpager.
I am trying to use an OnClickListener where I could use the setCurrentItem(index) method and go from the first to the last view.
The problem is that i don't know where I could put the OnClickListener, since I don't have the viewpager reference in my fragment.
How can I get the viewpager reference in my fragments? Do I have to put my OnClickListener method somewhere else?
Here is the code of one of my 3 Fragments:
public class InitializationStep1 extends Fragment{
private PagerAdapter mPagerAdapter;
CustomViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);
Button button = (Button) v.findViewById(R.id.goToStep2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CustomViewPager pager = (CustomViewPager) v.findViewById(R.id.viewpager);
if(pager == null)
System.out.println("It's null..."); // always getting a null value
else
pager.setCurrentItem(0);
}
});
return v;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
and this is my FragmentActivity:
public class Initialization extends FragmentActivity{
private PagerAdapter mPagerAdapter;
CustomViewPager pager;
Button b1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_initialization_layout);
//initialize the pager
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, InitializationStep1.class.getName()));
fragments.add(Fragment.instantiate(this, InitializationStep2.class.getName()));
fragments.add(Fragment.instantiate(this, InitializationStep3.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (CustomViewPager)super.findViewById(R.id.viewpager);
pager.setPagingEnabled(false);
pager.setAdapter(this.mPagerAdapter);
pager.setCurrentItem(0);
}
}
To sum all this up, I have:
- implemented 3 Fragments (1, 2 and 3)
and I would like to get:
- Each Fragment containing a button to go to the next Fragment (1 -> 2 -> 3).
- The last fragment containing a "finish" button (because there is no next Fragment to navigate to)
I assume that this is a very simple action, but I'm relatively new in Android development and couldn't figure this out. I'm maybe trying to use the wrong component to do that.
Thanks
I just had the same problem, try this! In your Initialization FragmentActivity:
public CustomViewPager getpager(){
return pager;
}
In "InitializationStep1":
CustomViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);
pager = ((Initialization)getActivity()).getpager();
Button next = (Button) v.findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pager.setCurrentItem(2);
}
});
In this way, you can get the viewpager object that you declare in your FragmentActivity, so you can use it in each fragment to switch between fragments as you want!

Categories

Resources