gpt4 book ai didi

java - 滑动时的Android Scrollable Tabs + Swipe状态

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:16 26 4
gpt4 key购买 nike

我正在尝试制作具有多个选项卡的应用程序。对于每个选项卡,我使用不同的 fragment 。当我翻阅标签时,其中的信息正在发生变化。

如何在滑动时保存每个标签的状态?

代码示例:

MainActivity.java:

package com.tabs.example;

import java.util.Locale;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
pagerTabStrip.setBackgroundColor(Color.BLACK);
pagerTabStrip.setTabIndicatorColor(Color.CYAN);
pagerTabStrip.setTextColor(Color.WHITE);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {

Fragment fragment;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = null;
break;
}
return fragment;

}

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

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}

Fragment1.java:

package com.tabs.example;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment1, container, false);

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String time = dateFormat.format(date);

TextView tv1 = (TextView) root.findViewById(R.id.tv1);
tv1.setText(time);

return root;

}
}

Fragment2.java:

package com.tabs.example;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment2, container, false);

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String time = dateFormat.format(date);

TextView tv2 = (TextView) root.findViewById(R.id.tv2);
tv2.setText(time);

return root;

}
}

Fragment3.java:

package com.tabs.example;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Fragment3 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment3, container, false);

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String time = dateFormat.format(date);

TextView tv3 = (TextView) root.findViewById(R.id.tv3);
tv1.setText(time);

return root;

}
}

最佳答案

您可以通过像这样设置 ViewPagerAdapter 来缓存它们:

mViewPager.setOffscreenPageLimit(3);

关于java - 滑动时的Android Scrollable Tabs + Swipe状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16091704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com