I'm learning to use Tablayout,but I found that the Tablayout can not slide normal.The tab slides with my gesture.The question is that
When I stop gesture, it will stop immediately, and does not automatically slide to the next tab.
the layout:
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
The code:
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter
(getSupportFragmentManager(), this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
When I had this happen to me, it was because either 1) My adapter didn't properly create a fragment for that page, or 2) The fragment didn't properly create a view for the page.
Use a tool like Android Device Monitor to check the view on the viewpager and see if it is there as you expect, and that it is not zero width.
Related
In this page Google explicitly said TabLayout can be integrated into ViewPager in layout xml file.
ViewPager integration
If you're using a ViewPager together with this layout, you can call
setupWithViewPager(ViewPager) to link the two together. This layout
will be automatically populated from the PagerAdapter's page titles.
This view also supports being used as part of a ViewPager's decor, and
can be added directly to the ViewPager in a layout resource file like
so:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
However, whatever I tried, I cannot make TableLayout appears. Separate both is fine. I wonder is this actually working??
I don't think TabLayout works as a ViewPager child as they claim.
Any view that wants to be a child of ViewPager has to set a boolean layout param property specific to ViewPager called isDecor. I just looked through the source code and I don't see any place where either the ViewPager or TabLayout set this layout param property.
I think it was something they meant to do and either a) they never did it or b) there was an issue so they took it out. Just use the TabLayout outside ViewPager, there's no real benefit to having it be a decor view.
In android.support.design.widget.TabLayout, how to make the active tab always appear at the center, just like in Play Newsstand app, as shown below.
The first and last tab should also appear at the center.
I tried using padding on the TabLayout. It is not working. This is the code that I wrote:
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/yellow"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#EEE"
app:tabMode="scrollable"
android:gravity="bottom"
android:clipToPadding="false"
android:paddingLeft="90dp"
android:paddingRight="90dp"
/>
In this case, the tabIndicator will also shift 90dp from left. It should remain at the center independent of the padding.
Checkout this solution: https://stackoverflow.com/a/36886331/651770
Could be solved by tabContentStart, but if you want to center both sides of Tablayout, you need to extend this class and set the padding by hand.
Maybe a little late, but you can try using this library :
https://github.com/jpardogo/PagerSlidingTabStrip
This can be done as they said :
Include the following dependency in your build.gradle file.
compile 'com.jpardogo.materialtabstrip:library:1.1.1'
Or add the library as a project. I tried to send a pull request, but looks like the original developer doesn't maintain it anymore.
Include the PagerSlidingTabStrip widget in your layout. This should usually be placed above the ViewPager it represents.
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
In your onCreate method (or onCreateView for a fragment), bind the widget to the ViewPager:
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new TestAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
Hope this help.
I want to implement a tabbed view pager where the tabs are not part of the action bar.
What is the correct way of doing this since it doesn't seem supported by standard.
So far I have been using the following approach and it seems like it's been so much more effort than it should be that it bothers me:
Create horizontally oriented linear layout to act as tab bar view
Create individual tab views to populate tab bar along with styling
Create custom ongesturelistener for left and right swipe to go between tabs
Create animations for sliding-in/out upon swiping (fragment transactions)
Create animations for the tab bar upon swiping
This seems like it's way too much work when there is a viewpager and tabbar built in. It seems though this "proper" approach is limited to only being used in conjunction with the actionbar. Can anyone provide a simple example of what I am trying to achieve? (see following picture for clarity).
You don't need to build a ViewPager bundled with an ActionBar, they are both independent views.
You can simply hide the ActionBar on that activity doing something like:
ActionBar mActionBar = activity.getActionBar();
mActionBar.hide();
Although it's an ugly solution you can do that to test your code. I would suggest either add a Theme to that specific Activity without an ActionBar (something like Theme.Material.NoActionBar) - not sure it's the correct name of the theme, but you get the idea.
Not sure I answered your question accordingly, but if not please show us some code.
You just add a viewpager with a pager tab strip. Plenty examples out there.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragmentPagerAdapter = new MyTabsPagerAdapter(
fm, tabsArray);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(fragmentPagerAdapter);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/default_screen_bg"
android:minWidth="1000dp">
put your image here
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp" />
</android.support.v4.view.ViewPager>
I am using the google example called EffectiveNavigation to create a ViewPager with tabs. The problem is that in the manifest, for my main activity, I have set
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen"
So that my app has no actionBar. Therefore I am getting a NullPointerException at
final ActionBar actionBar = getActionBar();//null from getActionBar()
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//NullPointerException
Now, all I want is to be able to create a simple ViewPager with tabs. That's it. Nothing grand. I am using the google example because it is what I found.
Basically in the google example, they are using the statusBar to hold the tabs. How else might I hold the tabs? Anything less than a good example or instruction on how to modify the goggle example is not going to be much help as I don't know much about ViewPagers. The link to the google example is http://developer.android.com/training/implementing-navigation/lateral.html
A simple workaround the statusBar might be enough.
Using the ActionBar to hold the tabs is actually deprecated because, even if your app does have action bar, it may lead to NullPointerException. Good news are the TabLayout from the Design package brings you the possibility to easily create a ViewPager with tabs. Just write this in the XML of your Activity (or Fragment):
<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.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
And once you have your Adapter for the ViewPager, include this code in the onCreate method of your Activity (or Fragment) after the setContentView method:
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
Hope it helps ;)
I found a fix. Basically in onCreate, before setContentView, I call
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Then everything works fine.
To anyone else arriving here finding that #learner's solution doesn't work for them, take a look at http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
I'm seeing a very strange error when trying to use ViewPagerIndicator (http://viewpagerindicator.com/). I've narrowed it down to the TitlePageIndicator that is part of that library.
When I use the TitlePageIndicator, I am unable to swipe between tabs, and my fragments don't show up (although they are created and ran, the button in the upper right is created by the fragment that should be showing). It looks like this: http://imgur.com/RJaI9 (Sorry, new user so I can't inline pictures)
But without, my fragments appear correctly and I'm able to swipe between them. http://imgur.com/bboSd
I have no idea why this is happening. I'm using the latest ViewPagerIndicator library, but I'm thinking maybe it has something to do with how I imported it? I followed the instructions on the website:
created the new android project in eclipse from existing source, imported the compatibility library, then added it to my project in the android project properties.
Any help would be greatly appreciated, I've been stuck on this for 2 weeks :(
Code:
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.viewpagerindicator.TitlePageIndicator
android:id="#+id/titles"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/grocery_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
FragmentActivity (From ActionBarSherlock):
public class GroceryActivity extends BaseFragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grocerylist);
// Set the pager with an adapter
ViewPager pager = (ViewPager) findViewById(R.id.grocery_pager);
pager.setAdapter(new GroceryViewPagerAdapter(getSupportFragmentManager()));
// Bind the title indicator to the adapter
//When these next two lines are commented out, everything works
TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
titleIndicator.setViewPager(pager);
}
}
I ended up finding the problem:
In my linear layout, I did not set the orientation of the layout. For some silly reason I thought the orientation field had to do with the orientation of the device, and not the layout itself. Adding the orientation:vertical field made everything work.
Hopefully this helps someone!