Viewpageindicator inside fragment

简介:

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

up vote 4 down vote accepted

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 ListViews). This was the end result (see the lines; red contains Fragment, blueViewPagerIndicator, green ViewPager):

Example ViewPager inside a Fragment

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);
    }
}

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:

本文来自:


相关文章
|
3天前
|
人工智能 运维 安全
|
1天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
8天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
770 109
|
3天前
|
机器学习/深度学习 传感器 算法
Edge Impulse:面向微型机器学习的MLOps平台——论文解读
Edge Impulse 是一个面向微型机器学习(TinyML)的云端MLOps平台,致力于解决嵌入式与边缘设备上机器学习开发的碎片化与异构性难题。它提供端到端工具链,涵盖数据采集、信号处理、模型训练、优化压缩及部署全流程,支持资源受限设备的高效AI实现。平台集成AutoML、量化压缩与跨硬件编译技术,显著提升开发效率与模型性能,广泛应用于物联网、可穿戴设备与边缘智能场景。
175 127
|
3天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
230 152
|
5天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
212 127
|
4天前
|
机器学习/深度学习 存储 资源调度
CMSIS-NN:ARM Cortex-M处理器的高效神经网络内核——论文解读
CMSIS-NN是专为ARM Cortex-M系列微控制器优化的神经网络计算内核库,旨在支持资源受限的物联网边缘设备进行高效的深度学习推理。该库通过对卷积、池化、全连接层等关键操作进行定点量化、SIMD指令优化和内存布局调整,显著提升了模型在嵌入式设备上的运行效率。实验表明,CMSIS-NN在Cortex-M7处理器上的推理速度比基准实现提升了近5倍,大幅降低了功耗,为边缘AI应用提供了可行的技术路径。
224 128