我使用ViewPager
实施了 FragmentStatePageAdapter
,因为它是一个动态的ViewPager,当它自己创建时它正常工作。
每个片段都有一个TextView
和两个Button
s,每个按钮都会将Textview
Size
更改为Big和Small。
参数保存在SharedPreference实例上。因此,从技术上讲,当我滑动到下一个片段时,它会显示应用了新Pararmeters的TextView,确实它可以工作并显示新的大小,它正常工作。
当我尝试在片段0和片段1之间滑动以刷新新参数时,我有三个片段并且问题开始。 方法:
public Fragment getItem(int position)
由于某种原因,它获得了position
2,是的,它显示fragment
1没有更改,当我滑到Fragment
2时,它会显示新的更改我在Fragment
0 上做了。同样,当我回到Fragment
1方法getItem
时,获取position
0并开始应用新更改,但永远不会更改Fragment
1。
我尝试添加一个新的fragment
所以现在是四个fragments
(0,1,2,3),当我在3到2之间跳转时,它开始刷新片段1上的信息
我检测到方法getItem(int position)返回奇数片段。
为什么这样工作?
假设它会返回0-1-2-3而不是0-2-4,1-3-5。
我的部分代码
posicionlist = pref.getInt("sel_psalm", 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_psalm_fullscreen);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
loadFragment();
private void loadFragment(){
mPager.setCurrentItem(posicionlist);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragmento = new FragmentSalmosFull();
Bundle args = new Bundle();
args.putString(FragmentSalmosFull.TIT_FRAG, Tsalmos.get(position).getName());
args.putString(FragmentSalmosFull.ESP_FRAG, Tsalmos.get(position).getEspFile());
args.putString(FragmentSalmosFull.HEB_FRAG, Tsalmos.get(position).getHebFile());
args.putString(FragmentSalmosFull.FON_FRAG, Tsalmos.get(position).getFonFile());
args.putInt(FragmentSalmosFull.LANG_SELECTED_FRAG, selectedLang);
fragmento.setArguments(args);
return fragmento;
}
@Override
public int getCount() {
return tabaux;
}
}
public static class FragmentSalmosFull extends Fragment {
public static final String TIT_FRAG = "section_name";
public static final String ESP_FRAG = "section_esp";
public static final String HEB_FRAG = "section_heb";
public static final String FON_FRAG = "section_fon";
public static final String LANG_SELECTED_FRAG = "language_selected";
Context contextu;
private Typeface tf, to;
private int sizefont;
private int posicionlang;
private TextView tvcontenido;
private TextView tvesp;
private TextView tvheb;
private TextView tvfon;
Button mas;
Button menos;
SharedPreferences pref;
Editor editor;
int tamanofinal = 0;
Boolean clickesp = false;
Boolean clickheb = false;
Boolean clickfon = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentsalmosfull, container, false);
final Bundle args = getArguments();
contextu = getActivity();
tf = Typeface.createFromAsset(getActivity().getAssets(), "Arvil_Sans.ttf");
to = Typeface.createFromAsset(getActivity().getAssets(), "AvantGardEF-Book.ttf");
pref = contextu.getSharedPreferences("MyPref",
Context.MODE_PRIVATE);
editor = pref.edit();
sizefont = pref.getInt("font_size", 22);
posicionlang = pref.getInt("posLang", 0);
// Setear el texto
TextView title = (TextView) rootView.findViewById(R.id.titulo_salmo);
title.setText(args.getString(TIT_FRAG));
tvcontenido = (TextView) rootView.findViewById(R.id.contenido_salmo);
tvcontenido.setTypeface(to);
tvesp = (TextView) rootView.findViewById(R.id.contenido_esp);
tvesp.setTypeface(tf);
tvheb = (TextView) rootView.findViewById(R.id.contenido_heb);
tvheb.setTypeface(tf);
tvfon = (TextView) rootView.findViewById(R.id.contenido_fon);
tvfon.setTypeface(tf);
mas = (Button) rootView.findViewById(R.id.btn_aumenta);
mas.setTypeface(tf);
menos = (Button) rootView.findViewById(R.id.btn_reduce);
menos.setTypeface(tf);
tvesp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvesp.setBackgroundColor(Color.parseColor("#9400D3"));
tvesp.setTextColor(Color.parseColor("#FFFFFF"));
tvheb.setBackgroundColor(Color.TRANSPARENT);
tvheb.setTextColor(Color.BLACK);
tvfon.setBackgroundColor(Color.TRANSPARENT);
tvfon.setTextColor(Color.BLACK);
lee(args.getString(ESP_FRAG));
if(!clickesp){
clickesp = true;
editor.putInt("posLang", 0);
editor.commit();
}else{
clickesp = false;
}
}
});
tvheb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvheb.setBackgroundColor(Color.parseColor("#9400D3"));
tvheb.setTextColor(Color.parseColor("#FFFFFF"));
tvesp.setBackgroundColor(Color.TRANSPARENT);
tvesp.setTextColor(Color.BLACK);
tvfon.setBackgroundColor(Color.TRANSPARENT);
tvfon.setTextColor(Color.BLACK);
lee(args.getString(HEB_FRAG));
if(!clickheb){
clickheb = true;
editor.putInt("posLang",1);
editor.commit();
}else{
clickheb = false;
}
}
});
tvfon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvfon.setBackgroundColor(Color.parseColor("#9400D3"));
tvfon.setTextColor(Color.parseColor("#FFFFFF"));
tvesp.setBackgroundColor(Color.TRANSPARENT);
tvesp.setTextColor(Color.BLACK);
tvheb.setBackgroundColor(Color.TRANSPARENT);
tvheb.setTextColor(Color.BLACK);
lee(args.getString(FON_FRAG));
if(!clickfon){
clickfon = true;
editor.putInt("posLang",2);
editor.commit();
}else{
clickfon = false;
}
}
});
mas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sizefont < 40) {
sizefont++;
tvcontenido.setTextSize(sizefont);
tamanofinal = sizefont;
editor.putInt("font_size", sizefont);
editor.commit();
}
}
});
menos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sizefont > 18) {
sizefont--;
tvcontenido.setTextSize(sizefont);
tamanofinal = sizefont;
editor.putInt("font_size", sizefont);
editor.commit();
}
}
});
if(args.getInt(LANG_SELECTED_FRAG)!= posicionlang) {
loadSelection(posicionlang);
System.out.println("Hizo click en alguno de los Languages" + posicionlang + "Posicion Original" + args.getInt(LANG_SELECTED_FRAG));
}
else{
loadSelection(args.getInt(LANG_SELECTED_FRAG));
}
return rootView;
}
谢谢大家。我可以看到,我是Android上的新手。