本文介绍了如何在Android上实现嵌套的主细节流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,在一个列表内,在一个列表内,依此类推.大约有5层.

I have a list, within a list, within a list, and so on. There's about 5 tiers.

为手机上的每个列表创建5个活动很容易,但是如果我也想支持平板电脑怎么办?因此,我需要使用主细节流程.

It's easy enough to create 5 activities for each list on phones, but what if I want to support tablets as well? So I'd need to work with master detail flow.

但是,我似乎找不到与嵌套主细节流程有关的任何教程或信息.

However, I can't seem to find any tutorials or information in relations to a nested master detail flow.

无论如何,这是我所描述的例子:

Anyway, here is an illustration of what I'm describing:

在平板电脑布局中,我希望屏幕一次移动2层.用户可以通过从右边的层中选择一个列表项来前进到下一层.要返回上一层,用户可以点击后退"按钮.

In the tablet layout, I want the screen to shift 2 tiers at a time. User can advanced to the next tier by selecting a list item from the right tier. To go back to the previous tier, user can tap the back button.

有什么想法可以实现吗?

Any idea how I can achieve this?

推荐答案

经过一整天的互联网搜索,我终于找到了解决方案.要获得嵌套主详细信息流"效果,只需将ViewPager与FragmentPageAdapter一起使用.主明细流将如下所示:

After a full day scouring the internet, I finally found a solution. To get a "Nested Master Details Flow" effect simply use a ViewPager with FragmentPageAdapter. The Master Detail Flow will look like this:

要在用户切换到横向时切换到两个面板模式,请在扩展的FragmentPagerAdapter类中重写以下方法:

To change to a two panel mode when the user switches to landscape, in your extended FragmentPagerAdapter class, override the following method:

@Override
public float getPageWidth(int position) {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    // if the width is greater than 900dp halve the width of the page
    if ((metrics.widthPixels / metrics.density) > 900) {
        return (0.5f);
    }
    return super.getPageWidth(position);
}

为视图寻呼机提供向上按钮":

To provide an "up button" for the view pager:

viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    // This method will be invoked when a new page becomes selected.
    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        } else {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

    }
});

您可以告诉向上按钮"返回这样的页面(其中viewpager是您的活动的成员变量,其中包含对ViewPager的引用):

You can tell the "up button" to go back a page like this (where viewpager is a member variable of your activity, holding the a reference to your ViewPager):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int position = viewpager.getCurrentItem();
    if (position > 0) viewpager.setCurrentItem(position-1);
    return true;
}

参考:

带有FragmentPagerAdapter的ViewPager

在操作栏上显示后退按钮

多视图ViewPager选项

如何实现具有不同片段/布局的ViewPager 示例github项目

这篇关于如何在Android上实现嵌套的主细节流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:06