topsapps is a reporting platform for mobile developers that automatically downloads and visualizes sales data, App Store reviews, hourly ranks and more

Android Combining Tab Layout and List View

Android Develop MarjoriePalumbo 44℃ 0评论

In Android Tab Layout Tutorial i explained how to implement a tab view. This tutorial is about implementing list view inside a tab layout. Everything is same except tabs will have listview inside it. List data is displayed by fetching json by making http request. I took an example of simple mailbox which will contains two list views for inbox, outbox messages and a profile tab.

Android Combining Tab Layout and List View

In this example i am displaying list view in each tab where the list data is fetched from a JSON.

Get Inbox JSON here

 {     "messages": [         {                 "id": "1",                 "from": "Android Hive",                 "email": "androidhive@gmail.com",                 "subject": "New Subscriber..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 9"         },   {                 "id": "2",                 "from": "Pay Pal",                 "email": "paypal@paypal.com",                 "subject": "Payment Notification",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 7"         },         .         .      ] } 

Get Outbox JSON here.

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

And get Profile JSON here

 {     "profile": {         "id": "1",         "name": "Ravi Tamada",         "email": "ravi8x@gmail.com",         "mobile": "+91 0000000000",         "address": "xxx - xxx -xxxxxx"     } } 

Create new Project

So let’s start with creation of new project in Eclipse IDE.

1. Create a new project File -> New -> Android Project and fill out required details.
2. Open your Main Activity and extend the class from TabActivity.

 public class AndroidTabAndListView extends TabActivity { 

3. Now open your main.xml under res -> layout folder and type the following code to create a tabview.

 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@android:id/tabhost"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <LinearLayout         android:orientation="vertical"         android:layout_width="fill_parent"         android:layout_height="fill_parent">         <TabWidget             android:id="@android:id/tabs"             android:layout_width="fill_parent"             android:layout_height="wrap_content" />         <FrameLayout             android:id="@android:id/tabcontent"             android:layout_width="fill_parent"             android:layout_height="fill_parent"/>     </LinearLayout> </TabHost> 

4. Create three activities for 3 tabs. I am creating 3 activities as InboxActivity.java, OutboxActvity.java and ProfileActivity.java and extend the classes from ListActivity.

 package com.example.androidhive;  import android.app.ListActivity; import android.os.Bundle;  public class InboxActivity extends ListActivity {     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.inbox_list);     } } 
 package com.example.androidhive;  import android.app.ListActivity; import android.os.Bundle;  public class OutboxActivity extends ListActivity {     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.outbox_list);     } } 

Note that ProfileActivity.java in not ListActivity.

 package com.example.androidhive;  import android.app.Activity; import android.os.Bundle;  public class ProfileActivity extends Activity {     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.profile);     } } 

5. Now create xml layouts for the list views. Here we have two list views and a normal layout. So create totally 5 xml layouts. inbox_list.xml, outbox_list.xml, profile.xml, inbox_list_item.xml (inbox listview single row item), outbox_list_item.xml (outbox listview single row item)

inbox_list.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <ListView         android:id="@android:id/list"         android:layout_width="fill_parent"         android:layout_height="wrap_content" />  </LinearLayout> 

inbox_list_item.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <!-- From Label -->     <TextView         android:id="@+id/from"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:paddingTop="8dip"         android:paddingLeft="8dip"         android:paddingBottom="4dip"         android:textSize="20dip"         android:textStyle="bold" />          <!-- Mail Subject -->     <TextView android:id="@+id/subject"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:paddingLeft="8dip"         android:paddingBottom="6dip"         android:textSize="15dip"         android:layout_below="@id/from"/>          <!-- Mail date -->     <TextView android:id="@+id/date"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:padding="8dip"/>/   </RelativeLayout> 

outbox_list.xml

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

0

outbox_list_item.xml

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

1

profile.xml

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

2

6. Each and every tab needs an icon so design icons for each tab. We need three dimensions of each icon. Design each icon in 48 x 48 px, 32 x 32 px and 24 x 24 px and place them in drawable-hdpi, drawable-mdpi and drawable-ldpi respectively. See following diagram for your guidance

Android Combining Tab Layout and List View

7. Android icon states will be define in xml files with default and hover state configurations. For three icons we need the icon state configuration files. So create three 3 xml files under drawable-hdpi directory. Type the following code for icon states.

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

3

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

4

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

5

8. Now open AndroidTabAndListView.java which is Main Activity and type the following code. In the following code we are creating three TabSepcs and adding them to TabHost.

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

6

9. Now open your project make sure that you an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below. Also don’t forgot to add INTERNET Permissions as we are getting JSON by making http request.

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

7

10. If you run your project now you will see tabs running without listviews. So all we need to do is implement listview in each tab activity. So open you individual tab activity classes and try to implement listview. If you are new to ListView i suggest you to go through Android ListView Tutorial once.

InboxActivity.java

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

8

OutboxActivity.java

 {     "messages": [         {                 "id": "1",                 "to": "dine@gmail.com",                 "subject": "Movie Tickets Confirmation..",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 22"         },   {                 "id": "2",                 "to": "william@gmail.com",                 "subject": "Project estimataion details",                 "message" : "Tutorial about combing Android Tabview and ListView",                 "date": "May 21"         },         .         .         .     ] } 

9

ProfileActivity.java

 {     "profile": {         "id": "1",         "name": "Ravi Tamada",         "email": "ravi8x@gmail.com",         "mobile": "+91 0000000000",         "address": "xxx - xxx -xxxxxx"     } } 

0

11. Finally create a class called JSONParser.java and paste the following code as we need a parser class to parse the json.

 {     "profile": {         "id": "1",         "name": "Ravi Tamada",         "email": "ravi8x@gmail.com",         "mobile": "+91 0000000000",         "address": "xxx - xxx -xxxxxx"     } } 

1

12. Now Run & Test your project.

Android Combining Tab Layout and List View
Android Combining Tab Layout and List View
Android Combining Tab Layout and List View
This image is for thumbnail purpose
Android Combining Tab Layout and List View

转载请注明:TopsApps » Android Combining Tab Layout and List View

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址