大家好,我的代码有问题,我想要做的是当你改变微调器位置改变要显示的片段时,这就是我写的,但如果有人可以,它就不起作用请给我一个更好的方法,或者一个解决方案,这将是伟大的!
这是我的班级:
public class MainActivity extends ActionBarActivity {
private static final int NUM_PAGES = 2;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
String[] ciudades = { "Desde Paraná", "Desde Santa Fe", "Desde el pozo", "Desde abogacia"};
public int ciudad = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
Spinner sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.opciones, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
ciudad = position;
Toast.makeText(parentView.getContext(), ciudades[position], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parentView) {
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
/* Here is what I do, I took the value of ciudad and use it to display different group of fragment */
if(ciudad == 0){
switch(position){
case 0:
return new Home_fragment();
case 1:
return new Next_fragment();
}
}else if(ciudad == 1){
switch(position){
case 0:
return new Home_fragment_staFe();
case 1:
return new Next_fragment_staFe();
}
}
return null;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
提前致谢!
修改
我已经尝试过kcoppock告诉我使用
的内容notifyDataSetChanged()
我这样使用,但它仍然无法正常工作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
Spinner sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.opciones, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
ciudad = position;
Toast.makeText(parentView.getContext(), ciudades[position], Toast.LENGTH_SHORT).show();
mPagerAdapter.notifyDataSetChanged();
}
public void onNothingSelected(AdapterView<?> parentView) {
}
});
// Instantiate a ViewPager and a PagerAdapter.
mPager.setAdapter(mPagerAdapter);
}
答案 0 :(得分:0)
它永远不会再次调用getItem(),因为它不知道ciudad
的值已经改变。在您的微调器值更改方法中,当您更改ciudad
的值时,请调用mPagerAdapter.notifyDataSetChanged()
以通知适配器它应该重新创建碎片。
此外,Home_fragment
是Java非常规的命名模式。它应该是HomeFragment
。