Android Tabs + Sliding, how do I implement ActionBarSherlock?

I am using new Google tools to launch an application that has the ability to switch between three tabs.

enter image description here

This is great, but not enough support for older devices.

1. I added an ABS application with a support library to the application.

2. I changed the public class MainActivity extends FragmentActivity implements ActionBar.TabListener { to public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

3. I still have a lot of errors, and I don’t even know if this will work on older devices. Does anyone have any tips on how to implement sliding tabs compatible with 2.x and above?

enter image description here

Update:

I am stuck in step 6 of alextsc's answer enter image description here

+6
source share
5 answers

Exactly the same process worked for me. I removed all imports and then pressed ctrl + shift + o and selected compatibility classes. He worked absurdly. see post here .

+3
source

I tried this wizard once, and I think I completely discarded the generated code when I applied this exact pattern using ActionBarSherlock, so I suggest starting with a normal "empty" activity from scratch. Here is a short walkthrough. Not all steps are fully described, but you should find enough documentation with keywords to get started.

1) Add ActionBarSherlock to your project (obviously)

2) Create a new action that extends SherlockFragmentActivity and sets the correct abs theme

At this point, you should have empty activity with the action bar.

3) Change the layout and enable the ViewPager, which fills the viewport

4) Write your snippets (or placeholders) and adapter for ViewPager, connect them together

There are many textbooks that explain everything that is needed here, for example. This is a blog post .

That should give you an action with an action bar and custom layout. Now you can draw between your fragments.

5) Add tabs to the action bar and attach a blank tab to them

Example:

 actionBar = getSupportActionBar(); sampleTab = actionBar.newTab() .setText(R.string.title) .setTag(TABTAG_SAMPLE) .setTabListener(tabListener); actionBar.addTab(sampleTab); 

Make sure you give each of your tabs a separate tag (the string const is fine). This will be used to determine which tab is pressed in a second. Also, make sure that you save the created tab instances in the class variable. You will need them later. Repeat the above snippet for each tab. You can use a regular TabListener, but I recomment using SimpleTabListener, since you only need to override one method later.

You should now have activity with an action bar, scrollable snippets, and (non-functional) tabs.

6) Fill the tab listener and connect it to the viewpager

 private SimpleTabListener tabListener = new SimpleTabListener() { @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { final String tag = (String) tab.getTag(); if (TABTAG_SAMPLE.equals(tag)) { viewPager.setCurrentItem(INDEX_SAMPLE); } else if (TABTAG_SECONDTAB.equals(tag)) { viewPager.setCurrentItem(INDEX_SECONDFRAGMENT); } } }; 

It should be easy. You listen to the tab selection event, check which tab is selected using the saved tag, and call the viewpagers setCurrentItem() method with the fragment index associated with the specific tab.

Now you can select a fragment using the tab, and also swipe the screen. You will notice that scrolling to a specific fragment does not synchronize tabs accordingly, they will not be correctly selected.

7) Attach OnPageChangeListener to ViewPager and select tabs accordingly

Again, you can use SimpleOnPageChangeListener here, and not instead of an interface. A brief example:

 private SimpleOnPageChangeListener onPageChangeListener = new SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { switch (position) { case INDEX_SAMPLE: actionBar.selectTab(sampleTab); break; case INDEX_SECONDFRAGMENT: actionBar.selectTab(secondTab); break; } }; }; 

It should also be self-evident. You observe the action of the swipe, which changes the displayed fragment, checks its index and selects the corresponding tab. You see why you needed to save tab instances from step 5, you need to select the tab.

Now everything should work.

+15
source

I have implemented a very complex application using sbarlock. It works fine on 2.2 ICS devices that I tested. I also used tabs and everything works fine. I changed it later to a navigation list because there should be 6 tabs. Try the demos included in the action bar to get started. These are probably errors of some problems with the classpath. You need to add sherlock as a lib project and the support library should be included both in the sherlock lib project and in your project. Also check if both support libraries should have the same version.

0
source

I would recommend taking a look at ViewPagerIndicator , it works with ActionBarSherlock and the compatibility library, I currently have an application that uses this to scroll tabs 2.2 and above.

0
source

I cannot add comments, so this is my comment on ViewPagerIndicator. I'm used to it, but when I started using loaders to load data and fragmented elements, sometimes there were strange problems when new data appeared, changing orientation ... But to use tabs with actionbarsherlock ViewPagerIndicator is not required.

0
source

Source: https://habr.com/ru/post/924194/


All Articles