Viewpageindicator inside fragment
来源:互联网 发布:localstorage用法 php 编辑:程序博客网 时间:2024/04/29 10:21
1down votefavorite
With a SherlockActionBar i've created a nice interface. In landscape mode you see both listfragment and "detailsfragment".
Now I would like to a add a viewpageindicator (https://github.com/JakeWharton/Android-ViewPagerIndicator) inside the detailsframe, so i would look like this:
--------------------------||_1___|TAB1 | TAB2 | ETC ||_2___|-------------------||_3___|Example: content ||_4___| TAB1 of listitem 1| |_5___|___________________|
I hope this is clear. So basicly I would a FragmentManager/TabPageIndicator inside a fragment.
I this possible? I can't figure it out.. :(
Thank you!
2 Answers
You should not put a Fragment
inside a Fragment
. You can however put a ViewPager
inside a fragment and get the result you're looking for.
I was facing the same issue a while ago for one of my apps (only I wanted the ViewPager
to contain some ListView
s). This was the end result (see the lines; red contains Fragment
, blueViewPagerIndicator
, green ViewPager
):
I accomplished this with the following steps. Fist, for the detail-fragment, I created the following XML file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.viewpagerindicator.TabPageIndicator android:id="@+id/viewpagerIndicator" android:layout_height="wrap_content" android:layout_width="match_parent" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>
Then, I set up a custom PagerAdapter to populate the ViewPager and the TabPageIndicator:
public class MainPagerAdapter extends PagerAdapter implements TitleProvider { // constructor ... @Override public void destroyItem(ViewGroup container, int position, Object object) { // remove the object (we don't want to keep it in memory as it will get recreated and cached when needed) ViewPager viewPager = (ViewPager) container; View view = (View) object; viewPager.removeView(view); } @Override public Object instantiateItem(View pager, final int position) { // inflate your 'content'-views here (in my case I added a listview here) // similar to a listadapter's `getView()`-method View wrapper = ... // inflate your layout ... // fill your data to the appropriate views ((ViewPager) pager).addView(wrapper); } public String getTitle(int position) { ... // return the title of the tab }}
Next, in my fragment, I set the following:
public class MainFragment extends Fragment { private MainPagerAdapter pagerAdapter; private TabPageIndicator mIndicator; private ViewPager viewPager; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View detailsFrame = context.findViewById(R.id.details_fragment); // setup your adapter: pass data, etc. this.pagerAdapter = new MainPagerAdapter(...); this.viewPager = (ViewPager) context.findViewById(R.id.viewpager); viewPager.setAdapter(pagerAdapter); this.mIndicator = (TabPageIndicator) context.findViewById(R.id.viewpagerIndicator); this.mIndicator.setViewPager(viewPager); }}
0down vote
You can't nest fragments inside fragments. It will be displayed fine, but it will crash when you will want to replace some of them using transactions.
Have a look a these:
- Problem putting a fragment into another fragment
- Android: Can you nest Fragments?
- Viewpageindicator inside fragment
- viewpageindicator
- ViewPageIndicator + ViewPager + Fragment实现类似网易新闻客户端Tab标签
- ViewPageIndicator + ViewPager + Fragment实现类似网易新闻客户端Tab标签
- Fragment使用(三):fragment+viewPageIndicator的组合使用,类似Tabhost
- viewpageindicator +fragment 在网络访问中与volley框架 null问题
- ViewPageIndicator 知识点
- 自定义ViewPageIndicator
- Using TabLayout inside a Fragment; tab text invisible
- ViewPageIndicator开源框架
- ViewPageIndicator的使用
- Updating a ListView inside a Fragment placed on a ViewPager on DialogFragment dismiss
- viewpageIndicator多页面滑动切换
- viewpageIndicator多页面滑动切换
- ViewPageIndicator和viewpager一起使用
- android ViewPageIndicator指示器源码研究
- viewpageindicator 加不到Eclipse Maven的原因
- Android 开源框架ViewPageIndicator 和 ViewPage
- SAP 设置币别转换率
- Syncml协议简述
- UNIX SOCKET编程简介
- Errors occurred during the build.
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Viewpageindicator inside fragment
- linux内核sk_buff的结构分析
- TCP 三次握手
- XCode 4.3.X一个小众Bug及解决方法
- SAP 如何设置采购收货直接转到供应商库存
- c#中用DirectShow实现媒体播放器的核心(0) 完成后的代码
- VS2005增加一个Direct show fitter 的配置
- 设计模式--策略模式
- 在一个8×8格的国际象棋盘上,最多放置5个皇后就可以控制整个局面,即无论对方的棋子放在哪一个都会被吃掉请编出五个皇后的分布