在Back Button上的Fragment(Android)中保存GridView状态 [英] Saving GridView state in a Fragment (Android) on Back Button

查看:52
本文介绍了在Back Button上的Fragment(Android)中保存GridView状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



片段代码



In my android application, I am switching between the two set of images-(Set1, Set2) displayed in a GridView-A inside a Fragment-A.I am doing this on a button press using a boolean variable. Clicking on any GridView icon leads to another Fragment-B. The problem is when I press back button on Fragment-B , the Fragment-A is always loaded with the images of Set1 by default. I want the same set of images (Set1 or Set2 ) to be loaded on FramentA that were displayed before going to FragmentB.

Fragment Code

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) {

        context = inflater.getContext();
        View 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) getActivity().findViewById(R.id.btn_favorite_duas);
        GridMenu =(GridView) view.findViewById(R.id.gridView1);
    float scalefactor = getResources().getDisplayMetrics().density * 150;
        @SuppressWarnings("deprecation")
        int number = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        int columns = (int) ((float) number / (float) scalefactor);
        GridMenu.setAdapter(ga);
        GridMenu.setNumColumns(columns);    
        return view;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

         if (savedInstanceState != null) {
                // Restore last state for checked position.
                mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
            }

        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适配器





GridView Adapter

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

    }

}



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



谢谢。


So, can anybody help me out regarding what should be done to load the previous set of images on coming back to gridview fragment instead of having the default images of Set1.

Thank you.

推荐答案

您可以使用SharedPreference来编写和读取要重新加载的值。



写:

You can use SharedPreference to write and read values that you want to reload.

write:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.image_set), which);
editor.commit();





阅读:



read:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String defaultValue = "set1";
String image_set = sharedPref.getString(getString(R.string.image_set), defaultValue);


这篇关于在Back Button上的Fragment(Android)中保存GridView状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆