android 多tab 保存Fragment状态

来源:互联网 发布:网络机柜配线架的安装 编辑:程序博客网 时间:2024/06/07 00:37


最近要实现一个功能,实现多Tab, 不能像ViewPager那样自由滑动,只能通过点击Tab切换,类似新浪微博客户端那样底部的tab切换,我的是在顶部,而且每个tab里面都是listview, 在切换tab的时候要保持tab的状态,就是说:无论切换哪个tab,listivew滑动到哪里,都要保持原来的状态~

开始我打算用FragmentTabHost来实现,我之前特意用了一天的时间调研了一下,见:

http://blog.csdn.net/shichaosong/article/details/23286997

但是用这个一个不好的地方就是:定制UI太复杂了,非常麻烦~当然是可以做到的,可以参考上面的链接~(非常麻烦,很麻烦,其实还好了,我是有多纠结~)


然后问了下同事,同事说最好不要用,用2个button就可以,因为我的每个子页面都是一个Fragment,所以在切换的时候replace就可以了。后来却是用2个Button实现的,但是在切换的时候发现不能保留Fragment状态,每次都要从新初始化Framgent,这肯定不行啊。后来根据同事的一个分享文档中看到,可以这样:注意红色字体部分~就可以实现保存Fragment状态了。

getFragmentManager().beginTransaction()
.replace(R.id.web_page_realcontent, f, f.getClass().getSimpleName())
.addToBackStack(f.getClass().getSimpleName())
.commit();


思考:

1.如果我的多tab数目是不确定的怎么办?有多个页面的tab有个是2个,有的是3个或者4个呢?难道每个人都写一个LinearLayout,里面放上几个Button么?应该重写一个自定义的tab bar。


tab可以使用下面的实现:

android:button="@null"

android:background="@drawable/filter_radio_button_left_selector"

这样的话,RadioButton原来自带的那种效果就没有了,好丑,然后用我们自己的图片作为背景了~


<LinearLayout
android:id="@+id/group_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/distance_to_border"
android:layout_marginRight="@dimen/distance_to_border"
android:layout_marginBottom="@dimen/distance_to_bottom"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/filter_all"
android:button="@null"
android:gravity="center"
android:layout_height="@dimen/filter_tab_height"
android:layout_width="0dip"
android:layout_weight="1"
android:singleLine="true"
android:background="@drawable/filter_radio_button_left_selector"
android:textSize="@dimen/download_text_size"
android:textColor="@color/filter_bar_unchecked_button_text_color"
android:text="@string/all_fileter" />

  

<RadioButton
android:id="@+id/filter_other"
android:button="@null"
android:gravity="center"
android:layout_height="@dimen/filter_tab_height"
android:layout_width="0dip"
android:layout_weight="1"
android:singleLine="true"
android:background="@drawable/filter_radio_button_right_selector"
android:textSize="@dimen/download_text_size"
android:textColor="@color/filter_bar_unchecked_button_text_color"
android:text="@string/other_filter" />

</RadioGroup>
</LinearLayout>



android之merge布局

http://www.cnblogs.com/travelfromandroid/articles/2133206.html


自定义RadioGroup,实现RadioButton带键值对~

http://www.360doc.com/content/11/0806/19/7322578_138528926.shtml


农民伯伯的文档:RadioGroup

http://www.cnblogs.com/over140/archive/2010/11/10/1873254.html


RadioButton去掉单选按钮,使用自己自定义的图片

http://bbs.51cto.com/thread-954128-1.html


实现RadioGroup多行多列

http://jasonshieh.iteye.com/blog/1972131

http://blog.csdn.net/frj463806056/article/details/20791189


0 0
原创粉丝点击