Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint
来源:互联网 发布:midi打击垫 编程 编辑:程序博客网 时间:2024/05/16 15:44
This article is a follow up of Use Android ActivityGroup within TabHost to show different Activity. As you probably noticed or read in the comments, the provided solution in the last article was less than ideal. Even though it does work, it uses multiple activies and therefor has a bigger memory consumption than necessary.
What happens when you use ActivityGroup with Tabhost
In the previous example we had one Activiy per tab. One tab was even holding two Activies and using ActivityGroup to switch between them. That's a total of five Activites that end up on the activity stack. Also the hierarchy was more nested than needed.
The improvement
So instead of using a Activity per Tab we can display a View in each. Thus we only need one Activity, all the events are caught by the class, so you need to find an ordered way of catching them. My example will implement the OnClickListener.
To keep the example short, we have two tabs. In the first one, there's a ViewFlipper which switches between two TextViews when clicked. In the second tab, there's just a bit of text and a button.
Here's the code:
- package com.unitedcoders.android.examples;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TabHost;
- import android.widget.Toast;
- import android.widget.ViewFlipper;
- public class Tabs extends TabActivity implements OnClickListener {
- Button doSomething;
- TabHost tabHost;
- ViewFlipper flipper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tablayout_1);
- doSomething = (Button) findViewById(R.id.btn_do_something);
- doSomething.setOnClickListener(this);
- flipper = (ViewFlipper) findViewById(R.id.layout_tab_one);
- flipper.setOnClickListener(this);
- tabHost = getTabHost();
- tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1").setContent(R.id.layout_tab_one));
- tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2").setContent(R.id.layout_tab_two));
- tabHost.setCurrentTab(0);
- }
- @Override
- public void onClick(View v) {
- // show a toast in second tab
- if (v == doSomething) {
- Toast.makeText(getApplicationContext(), "doing something", Toast.LENGTH_SHORT).show();
- }
- // toggle TextView in first tab
- if (v == flipper) {
- flipper.showNext();
- }
- }
- }
and the layout that goes with it:
- <?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">
- <ViewFlipper android:id="@+id/layout_tab_one"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <TextView android:id="@+id/tabone1" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:text="this is a tab one" />
- <TextView android:id="@+id/tabone2" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:text="this is the other tab one" />
- </ViewFlipper>
- <LinearLayout android:id="@+id/layout_tab_two"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/textview2" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="this is a tab two"/>
- <Button android:id="@+id/btn_do_something" android:text="do something"
- android:layout_width="fill_parent" android:layout_height="wrap_content"/>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
From this UI you can use AsyncTask to do your processing and refresh the UI. This will keep your number of Activities and the memory consumption low.
As a little bonus, here are the screenshots of the hierachy view for the
current example and the previous one.
The code is in the UCdroid project on github. You can run it on your device or emulator.
- Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint
- Use Android ActivityGroup within TabHost to show different Activity
- Swipe Views with Tabs
- android-Implementing Effective Navigation,Creating Swipe Views with Tabs
- Creating Swipe Views with Tabs
- Compile with ARM Thumb2 to Reduce Memory Footprint and Improve Performance
- Optimize memory footprint with the string intern pool
- TabHost requires a TabWidget with id "android:id/tabs" 报错解决办法
- android tab with interacting map and list views
- ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView
- android viewflipper和tabhost效率对比
- iPhone Tabs for Android!
- Swipe Views with Tabs(左右滑动切换导航)
- Printing with Documents and Views
- Butter Knife Field and method binding for Android views
- 解决:[Android UI界面] 创建TabHost XML图形视图一直出错看不到内容?TabHost requires a TabWidget with id "android:id/tabs".
- Android Tabhost with FragmentActivity
- for different situations and consequences
- 上传文件中应当注意的细节
- DBCP开源数据库连接池的编写源码
- Smarty案例
- 合作开发小结
- Jndi技术----与数据库的连接(tomcat)
- Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint
- myeclipse包资源管理器展示问题
- 使用元数据简化jdbc代码---查询操作(用到反射)
- 使用元数据简化jdbc代码
- 基本数据库并发操作
- java笔试题(引自传智播客)-下
- NDK JNI LOG
- Apache---DBUtils框架简介
- smarty