Android保存动态添加的viewpager';从另一个活动返回并重新打开应用程序时的碎片
在StackOverflow()中回答这个问题的帮助下,我成功地创建了我的项目,并根据需要进行了一些修改 我可以在主布局中添加N个片段(viewpages)并删除它们,直到页面计数变为零 现在,我突然想到一个问题。如果我从当前的活动转移到其他活动,或者关闭我的应用程序并返回,我只能看到最初静态添加的片段 我想看到的不仅是最初的页面,而且以前添加的页面。我应该使用SharedReferences来存储动态添加的片段吗 以下是我的源代码: 我的主要布局:Android保存动态添加的viewpager';从另一个活动返回并重新打开应用程序时的碎片,android,android-fragments,Android,Android Fragments,在StackOverflow()中回答这个问题的帮助下,我成功地创建了我的项目,并根据需要进行了一些修改 我可以在主布局中添加N个片段(viewpages)并删除它们,直到页面计数变为零 现在,我突然想到一个问题。如果我从当前的活动转移到其他活动,或者关闭我的应用程序并返回,我只能看到最初静态添加的片段 我想看到的不仅是最初的页面,而且以前添加的页面。我应该使用SharedReferences来存储动态添加的片段吗 以下是我的源代码: 我的主要布局: <?xml version="1.0"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="900dp"
/>
<Button
android:id="@+id/show_items"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:textColor="#ffffff"
android:background="#49a942"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="+" />
<Button
android:id="@+id/grid_apps_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pager"
android:layout_alignParentBottom="true"
android:text="Apps"
/>
</RelativeLayout>
我的主要活动课:
public class MainActivity extends FragmentActivity implements TextProvider{
Button home;
private ViewPager mPager;
private MyPagerAdapter mAdapter;
private ArrayList<String> mEntries = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = (Button) findViewById(R.id.show_items);
mEntries.add("pos 1");
mPager = (ViewPager) findViewById(R.id.pager);
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setTitle("Add/Delete Screens");
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("+",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
addNewItem();
}
})
.setPositiveButton("-",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
removeCurrentItem();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this);
mPager.setAdapter(mAdapter);
}
private void addNewItem() {
mEntries.add("Pages");
mAdapter.notifyDataSetChanged();
}
private void removeCurrentItem() {
int position = mPager.getCurrentItem();
if(position != 0){
mEntries.remove(position);
mAdapter.notifyDataSetChanged();
}
else{
Toast.makeText(getApplicationContext(), "Minimum Screens are one!", Toast.LENGTH_LONG).show();
}
}
@Override
public String getTextForPosition(int position) {
return mEntries.get(position);
}
@Override
public int getCount() {
return mEntries.size();
}
private class MyPagerAdapter extends FragmentPagerAdapter {
private TextProvider mProvider;
private long baseId = 0;
public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
super(fm);
this.mProvider = provider;
}
@Override
public Fragment getItem(int position) {
if(position == 0){
return ScreenOne.newInstance(mProvider.getTextForPosition(position));
}
if(position == 1){
return ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
return ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
@Override
public int getCount() {
return mProvider.getCount();
}
//this is called when notifyDataSetChanged() is called
@Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
}
@Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* @param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
}
}
my fragment one:
public class ScreenOne extends Fragment {
private String mText;
public static ScreenOne newInstance(String text) {
ScreenOne f = new ScreenOne(text);
return f;
}
public ScreenOne() {
}
public ScreenOne(String text) {
this.mText = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.screen_one, container, false);
((TextView) root.findViewById(R.id.position_one)).setText(mText);
return root;
}
}
my fragment two:
public class ScreenTwo extends Fragment {
private String mText;
public static ScreenTwo newInstance(String text) {
ScreenTwo f = new ScreenTwo(text);
return f;
}
public ScreenTwo() {
}
public ScreenTwo(String text) {
this.mText = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.screen_two, container, false);
((TextView) root.findViewById(R.id.position_two)).setText(mText);
return root;
}
}
public类MainActivity扩展FragmentActivity实现TextProvider{
按钮主页;
私人寻呼机;
私人MyPagerAdapter mAdapter;
private ArrayList mEntries=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
主页=(按钮)findViewById(R.id.show_项);
添加(位置1);
mPager=(ViewPager)findViewById(R.id.pager);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
主要活动(本);
alertDialogBuilder.setTitle(“添加/删除屏幕”);
alertDialogBuilder
.setCancelable(错误)
.setNegativeButton(“+”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
addNewItem();
}
})
.setPositiveButton(“-”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
删除currentitem();
}
});
//创建警报对话框
AlertDialog AlertDialog=alertDialogBuilder.create();
//表现出来
alertDialog.show();
}
});
mAdapter=new MyPagerAdapter(this.getSupportFragmentManager(),this);
mPager.setAdapter(mAdapter);
}
私有void addNewItem(){
添加(“页面”);
mAdapter.notifyDataSetChanged();
}
私有void removeCurrentItem(){
int position=mPager.getCurrentItem();
如果(位置!=0){
删除(位置);
mAdapter.notifyDataSetChanged();
}
否则{
Toast.makeText(getApplicationContext(),“最小屏幕数为一!”,Toast.LENGTH\u LONG.show();
}
}
@凌驾
公共字符串getTextForPosition(int位置){
返回mEntries.get(位置);
}
@凌驾
public int getCount(){
返回mEntries.size();
}
私有类MyPagerAdapter扩展了FragmentPagerAdapter{
私人文本提供商mProvider;
私有长baseId=0;
公共MyPagerAdapter(FragmentManager fm、文本提供程序){
超级(fm);
this.mProvider=提供者;
}
@凌驾
公共片段getItem(int位置){
如果(位置==0){
返回ScreenOne.newInstance(mProvider.getTextForPosition(position));
}
如果(位置==1){
返回ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
返回ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
@凌驾
public int getCount(){
返回mProvider.getCount();
}
//在调用notifyDataSetChanged()时调用此函数
@凌驾
public int getItemPosition(对象){
//数据集更改时刷新所有片段
返回PagerAdapter.POSITION\u无;
}
@凌驾
公共长getItemId(int位置){
//当位置改变时,给出一个与位置不同的ID
返回baseId+位置;
}
/**
*通知片段的位置已更改。
*为每个位置创建一个新ID,以强制重新创建片段
*@param n已更改的项目数
*/
public void notifyChangeInPosition(int n){
//将getItemId返回的ID移到前面所有片段的范围之外
baseId+=getCount()+n;
}
}
}
我的片段一:
公共类ScreenOne扩展了片段{
私有字符串多行文字;
公共静态ScreenOne新实例(字符串文本){
ScreenOne f=新的ScreenOne(文本);
返回f;
}
公共屏幕一(){
}
公共屏幕一(字符串文本){
this.mText=文本;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根=充气机。充气(R.layout.screen_one,容器,错误);
((TextView)root.findviewbyd(R.id.position_one)).setText(mText);
返回根;
}
}
我的片段二:
公共类ScreenTwo扩展片段{
私有字符串多行文字;
公共静态屏幕两个新实例(字符串文本){
ScreenTwo f=新的ScreenTwo(文本);
返回f;
}
公共屏幕2(){
}
公共屏幕2(字符串文本){
this.mText=文本;
}
@结束
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(ViewGroup container, int position, Object object);
}
super.destroyItem(ViewGroup container, int position, Object object);