android 多tab 保存Fragment状态


最近要实现一个功能,实现多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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值