问题描述
随着SherlockActionBar我创建了一个漂亮的界面。在横向模式下,你看到这两个listfragment和detailsfragment。
With a SherlockActionBar i've created a nice interface. In landscape mode you see both listfragment and "detailsfragment".
现在我想一个加viewpageindicator(https://github.com/JakeWharton/Android-ViewPagerIndicator)在detailsframe里面,所以我想是这样的:
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___|___________________|
我希望这是显而易见的。所以basicly我会一个FragmentManager / TabPageIndicator片段里。
I hope this is clear. So basicly I would a FragmentManager/TabPageIndicator inside a fragment.
我这可能吗?我不出来..:(
I this possible? I can't figure it out.. :(
感谢您!
推荐答案
您不应该把一个片段
在片段
。然而,你可以把一个 ViewPager
片段内,得到你要寻找的结果。
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.
我前一段时间面临着同样的问题对我的应用程序之一(仅我想要的 ViewPager
来包含一些的ListView
S)。这是最终的结果(见的线条,红色包含片段
,蓝色 ViewPagerIndicator
,绿色 ViewPager
):
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
, blue ViewPagerIndicator
, green ViewPager
):
我做到了这一点与下面的步骤。拳,对于细节的片段,我创建了下面的XML文件:
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>
然后,我建立了一个自定义的PagerAdapter填充ViewPager和TabPageIndicator:
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);
}
}
这篇关于Viewpageindicator片段内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!