从纵向转向景观耦合数据

时间:2018-03-22 12:28:57

标签: android android-recyclerview fragment landscape

我有一个活动,其中包含一个包含recyclelerview的片段,当我以纵向方式查看应用程序时,它工作正常但是当我转向横向时,数据是耦合的,这意味着我在刷卡时看到了另一个回收视图。 / p>

活动代码

public class DailyAzkaar extends AppCompatActivity {
    Toolbar toolbar;
    SharedPreferences settings;
    String locale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_azkaar);
        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());
        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        DailyAzkaarFragment dailyAzkaarFragment=new DailyAzkaarFragment();
        fragmentTransaction.add(R.id.fragment_container,dailyAzkaarFragment);
        fragmentTransaction.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this,"onResume",Toast.LENGTH_SHORT).show();
    }
}

这是片段

的代码
public class DailyAzkaarFragment extends Fragment implements ItemClickListener {
    ArrayList<AzkarForm> list;
    RecyclerView rv;
    int activity;
    SharedPreferences settings;
    Boolean[] favourite=new Boolean[28];
    Boolean[] favourite_evening=new Boolean[27];
    RecyclerView.LayoutManager layoutManager;
    RecyclerFavouriteAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.daily_azkaar_fragment,container, false);
        rv = (RecyclerView) view.findViewById(R.id.recyclerviewcard);
        activity = getActivity().getIntent().getIntExtra("activity", 3);
        AzkaarData azkaarData=new AzkaarData(getActivity());
        settings = getActivity().getSharedPreferences("MyPrefs", 0);
        if (settings.getBoolean("is_first_time", true)) {
            //the app is being launched for first time, do something
            Log.d("TAG", "First time");
            settings.edit().putBoolean("is_first_time", false).commit();
            storeArray(favourite,"fav",false,getActivity());
            storeArray(favourite_evening,"fav_evening",false,getActivity());
        } else {
            //second time launch..
            favourite=loadArray("fav",getActivity());
            favourite_evening=loadArray("fav_evening",getActivity());
            for(int i=0;i<favourite.length;i++)
                Log.d("msg ",String.valueOf(favourite[i]));
        }
        if(activity == 1)
            list=azkaarData.getArrayListData(favourite,activity);
        else
            list=azkaarData.getArrayListData(favourite_evening,activity);
        layoutManager=new LinearLayoutManager(getActivity());
        rv.setLayoutManager(layoutManager);
        rv.setHasFixedSize(true);
        adapter=new RecyclerFavouriteAdapter(list,activity,getActivity());
        rv.setAdapter(adapter);
        adapter.setClickListener(this);
        return view;
    }
    public boolean storeArray(Boolean[] array, String arrayName,boolean b, Context mContext) {
        settings = mContext.getSharedPreferences("MyPrefs", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(arrayName +"_size", array.length);
        for(int i=0;i<array.length;i++) {
            editor.putBoolean(arrayName + "_" + i, b);
            array[i]=b;
        }
        return editor.commit();
    }
    public Boolean[] loadArray(String arrayName, Context mContext) {
        settings = mContext.getSharedPreferences("MyPrefs", 0);
        int size = settings.getInt(arrayName + "_size", 0);
        Boolean array[] = new Boolean[size];
        for(int i=0;i<size;i++)
            array[i] = settings.getBoolean(arrayName + "_" + i, true);
        return array;
    }
    @Override
    public void onClick(View view, int position) {
        if(view.getId()==R.id.cardview) {
            //AzkarForm azkarForm = list.get(p);
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtra("position", position);
            if(activity == 1) intent.putExtra("activity", 3);
            else intent.putExtra("activity", 4);
            this.startActivity(intent);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当Android的屏幕方向发生变化时,通常会销毁并重新创建活动。娱乐可能会导致一些异常现象。要解决此问题,您需要保存活动(或片段或任何内容)的状态,并在重新创建活动时将其还原。你应该做以下

public void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's instance
        mContent = getSupportFragmentManager().getFragment(savedInstanceState, "myFragmentName");
        ...
    }
    ...
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //Save the fragment's instance
    getSupportFragmentManager().putFragment(outState, "myFragmentName", mContent);
}

mContent是一个片段,是对活动中当前片段实例的引用。我希望这会有所帮助。