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);
Related
i'm trying to create an Fragment with a tabs
first the main Activity has a bottom navigation with
4 fragments. 1 of the fragments i want to make a tabs which will insert another Fragment so basically the Fragment has a tabs with Fragment
and i have Class MyPagerAdapter here is the java file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View pop = inflater.inflate(R.layout.fragment_adopt,
container, false);
TabLayout tabLayout = (TabLayout) pop.findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setText("DOGS"));
tabLayout.addTab(tabLayout.newTab().setText("CATS"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final ViewPager viewpager = (ViewPager) pop.findViewById(R.id.pager);
final PagerAdapter adapter = new MyPagerAdapter.(getSupportFragmentManager().tablayout.getTabCount()) ;
ViewPager.setAdapter(adapter);
ViewPager.setOnPageListener(new tabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return inflater.inflate(R.layout.fragment_adopt,container,false);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
The problem is that the GetSupportFragment doesn't funtioning
thank you for answering and Godbles
The definition of getChildFragmentManager() is:
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
The definition of getSupportFragmentManager():
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
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!
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?
I created an app on AS and I use Tab Layout with View Pager, Fragment in it from this site!
and I want to access each fragment layout (I have 3 fragment) to apply some changes on them. I searched about it in the Internet but I could not find anything about it.
Without seeing any code or errors.. I'll try to give the best answer I can.. In the class you set up the tab layout you can create the page fragments like so:
public class TabLayoutActivity extends MainActivity{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_layout);
ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
this.addPages(vp);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(listener(vp));
}
//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Tab1Fragment tab1Fragment = new Tab1Fragment();
adapter.addPage(mondayFragment);
Tab2Fragment tab2Fragment = new Tab2Fragment();
adapter.addPage(tab2Fragment);
Tab3Fragment tab3Fragment = new Tab3Fragment();
adapter.addPage(tab3Fragment);
pager.setAdapter(adapter);
}
private TabLayout.OnTabSelectedListener listener(final ViewPager pager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
}
You then create 3 classes, Tab1Fragment, Tab2Fragment, and Tab3Fragment, aswell as the xml layouts for each. Here is an example of one of the tab fragment classes:
public class Tab1Fragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_fragment, null);
}
#Override
public String toString() {
return "Tab 1";
}
}
This is a very simple example to give you an idea. You can then change the xml layout in the corresponding fragment class. Let me know if you have any questions
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