首页 \ 问答 \ 碎片重新初始化初始选项卡上的内容 - ANDROID(Fragments Re-Initializing content on Initial tab selection - ANDROID)

碎片重新初始化初始选项卡上的内容 - ANDROID(Fragments Re-Initializing content on Initial tab selection - ANDROID)

我有一个应用程序使用ViewPager作为TabHost。 问题是,当应用程序启动时,全部显示正常,在所有3个选项卡之间滑动。

但是,重新选择第一个选项卡后,应用程序会重新初始化,但不会显示正确的内容,应用程序应该保留碎片当前状态而不是重新初始化。

码:

TabAdapter.java

public class TabAdapter extends FragmentPagerAdapter{


public TabAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

    switch(position){
        case 0:
            return new MapFragmentView();
        case 1:
            return new LogView();
        case 2:
            return new SettingsView();
    }

    return null;
}

@Override
public int getCount() {
    return 3;
}

}

MapsActivity.java(主要活动)

public class MapsActivity extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPage;
private TabAdapter myAdapter;
private ActionBar actionBar;
public static FragmentManager fragmentManager;

private String[] tabNames = {"Map", "Log" , "Settings"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    fragmentManager = getFragmentManager();

    viewPage = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    myAdapter = new TabAdapter(getSupportFragmentManager());

    viewPage.setAdapter(myAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabNames) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPage.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPage.setCurrentItem(tab.getPosition());
}
}

初始视图:

标签的初始视图

重新选择地图后的实际结果标签:

在重新选择地图选项卡

预期结果:

究竟应该发生什么


我知道在TabAdapter文件中,每次调用getPosition时都会创建新实例,所以这可能是问题所在。

任何帮助将不胜感激!


I have an application that is using a ViewPager that acts as a TabHost. The issue is, when the application launches, all displays fine, swiping between all 3 tabs.

However upon reselecting the 1st tab, the application gets reinitialized but does not display the correct content, the app should retain the Fragments current state instead of Re-initializing.

Code:

TabAdapter.java

public class TabAdapter extends FragmentPagerAdapter{


public TabAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

    switch(position){
        case 0:
            return new MapFragmentView();
        case 1:
            return new LogView();
        case 2:
            return new SettingsView();
    }

    return null;
}

@Override
public int getCount() {
    return 3;
}

}

MapsActivity.java (Main Activity)

public class MapsActivity extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPage;
private TabAdapter myAdapter;
private ActionBar actionBar;
public static FragmentManager fragmentManager;

private String[] tabNames = {"Map", "Log" , "Settings"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    fragmentManager = getFragmentManager();

    viewPage = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    myAdapter = new TabAdapter(getSupportFragmentManager());

    viewPage.setAdapter(myAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabNames) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPage.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPage.setCurrentItem(tab.getPosition());
}
}

Initial View :

Initial View of Tab

Actual Result after Re-selecting Maps Tab:

Upon Reselecting Maps Tab

Expected Result:

What should actually happen


I understand that in the TabAdapter file, we are creating New Instances everytime the getPosition is called, so that might be the issue.

Any help will be greatly appreciated!


原文:https://stackoverflow.com/questions/31806420
更新时间:2022-04-03 13:04

最满意答案

使用帮助图标更改div的CSS以包含display: inline-block; ,这将使其适合其内容。 div默认display: block; 它横跨整条线。 不要绝对定位div ,使用默认的静态位置,让它float到你想要的任何地方。 文本将围绕它流动,当帮助div的大小在悬停时发生变化时,它会将文本推到一边,文本将围绕它重新流动。

以下是float一些文档: https//developer.mozilla.org/en-US/docs/Web/CSS/float


Change the CSS for the div with the help icon to include display: inline-block;, which will cause it to fit its contents. div is by default display: block; which stretches across the entire line. Do not position the div absolutely, use the default static position, and have it float to wherever you want. The text will flow around it, and when the size of the help div changes on hover, it will push the text aside and the text will reflow around it.

Here is some documentation for float: https://developer.mozilla.org/en-US/docs/Web/CSS/float

相关问答

更多
  • 有position: fixed;东西position: fixed; 从不推动静态(或相对)内容。 要么有造型,以确保它们不重叠...或重叠。 看到这个例子JSFiddle ,它使用你的CSS。 问题在于别的地方。 标题如何不与第一页中的文本重叠? 你有什么推动文本? 边距? 什么? 是否有可能分享原始或简化的例子? (您可以使用JSFiddle 。) Something with position: fixed; never pushes static (or relative) content. Ei ...
  • 如果要填充没有绝对位置的窗口,请将体边距设置为0 从#maincontainer和#slider中删除绝对位置,然后您可以从#slider中删除margin-top。 div正在崩溃,因为你没有内容,请尝试添加以下内容: body { margin:0; } .containers { height:50px; background: blue; margin-top: 5px; } 您现在将看到您的4个容器。 Set body margin to 0 if you wan ...
  • 始终为position: absolute;设置top和left属性position: absolute; 因为浏览器会尝试猜测它,有时它不是你想要的。 元素实际宽度也包括width + padding ,所以当你设置width: 105mm; padding: 10px; width: 105mm; padding: 10px; 比你的实际宽度是105mm + 20px body { background: rgb(204, 204, 204); } page[size="A4"] { b ...
  • 根据我的评论的答案,你不希望sectionone有position:fixed; 。 我已将/* ### */放在我添加的CSS旁边,并注释掉了需要删除的内容。 基本上我已经为html / body添加了一些重置规则,然后在wrapper添加了20%的左边距。 其他元素自然地靠近它。 html, body { /* ### */ margin:0; padding:0; height:100%; width:100%; } body { backgrou ...
  • 为此,您可以使用position: absolute; 为重叠的div 。 并添加任何文本到该div。 这将是这样的:
    The text to be added, will be written here! :)
    你可以使用class或id来设计它们! 在你的图片中,你使用的是position: absolute; 图像标签也是如此。 ...
  • 使用帮助图标更改div的CSS以包含display: inline-block; ,这将使其适合其内容。 div默认display: block; 它横跨整条线。 不要绝对定位div ,使用默认的静态位置,让它float到你想要的任何地方。 文本将围绕它流动,当帮助div的大小在悬停时发生变化时,它会将文本推到一边,文本将围绕它重新流动。 以下是float一些文档: https : //developer.mozilla.org/en-US/docs/Web/CSS/float Change the CSS ...
  • 使用bootstrap CSS,您可以使用float:left设置该信息的容器float:left您可能需要清除浮动以将每个表单保持在单独的行中; 用这个: .form-group:after { content: " "; display:block; clear:both; } 演示http://jsfiddle.net/Q2FHr/2/ With the bootstrap CSS you are setting the containers of that information wi ...
  • 你需要改变所有的position: fixed; position: relative; 用于在屏幕尺寸发生变化时可以调整大小的元素。 fixed维护
    标签的起始位置,并可以从那里增长和缩小。 所以,当你缩小到手机屏幕时,你所拥有的文本的大小会导致包含它们的
    调整大小并重叠下一个
    。 当您将position更改为relative position时,
    将按顺序排列,如果导致
    被调整大小,则会调整后跟
    ,以防止重叠。 你可以看到它修改了编辑小提琴: htt ...
  • 侧栏需要有一个绝对位置,请阅读: https : //developer.mozilla.org/en-US/docs/Web/CSS/position
    尝试使用class row-fluid而不是row 。 Try use class row-fluid instead of row.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。