У меня есть два фрагмента, один содержит сводку многих элементов в представлении переработчика, а другой содержит сведения об этих элементах. В альбомном режиме на планшете: я поместил два фрагмента в одно и то же упражнение.
В фрагменте подробностей у меня есть кнопка «Далее», чтобы перейти к следующему элементу, но когда я добрался до последнего элемента и нажал кнопку, это переход к элементу перед последним, а при нажатии на этот элемент, он перемещается к последнему и и так далее, хотя эта проблема не происходит в портретном режиме
этот код первого действия
public class FavouritesActivity extends AppCompatActivity implements ItemClickListener{
int activity;
ViewPager vp;
public static final String VIEW_POSITION="view_position";
public static final String CONDITION="condition";
public static final String ACTIVITY="activity";
SharedPreferences settings;
String locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favouritesactivity_layout);
activity = getIntent().getIntExtra("activity",1);
settings = getSharedPreferences("MyPrefs", 0);
locale = settings.getString("locale", "en-US");
/*Locale.setDefault(Locale.forLanguageTag(locale));
Configuration config = new Configuration();
config.setLocale(Locale.forLanguageTag(locale));
//config.locale = locale;
getResources().updateConfiguration(config,getResources().getDisplayMetrics());*/
String languageToLoad = locale; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
vp=(ViewPager)findViewById(R.id.viewpager);
this.addPages(vp);
TabLayout tab=(TabLayout)findViewById(R.id.mTab_Id);
tab.setTabGravity(TabLayout.GRAVITY_FILL);
tab.setupWithViewPager(vp);
tab.addOnTabSelectedListener(listener(vp));
}
// ADD ALL PAGES
private void addPages(ViewPager vp) {
MyFragPagerAdapter adapter=new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new SunsetFavouritesFragment());
adapter.addPage(new SunriseFavouritesFragment());
// SET ADAPTER TO PAGER
vp.setAdapter(adapter);
}
private TabLayout.OnTabSelectedListener listener(final ViewPager pager){
return new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
@Override
public void onClick(View view, int position) {
double inches=getScreenInches();
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
&& inches >=7){
int tabId=vp.getCurrentItem();
FavouritesDetailsFragment fragment = FavouritesDetailsFragment.newInstance(6
,getIntent().getIntExtra(FavouritesActivity.CONDITION,tabId+1),
getIntent().getIntExtra(FavouritesActivity.VIEW_POSITION,position));
replaceFragment(fragment, "SecondFragment");
}else{
int tabId=vp.getCurrentItem();
Intent intent=new Intent(this,FavouriteDetailsActivity.class);
intent.putExtra(CONDITION,tabId+1);
intent.putExtra(VIEW_POSITION,position);
intent.putExtra(ACTIVITY,6);
startActivity(intent);
}
}
public void replaceFragment(Fragment fragment, String tag) {
try {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.azkaarfav_detail_fragment, fragment, tag).commit();
} catch (Exception e) {
Log.d("", e.toString());
}
}
этот код второго фрагмента
@Override
public void onClick(View view, int position) {
final AzkarForm azkarForm=azkarForms.get(position);
switch (view.getId()){
case R.id.icon:
if (azkarForm.isFavourite() ){
if(CONDITION == 1) {
putFav(false, "fav", azkarForms.get(position).getId());
}
else {
putFav(false, "fav_evening", azkarForms.get(position).getId());
}
azkarForms.remove(position);
adapter.notifyItemRemoved(position);
}
break;
case R.id.fav_sample_model:
Intent intent1=new Intent(getActivity(),FavouriteDetailsActivity.class);
intent1.putExtra("position", 2);
intent1.putExtra("activity", 6);
intent1.putExtra("condition", 1);
getActivity().startActivity(intent1);
break;
case R.id.next:
Boolean next_vibrate=settings.getBoolean("sett_5",false);
vibe = (Vibrator) getActivity().getSystemService(Activity.VIBRATOR_SERVICE);
if(next_vibrate==true)
vibe.vibrate(100);
rv.scrollToPosition(position+1);
break;
case R.id.press:
Boolean press_vibrate=settings.getBoolean("sett_4",false);
Boolean sound_count=settings.getBoolean("sett_6",false);
Boolean next_count=settings.getBoolean("sett_3",false);
vibe = (Vibrator) getActivity().getSystemService(Activity.VIBRATOR_SERVICE);
MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.ringpress);
int num;
item=list.get(position);
Button press = (Button) view;
if(press_vibrate==true)
vibe.vibrate(100);
if(sound_count == true)
mp.start();
int currentNum = Integer.parseInt(press.getText().toString());
num = item.getNumber();
if (currentNum < num) {
if(ACTIVITY == 3)
putPress(currentNum,"presses",position);
else
putPress(currentNum,"presses_evening",position);
currentNum++;
}
if(currentNum == num)
{
if(ACTIVITY == 3)
putPress(currentNum,"presses",position);
else
putPress(currentNum,"presses_evening",position);
if(position == azkarForms.size()-1){
currentNum = num;
press.setText(String.valueOf(currentNum));
}
if(next_count == true)
rv.scrollToPosition(position+1);
else {
press.setText(String.valueOf(currentNum));
}
}
press.setText(String.valueOf(currentNum));
break;
}
}