java - 在选项卡更改上,Android如何停止刷新 Fragments

  显示原文与译文双语对照的内容
0 0

我有以下代码:

MainActivity.java


package com.erc.library;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.erc.sayeghlibrary.adapter.TabsPagerAdapter;

public class MainActivity extends FragmentActivity implements
 ActionBar.TabListener {

 private ViewPager viewPager;
 private TabsPagerAdapter mAdapter;
 private ActionBar actionBar;
//Tab titles
 private String[] tabs = {"Stories","Dictionaries","eBooks"};

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






 int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title","id","android");
 if (actionBarTitleId> 0) {
 TextView title = (TextView) findViewById(actionBarTitleId);
 if (title!= null) {
 title.setTextColor(Color.WHITE);
 }
 } 

//Initilization
 viewPager = (ViewPager) findViewById(R.id.pager);
 actionBar = getActionBar();
 mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

 viewPager.setAdapter(mAdapter);
 actionBar.setHomeButtonEnabled(false);
 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


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

/**
 * on swiping the viewpager make respective tab selected
 * */
 viewPager.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 onTabReselected(Tab tab, FragmentTransaction ft) {
 }

 @Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {
//on tab selected
//show respected fragment view
 viewPager.setCurrentItem(tab.getPosition());


 }

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






 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 MenuInflater inflater = getMenuInflater();
 inflater.inflate(R.menu.main, menu);
 return true;
 } 




 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
//action with ID action_refresh was selected
 case R.id.Favorites:
 Toast.makeText(this,"Favorites selected", Toast.LENGTH_SHORT)
. show();
 break;
//action with ID action_settings was selected

 default:
 break;
 }

 return true;
 } 


}

TabsPagerAdapter.java


package com.erc.library.adapter;

import com.erc.library.Dictionaries;
import com.erc.library.Ebooks;
import com.erc.library.Stories;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

 @Override
 public Fragment getItem(int index) {

 switch (index) {
 case 0:

 return new Stories();
 case 1:

 return new Dictionaries();
 case 2:
//Movies fragment activity
 return new Ebooks();
 }

 return null;
 }

 @Override
 public int getCount() {
//get item count - equal to number of tabs
 return 3;
 }

}

我正在制作一个库应用程序,问题是每次从第三个选项卡到第三个或者第三个。

时间: 原作者:

0 0

默认情况下,ViewPager 在你浏览页面时重新创建 Fragment 。 要防止这里问题,可以尝试以下两种操作之一:

在你的FragmentonCreate() 中花费磅,调用 setRetainInstance(true);

如果固定的Fragment的数目相对较小,则为 ,然后在你的onCreate() 中添加下面的代码:


mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(limit);/* limit is a fixed integer*/

如果我记得正确的话第二个选项更有希望。 但我希望你同时尝试,看看他们中的哪一个。

原作者:
0 0

因为 Activity 实现了 is 。TabListener,所以 Activity的onCreate() 再次被调用。 因此,将以下代码放在 onResume() 方法中:


//Initilization
 viewPager = (ViewPager) findViewById(R.id.pager);
 actionBar = getActionBar();
 mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

 viewPager.setAdapter(mAdapter);
 actionBar.setHomeButtonEnabled(false);
 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


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

/**
 * on swiping the viewpager make respective tab selected
 * */
 viewPager.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) {
 }
 });

原作者:
...