Java 9 Programming Blueprints by Jason Lee

Java 9 Programming Blueprints by Jason Lee

Author:Jason Lee
Language: eng
Format: epub
Tags: COM051280 - COMPUTERS / Programming Languages / Java, COM051000 - COMPUTERS / Programming / General, COM051230 - COMPUTERS / Software Development and Engineering / General
Publisher: Packt
Published: 2017-07-26T10:22:40+00:00


Android tabs and fragments

We've looked at quite a bit, but there is still a fair bit we haven't seen, such as the implementation for TwitterClient, as well as any details on the integration of networks, such as Instagram, which we saw in the last chapter. For the most part, TwitterClient is identical to what we saw in Chapter 5, Sunago - A Social Media Aggregator. The only major difference is in the use of the stream APIs. Some APIs are only available in certain Android versions, specifically, version 24, also known as Nougat. Since we're targeting Lollipop (SDK version 21), we are unable to use them. That aside, the internal logic and API usage are identical. You can see the details in the source repository. Before we finish, though, we need to take a look at the Twitter preferences screen, as there are some interesting items there.

We'll start with a tab layout activity, as follows:

public class PreferencesActivity extends AppCompatActivity { private SectionsPagerAdapter sectionsPagerAdapter; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_preferences); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.container); viewPager.setAdapter(sectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); }

For making a tabbed interface, we need two things--FragmentPagerAdapter and ViewPager. The ViewPager is a user-interface element that actually shows the tabs. Think of it as ListView for tabs. The FragmentPagerAdapter, then, is like CursorAdapter for the tabs. Instead of an SQL-backed data source, though, FragmentPagerAdapter is an adapter that represents pages as Fragments. In this method, we create an instance of our SectionsPagerAdapter, and set it as the adapter on our ViewPager. We also associate the ViewPager element with the TabLayout.

SectionsPagerAdapter is a simple class, and is written as follows:

public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0 : return new TwitterPreferencesFragment(); case 1 : return new InstagramPreferencesFragment(); default: throw new RuntimeException("Invalid position"); } } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Twitter"; case 1: return "Instagram"; } return null; } }

The method getCount() tells the system how many tabs we support, the title for each tab that is returned by getPageTitle(), and the Fragment representing the selected tab is returned from getItem(). In this example, we create a Fragment instance as needed. Note, we hint at Instagram support here, but its implementation looks strikingly similar to the Twitter implementation, so we won't go into detail on that here.

TwitterPreferencesFragment looks as follows:

public class TwitterPreferencesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate( R.layout.fragment_twitter_preferences, container, false); } @Override public void onStart() { super.onStart(); updateUI(); }

Fragments have a slightly different lifecycle than an Activity. Here, we inflate the view in onCreateView(), then we update the user interface with the current state from onStart(). What does the view look like? That's determined by R.layout.fragment_twitter_preferences.

<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" 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:orientation="vertical"> <Button android:text="Login" android:layout_width="wrap_content" android:layout_height="wrap_content"



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.