我开发了一个带有3个标签的Android应用程序。完成从服务器获取数据后,我在第一个选项卡中设置文本 Tab1数据集成功,并在视图中显示新文本。然后我切换到tab3并切换回tab1。现在文本是 Tab 1 ,它在tab1.xml中定义。那么当我在标签之间切换时,如何保留以前的观看内容。
我的安卓代码在
之下public class secondPage extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
Intent intent = getIntent();
String dateStrig = intent.getStringExtra("Date");
String rData = null;
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("TAB 1"));
tabLayout.addTab(tabLayout.newTab().setText("TAB 2"));
tabLayout.addTab(tabLayout.newTab().setText("TAB 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
new fetchData().execute(dateStrig);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public class fetchData extends AsyncTask<String, String, String> {
Logger logger = Logger.getLogger(fetchData.class.getName());
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(secondPage.this);
progress.setMessage("loading...");
progress.setCancelable(false);
progress.show();
}
@Override
protected String doInBackground(String... params) {
String webPage = "", data = "";
{
try {
URL url = new URL("http://192.168.1.101:3000/rate?date=" + params[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((data = reader.readLine()) != null) {
webPage += data + "\n";
}
logger.info(webPage);
} catch (Exception e) {
e.printStackTrace();
}
}
return webPage;
}
protected void onPostExecute(String result) {
TextView t1=(TextView) findViewById(R.id.textView1);
t1.setText("Tab 1 selected");
progress.dismiss();
}
}
}
PageAdapter.java
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Tab1.java
public class Tab1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
}
Tab2.java
public class Tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false);
}
}
Tab3.java
public class Tab3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab3, container, false);
}
}
main_screen.xml
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
tab1.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 1"
android:textAppearance="?android:attr/textAppearanceLarge"/>
tab2.xml和tab3.xml看起来也与不同的ID和文本相同。
答案 0 :(得分:3)
这是因为ViewPager的屏幕外行为。默认值是1尝试根据您的要求最大化此限制它对我有效。
_pager.setOffscreenPageLimit(10);
答案 1 :(得分:0)
您可以使用任何缓存技术或使用 FragmentPagerAdapter 代替 FragmentStatePagerAdapter 来实现。 我不喜欢 FragmentPagerAdapter 解决方案,因为它会在内存中维护片段,如果从服务器获取大量数据,会增加内存开销。