1

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

我正在使用的代码是:

 @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;
    }

问题是,如果我尝试旋转屏幕,然后内容消失并再次出现,必须单击按钮(的id.LOAD),我添加了一条语句来检查 saveInstance 状态是否为空,该解决方案不起作用现在,当我转动屏幕时cardviews,我通过适配器加载的内容消失了,并且不可能再次加载它们。

我可以保存状态吗?我读过关于覆盖该onViewStateRestored方法的信息,但这个想法似乎并不乐观,因为内容需要在点击侦听器内部处理。

任何建议将不胜感激。

根据要求,这是 .

public class Stops implements Parcelable{
    private String stop_id;
    private String type;
    private String location;
    private String address;
    private String city;
    private String state;
    private String zip;
    private String from;
    private String to;

    private String getitem;

    private Commodities[] commodities;



    public Stops() {}
    public String getStop_id() {
        return stop_id;
    }

    public void setStop_id(String stop_id) {
        this.stop_id = stop_id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public Commodities[] getCommodities() {
        return commodities;
    }



    public void setCommodities(Commodities[] commodities) {
        this.commodities = commodities;
    }
    /**
     * The content for the actual parcelables start in here
     *
     * */

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.stop_id);
        dest.writeString(this.type);
        dest.writeString(this.location);
        dest.writeString(this.address);
        dest.writeString(this.city);
        dest.writeString(this.state);
        dest.writeString(this.zip);
        dest.writeString(this.from);
        dest.writeString(this.to);
        dest.writeString(this.getitem);
        dest.writeTypedArray(this.commodities, flags);
    }

    protected Stops(Parcel in) {
        this.stop_id = in.readString();
        this.type = in.readString();
        this.location = in.readString();
        this.address = in.readString();
        this.city = in.readString();
        this.state = in.readString();
        this.zip = in.readString();
        this.from = in.readString();
        this.to = in.readString();
        this.getitem = in.readString();
        this.commodities = in.createTypedArray(Commodities.CREATOR);
    }

    public static final Parcelable.Creator<Stops> CREATOR = new Parcelable.Creator<Stops>() {
        @Override
        public Stops createFromParcel(Parcel source) {
            return new Stops(source);
        }

        @Override
        public Stops[] newArray(int size) {
            return new Stops[size];
        }
    };
}
4

2 回答 2

3

由于您的片段在方向更改时被销毁并再次创建,因此您必须实现一些逻辑才能保存在方向更改事件之前收集的数据。

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

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

查看以下要点。

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);
      }

}

为了正确保存您的数据,您的商品类需要实现 Parcelable。检查此链接以获取更多信息。可包裹

PS 由于我不知道您的商品对象的实际类型,您将不得不稍微修复代码。只是一个简单的复制-> 粘贴是行不通的!

于 2016-11-24T08:15:28.627 回答
0

把它放在你的清单的活动标签中

 android:configChanges="orientation|keyboardHidden|screenSize"

希望这会有所帮助。

于 2016-11-24T10:05:50.440 回答