android - ActionBar compact with 3 tabs : issue with fragments

标签 android android-fragments tabs android-actionbar-compat

我目前正在一个新的 android 项目中工作。

我正在使用 ActionBar CompactTab 导航模式

我在 Activity 中添加了 3 个标签

在第一个 Fragment 中,有一个 listView 和 TextView(女巫包含列表中选定行的文本)

现在,当我选择第二个选项卡( fragment (空))并返回到我的第一个 fragment 时,TextView 包含所选值 但是 当我选择第三个选项卡( fragment 3 为空)并返回到我的第一个 fragment 时,TextView 已初始化。 我认为我的问题出在 TabListener

谁能帮帮我! 这是代码(简化)

主要 Activity :

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

        /** Getting a reference to action bar of this activity */
        mActionbar = getSupportActionBar();     

        /** Set tab navigation mode */
        mActionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        /** set HomeButton to true */
        mActionbar.setHomeButtonEnabled(true);

        /** Getting a reference to ViewPager from the layout */
        mPager = (ViewPager) findViewById(R.id.pager);

        /** Getting a reference to FragmentManager */
        FragmentManager fm = getSupportFragmentManager();

        /** Defining a listener for pageChange */
        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
                @Override
                public void onPageSelected(int position) {
                        super.onPageSelected(position);
                        mActionbar.setSelectedNavigationItem(position);
                }

        };

        /** Setting the pageChange listener to the viewPager */
        mPager.setOnPageChangeListener(pageChangeListener);

        /** Creating an instance of FragmentPagerAdapter */
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the FragmentPagerAdapter object to the viewPager object */
        mPager.setAdapter(fragmentPagerAdapter);

        mActionbar.setDisplayShowTitleEnabled(true);

        /** Defining tab listener */
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

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

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

                        }

                        @Override
                        public void onTabReselected(Tab tab, FragmentTransaction ft) {
                        }
                };

                /** Creating fragment1 Tab */
                Tab tab = mActionbar.newTab()
                                   .setText("Categories")                                   
                                   .setTabListener(tabListener);

                mActionbar.addTab(tab, 0, false);

                /** Creating fragment2 Tab */
                tab = mActionbar.newTab()
                               .setText("Acceuil")                               
                               .setTabListener(tabListener);

                mActionbar.addTab(tab, 1, true);  
                /** Creating fragment3 Tab */
                tab = mActionbar.newTab()
                               .setText("Services")                               
                               .setTabListener(tabListener);

                mActionbar.addTab(tab, 2, false);       


    }

fragment 2::

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        categories = inflater.inflate(R.layout.fragment_categories, container, false);
         list= (ListView)categories.findViewById(R.id.listCategories);
          // Defined Array values to show in ListView
         String[] values = new String[] { "Android List View", 
                                          "Adapter implementation",
                                          "Simple List View In Android",
                                          "Create List View Android", 
                                          "Android Example", 
                                          "List View Source Code", 
                                          "List View Array Adapter", 
                                          "Android Example List View" 
                                         };

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);
         // Assign adapter to ListView
         list.setAdapter(adapter); 
        t =(TextView)categories.findViewById(R.id.textView1);
         list.setOnItemClickListener(new OnItemClickListener()
         {
             public void onItemClick(AdapterView<?> parent,View v, int position, long id)
          {
                 Toast.makeText(getActivity().getBaseContext(),"category " + (position + 1) +" selected",Toast.LENGTH_SHORT).show();
                    t.setText("category " + (position + 1) +" selected");


           }
         });
        return categories;
    }

屏幕截图

当我从 listView 中选择行时( fragment 1)==> fragment 2 ==> 返回 fragment 1

enter image description here

当我从 listView 中选择行时( fragment 1)==> fragment 3 ==> 返回 fragment 1

enter image description here

最佳答案

在主 Activity 的 onCreate() 中,初始化 ViewPager 后,添加:

mPager.setOffscreenPageLimit(2);

根据 Android docs , setOffscreenPageLimit():

Sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

由于默认设置为 1,当您从第一个选项卡开始并导航到第三个选项卡时,当您导航回第一个选项卡时,将重新创建第一页(即 fragment )。将此设置为 2 应该会保留第一个选项卡中的 fragment ,即使您导航到第三个选项卡也是如此。

关于android - ActionBar compact with 3 tabs : issue with fragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23794684/

相关文章:

java - 如何在写入文本文件时使用 "tab space"

android - 在 GPX 或 KML 中指定精度

android - 在 Android 中,从 Firebase 存储下载的文件存储在哪里?

android - 如何设置每个 fragment 在屏幕中的百分比

Android从其父 Activity 刷新 fragment 列表

Vim - 用制表符缩进多行

javascript - Android 版格子链接

android - 实现应用内 bp 后,如何解锁应用内的付费 flavor

java - 对话框 fragment 使用改造更改密码

Flutter:如何更改幻灯片上的选项卡图标颜色,而不仅仅是点击?