在Back Button上的Fragment(Android)中保存GridView状态

时间:2014-04-18 08:23:25

标签: android-fragments android-gridview android-adapter savestate

在我的Android应用程序中,我在Fragment-A内的GridView-A中显示的两组图像(Set1,Set2)之间进行切换。我正在使用布尔变量按下按钮。单击任何GridView图标将导致另一个Fragment-B。问题是当我按下Fragment-B上的后退按钮时,Fragment-A默认会加载Set1的图像。我希望在进入FragmentB之前显示的FramentA上加载相同的图像集(Set1或Set2)。

EDITED: 片段代码

  public class GridViewFragment extends Fragment {

    Context context;
    GridView GridMenu;
    GridViewAdapter ga;
    Button Btn_Settings;
    Button Btn_lang_Ch;
    Button favoriteDuas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //fragment = new GridViewFragment();
        if (view == null) {
            context = inflater.getContext();
            view = inflater.inflate(R.layout.fragment_gridview, container, false);
            ga = new GridViewAdapter(context);
            Btn_lang_Ch = (Button) view.findViewById(R.id.lng_ch);
            Btn_Settings = (Button) view.findViewById(R.id.button_settings);
            favoriteDuas = (Button) view.findViewById(R.id.btn_favorite_duas);
            GridMenu = (GridView) view.findViewById(R.id.gridView1);
            GridMenu.setAdapter(ga);

        } else {
            // remove view from previously attached ViewGroup
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        Btn_lang_Ch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ga.changeImages(!ga.imageSetChange);
                ga.Lang_Status();
            }
        });

        GridMenu.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,  long id) {
                ((MainActivity) context).loadSingleDuaFragment();
            }
        });

        super.onActivityCreated(savedInstanceState);
    }

}

GridView适配器

public class GridViewAdapter extends BaseAdapter {

    public boolean imageSetChange = false;
    public static boolean curr_lang = false;//english

    public Integer[] mThumbIds = {
            R.drawable.eng_pic1, R.drawable.eng_pic2,
            R.drawable.eng_pic3, R.drawable.eng_pic4,
            R.drawable.eng_pic5, R.drawable.eng_pic6,
            R.drawable.eng_pic7, R.drawable.eng_pic8,
            R.drawable.eng_pic9, R.drawable.eng_pic10,
            R.drawable.eng_pic11, R.drawable.eng_pic12,
            R.drawable.eng_pic13, R.drawable.eng_pic14,
            R.drawable.eng_pic15, R.drawable.eng_pic16,
            R.drawable.eng_pic17, R.drawable.eng_pic18,
            R.drawable.eng_pic19, R.drawable.eng_pic20,
            R.drawable.eng_pic21
    };

    public Integer[] mThumbIds1 = {
            R.drawable.urdu_dua1, R.drawable.urdu_dua2,
            R.drawable.urdu_dua3, R.drawable.urdu_dua4,
            R.drawable.urdu_dua5, R.drawable.urdu_dua6,
            R.drawable.urdu_dua7, R.drawable.urdu_dua8,
            R.drawable.urdu_dua9, R.drawable.urdu_dua10,
            R.drawable.urdu_dua11, R.drawable.urdu_dua12,
            R.drawable.urdu_dua13, R.drawable.urdu_dua14,
            R.drawable.urdu_dua15, R.drawable.urdu_dua16,
            R.drawable.urdu_dua17, R.drawable.urdu_dua18,
            R.drawable.urdu_dua19, R.drawable.urdu_dua20,
            R.drawable.urdu_dua21

    };
    private Context mContext;

    public GridViewAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View MyView;
        if (convertView == null) {
            LayoutInflater li = ((Activity) mContext).getLayoutInflater();
            MyView = li.inflate(R.layout.menuitem, parent, false);

        } else {
            MyView = (View) convertView;
        }
        ImageView iv = (ImageView) MyView.findViewById(R.id.image);

        if (imageSetChange) {
            iv.setImageResource(mThumbIds1[position]);

        } else {
            iv.setImageResource(mThumbIds[position]);
        }

        return MyView;
    }

    public void changeImages(boolean change) {
        this.imageSetChange = change;
        notifyDataSetChanged();
        //   gf.Lang_Status();
    }

    public void Lang_Status() {
        // if curr_eng
        if (!curr_lang)
            this.curr_lang = true; // change to urdu
            // if curr_urdu
        else
            this.curr_lang = false;   // change to english

    }

}

编辑: 的 MainActivity

public void loadGridViewFragment() {
        if (activityActive) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if(getSupportFragmentManager().findFragmentByTag(GridViewFragment.TAG)!=null){
                ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
            }   
            else
            {
            frag = new GridViewFragment();
            ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
            }
            ft.commit();
        }
    }

那么,任何人都可以帮我解决在回到gridview片段时加载前一组图像应该做什么而不是使用Set1的默认图像。

0 个答案:

没有答案