刷卡时,Android的滚动标签+刷卡状态 [英] Android Scrollable Tabs + Swipe state when swiping
本文介绍了刷卡时,Android的滚动标签+刷卡状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图与多个选项卡的应用程序。对于每个选项卡,我用不同的片段。而当通过选项卡在其中的信息正在改变我做人。
我如何保存每个选项卡的状态,刷卡的时候?
code,例如:
MainActivity.java
包com.tabs.example;
进口java.util.Locale中;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.PagerTabStrip;
进口android.support.v4.view.ViewPager;
公共类MainActivity扩展FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mSectionsPagerAdapter =新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);
}
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
公共SectionsPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT位置){
片段片段
开关(位置){
情况下0:
片段=新片段1();
打破;
情况1:
片段=新Fragment2();
打破;
案例2:
片段=新Fragment3();
打破;
默认:
片段= NULL;
打破;
}
返回片段;
}
@覆盖
公众诠释getCount将(){
返回3;
}
@覆盖
公共CharSequence的getPageTitle(INT位置){
区域设置L = Locale.getDefault();
开关(位置){
情况下0:
返回的getString(R.string.title_section1).toUpperCase(升);
情况1:
返回的getString(R.string.title_section2).toUpperCase(升);
案例2:
返回的getString(R.string.title_section3).toUpperCase(升);
}
返回null;
}
}
}
Fragment1.java
包com.tabs.example;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口java.text.DateFormat中;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
公共类片段1扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看根= inflater.inflate(R.layout.fragment1,集装箱,假);
日期格式日期格式=新的SimpleDateFormat(HH:MM:SS);
日期日期=新的日期();
字符串时间= dateFormat.format(日期);
TextView的TV1 =(TextView中)root.findViewById(R.id.tv1);
tv1.setText(时间);
返回根;
}
}
Fragment2.java
包com.tabs.example;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口java.text.DateFormat中;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
公共类Fragment2扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看根= inflater.inflate(R.layout.fragment2,集装箱,假);
日期格式日期格式=新的SimpleDateFormat(HH:MM:SS);
日期日期=新的日期();
字符串时间= dateFormat.format(日期);
TextView的TV2 =(TextView中)root.findViewById(R.id.tv2);
tv2.setText(时间);
返回根;
}
}
Fragment3.java
包com.tabs.example;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口java.text.DateFormat中;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
公共类Fragment3扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看根= inflater.inflate(R.layout.fragment3,集装箱,假);
日期格式日期格式=新的SimpleDateFormat(HH:MM:SS);
日期日期=新的日期();
字符串时间= dateFormat.format(日期);
TextView的TV3 =(TextView中)root.findViewById(R.id.tv3);
tv1.setText(时间);
返回根;
}
}
解决方案
您可以通过设置ViewPagerAdapter这样的缓存它们:
mViewPager.setOffscreenPageLimit(3);
I'm trying to make an application with multiple Tabs. For each tab I'm using different fragments. And when I leaf through tabs the information in them is changing.
How can I save the state of each tab when swiping?
Code for example:
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;
}
}
解决方案
You can cache them all by setting the ViewPagerAdapter like this:
mViewPager.setOffscreenPageLimit(3);
这篇关于刷卡时,Android的滚动标签+刷卡状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文