Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 屏幕定位后的固定卡视图_Java_Android_Android Fragments_Onsaveinstancestate - Fatal编程技术网

Java 屏幕定位后的固定卡视图

Java 屏幕定位后的固定卡视图,java,android,android-fragments,onsaveinstancestate,Java,Android,Android Fragments,Onsaveinstancestate,我有一个片段,它进行异步调用,分配给一个对象变量(我花了一段时间才弄明白),然后单击按钮,它通过适配器将cardwiews加载到屏幕中 我使用的代码是: @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) { super.onCreateView(inflater, c

我有一个片段,它进行异步调用,分配给一个对象变量(我花了一段时间才弄明白),然后单击按钮,它通过适配器将
cardwiews
加载到屏幕中

我使用的代码是:

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragments_stops_list, container, false);
        ButterKnife.bind(this, view); // since this is a fragment the butterknife bind method should goafter the view declaration
        // saved state would indicate that we are now not capable of making multiple requests. This point has been
        // properly taken care of and now I can purposely move on to other things of more importance
        loadButton = (Button) view.findViewById(R.id.LOAD);
        if(savedInstanceState == null) {
            loadButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d(">==========<", extractCreate.getCreateR().getPhone());
                    Parcelable[] parcelable = extractCreate.getStopsR();
                    commodities = Arrays.copyOf(parcelable, parcelable.length, Stops[].class);
                    //Log.d("Commodities size check", String.valueOf(commodities.length));


                    StopsAdapter adapter = new StopsAdapter(commodities);
                    mRecyclerView.setAdapter(adapter);
                    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); // sketchy on this part
                    mRecyclerView.setLayoutManager(layoutManager);
                    mRecyclerView.setHasFixedSize(true);
                    Log.d(TAG, "This is being called from the onCreate method");


                }
            });
        }



        return view;
    }

由于当方向发生变化时,您的片段将被销毁并再次创建,因此您必须实现一些逻辑来保存在方向发生变化事件之前收集的数据

这是通过覆盖savedinstancestate(Bundle)方法实现的。您需要保存的所有内容都包含在此处。您将数据放入Bundle对象,并为其分配一个键,以便能够在最近检索数据

当操作系统再次创建片段时,它将再次通过onCreateView()方法。在这里,您必须检查savedInstanceState对象是否为空。如果不是,则它很可能包含在方向更改事件发生之前保存的一些数据

看看下面的要点

public class SomeClass extends Activity {

  private static final String KEY = "some_key";

  commodities;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragments_stops_list, container, false);
        ButterKnife.bind(this, view); // since this is a fragment the butterknife bind method should goafter the view declaration
        // saved state would indicate that we are now not capable of making multiple requests. This point has been
        // properly taken care of and now I can purposely move on to other things of more importance
        loadButton = (Button) view.findViewById(R.id.LOAD);
        if(savedInstanceState == null) {
            loadButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d(">==========<", extractCreate.getCreateR().getPhone());
                    Parcelable[] parcelable = extractCreate.getStopsR();
                    commodities = Arrays.copyOf(parcelable, parcelable.length, Stops[].class);
                    //Log.d("Commodities size check", String.valueOf(commodities.length));


                    StopsAdapter adapter = new StopsAdapter(commodities);
                    mRecyclerView.setAdapter(adapter);
                    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); // sketchy on this part
                    mRecyclerView.setLayoutManager(layoutManager);
                    mRecyclerView.setHasFixedSize(true);
                    Log.d(TAG, "This is being called from the onCreate method");


                }
            });
        } else {
          if(savedInstanceState.containsKey(KEY)) {
            commodities = savedInstanceState.getParcelable(KEY);
            // here you will pretty much repete the code from above
            // for creating an Adapter for the RecyclerView
                    StopsAdapter adapter = new StopsAdapter(commodities);
                    mRecyclerView.setAdapter(adapter);
                    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); // sketchy on this part
                    mRecyclerView.setLayoutManager(layoutManager);
                    mRecyclerView.setHasFixedSize(true);
          }
        }
        return view;
    }

      @Override
      public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putParcelable(KEY, commodities);
      }

}
public类SomeClass扩展活动{
私有静态最终字符串KEY=“some_KEY”;
商品;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、最终捆绑包保存状态){
super.onCreateView(充气机、容器、保存状态);
视图=充气机。充气(R.layout.fragments\u stops\u list,container,false);
bind(this,view);//由于这是一个片段,ButterKnife绑定方法应该在视图声明之后
//保存状态将表明我们现在无法发出多个请求。这一点已被忽略
//得到适当的照顾,现在我可以有目的地继续做其他更重要的事情
loadButton=(Button)view.findViewById(R.id.LOAD);
如果(savedInstanceState==null){
loadButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){

Log.d(“>=============将其放入清单的活动标记中

 android:configChanges="orientation|keyboardHidden|screenSize"

希望这会有所帮助。

关于这个问题,这里有多个问题。例如,请检查这个@TodorKostov hi Todor,并感谢您提供的指针,不幸的是,那篇文章中描述的选项以前就已经应用过了(将setRetainInstance以及saveInstance状态上的if语句设置为true,如我的代码中所述),该方法仍然无法工作。但是您是否尝试重写onSavedInstanceState()方法并保存所需的信息?我在源代码中看不到它。我无法将onClick中发生的代码保存到过度依赖的onSaveInstanceState()中方法,花了4个小时,并决定寻找一个替代方案。你会怎么做?任何基于我在click listener中所拥有的代码样本都会令人惊讶,但请提供一些类似商品类型的源代码。谢谢你的回答!我以前尝试过类似的解决方案。我将更新上面的代码,向你展示我的缺点商品实例的帐篷。它实际上是一个名为Stops(可包裹)的类,包含一个名为商品(也可包裹)的实际类的数组那么,问题的当前状态是什么?你设法解决了吗?仍然在努力解决。正如我所说,我尝试使用你以前提供的解决方案,但由于无法使其发挥作用,我开始尝试做一些不同的事情。@sigillum_conf我很有兴趣看到你关于这个问题的方法!事实上,我找到了解决问题的方法它!我只需要在行内进行一次简单的转换:
commodities=(Stops[]savedInstanceState.getParcelableArray(KEY))
我确实考虑过这一点,但这似乎是一个糟糕的解决方案,可能会成为更大范围的问题。+1因为它让我哈哈大笑!谢谢
 android:configChanges="orientation|keyboardHidden|screenSize"