我在片段中实现了一个视图寻呼机。按“返回”按钮后,将终止应用程序而不是上一页进行更改。有人能告诉我一个页面的Back按钮会如何回复给我。
public class Warenkonto extends Fragment {
public Warenkonto() {
}
ViewPager viewPager;
WarenkontoPageAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.start_kapitel, container, false);
adapter = new WarenkontoPageAdapter(this.getActivity());
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
return view;
}
public void onBackPressed() {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(0);
} else {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
}
public class WarenkontoPageAdapter extends PagerAdapter {
Context context;
public WarenkontoPageAdapter(Context context) {
this.context = context;
}
TextView titel;
TextView titelInhalt;
TextView inhalt;
TextView seite_von_bis;
ImageView bild;
ImageView imagePfeilrechts;
CharSequence titelInhalttext;
CharSequence inhaltText;
public int seite;
int maxSeitenzahl = 20;
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.a_titel_inhalt;
seite = 1;
break;
case 1:
resId = R.layout.a_titel_inhalt;
seite = 2;
break;
case 2:
resId = R.layout.a_inhalt;
seite = 3;
break;
}
View view = inflater.inflate(resId, null);
if (seite == 1) {
titel = (TextView) view.findViewById(R.id.a_ti_titel);
titel.setText("Warenkonto");
titelInhalt = (TextView) view.findViewById(R.id.a_ti_inhalt);
titelInhalttext = Html.fromHtml("Page 1");
} else if (seite == 2) {
titel = (TextView) view.findViewById(R.id.a_ti_titel);
titel.setText("Page 2");
titelInhalt = (TextView) view.findViewById(R.id.a_ti_inhalt);
titelInhalttext = Html
.fromHtml("Page 2");
titelInhalt.setText(titelInhalttext);
} else if (seite == 3) {
inhalt = (TextView) view.findViewById(R.id.a_i_inhalt);
inhaltText = Html
.fromHtml("Page 3");
inhalt.setText(inhaltText);
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
答案 0 :(得分:0)
根据Android Design Guide Navigation section(强调添加)
在屏幕中更改视图
更改屏幕的视图选项不会更改Up的行为 或者返回:屏幕仍然在应用程序内的相同位置 层次结构,并且不会创建新的导航历史记录。
此类视图更改的示例如下:
- 使用标签和/或左右滑动切换视图
- 使用下拉列表切换视图(也称为折叠标签)
- 过滤列表
- 排序列表
- 更改显示特性(例如缩放)
选项卡(和ViewPagers)不应该有历史记录,因此不应将它们添加到后台堆栈。