Android Programming

Android Fragments ListView Tutorial.

Android Fragments : With ListViews - How to Show and Navigate them via Material Tabs

Fragments are sub-activities and hence powerful.They have their own lifecycle like activities.In fact Fragments define their own View Hierarchy.That means you can throw whatever views you like in layout and they shall get displayed just as you normally do with your activities.One common adapterview normally used of course is the ListView.The ListView has been a core component of the android ecosystem since time immemorial.ListView is generally easy to use and very reasonable for mobile devices and modern app development as a whole.Most apps work with some sort of data.Data from databases,from servers online etc.These data need a good way of presentation so the user can easily view whatever he wants.And ListView provide that perfectly.Lists are scrollable and can contains a lot of information.Moreover ListViews are reasonably customizable.You easily customize them to display images and text as these are the most common data formats. Now there is the ListFragment that comes with a ListView inbuilt.However,its also important to explicitly define your ListView and reference from your code before we move to ListFragment.Thats the purpose of this tutorial.To show a ListView in fragments,multiple fragments that is,with different listViews.Our fragments shall be swipeable.Moreover user can switch through them using material tabs.  

What we do :

  • Three fragments with 3 different ListViews.
  • We can navigate through the fragments either by clicking tabs or by swiping.
  • The tabs are material and nice.
  • We use support.v4.View.ViewPager to enable our swiping/sliding feature.
  • Each ListView is going to have different data set.
  • We'll explicitly add the ListViews in our layout specification.
  • Then in the fragment classes we shall be inflating those layouts inside our onCreatView() method.
  • To use fragments as our pages we need FragmentPagerAdapter.
  • It helps in binding our pages/fragments to our tabs.
  • FragmentPageAdapter belongs to support.v4 libraries.

What You do :

  • Create a project in android studio.
  • Give it a name and choose minimum and target SDKs.
  • Personally I used 15 and 23 as visible in my build.gradle below.
  • Then choose basic activity as your template layout.
  • All these are not requirements and you can choose whatever you like.