更改屏幕方向时,RecyclerView消失

时间:2018-03-12 04:40:54

标签: android android-recyclerview android-orientation

如果返回的JSONarray中存在任何数据,我正在开发一个每10秒钟点击一次Web服务的应用程序来获取数据,然后在RecyclerView中填充它。一旦应用程序检索到数据,检索到的数据将从服务器中删除。因此,下次应用程序访问Web服务时,它可能会变为空数组,直到新数据进入服务器。

问题: 当手机旋转或屏幕方向改变时,RecyclerView会消失。这可能是因为,android通常会破坏应用程序的现有活动,并在方向更改时重新创建它们。但在我的情况下,已经检索到的数据将不会出现在服务器中,因此可以在重新创建活动后显示它。

我认为它必须通过实现onSaveInstanceState方法来保存状态,但不确定如何在我的代码中实现它。任何人都可以告诉我要做出哪些改变。

MainActivity.java

public class MainActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener{

List<DataModel> DataAdapterClassList;
RecyclerView recyclerView;
LinearLayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ArrayList<String> SubjectNames;
RequestQueue requestQueue ;
SwipeRefreshLayout mSwipeRefreshLayout;
public MainActivity mainActivity;
View ChildView ;
int RecyclerViewClickedItemPOS ;
private static final String TAG = MainActivity.class.getSimpleName();
        .....//More declarations
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mainActivity = this;
    mRequestingLocationUpdates = false;
    mLastUpdateTime = "";
    // Update values using data stored in the Bundle.
    updateValuesFromBundle(savedInstanceState);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mSettingsClient = LocationServices.getSettingsClient(this);
    setContentView(R.layout.activity_main);
    DataAdapterClassList = new ArrayList<>();
    SubjectNames = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    recyclerView.setHasFixedSize(true);
    recyclerViewlayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(recyclerViewlayoutManager);
    recyclerViewlayoutManager.setReverseLayout(true);
    recyclerViewlayoutManager.setStackFromEnd(true);
            ......//location callbacks....
    startTimer();
    JSON_WEB_CALL();

}


/**Location Updates fields based on data stored in the bundle.*/
private void updateValuesFromBundle(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        if (savedInstanceState.keySet().contains(KEY_REQUESTING_LOCATION_UPDATES)) {
            mRequestingLocationUpdates = savedInstanceState.getBoolean(
                    KEY_REQUESTING_LOCATION_UPDATES);
        }
        if (savedInstanceState.keySet().contains(KEY_LOCATION)) {
            mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
        }

        // Update the value of mLastUpdateTime from the Bundle and update the UI.
        if (savedInstanceState.keySet().contains(KEY_LAST_UPDATED_TIME_STRING)) {
            mLastUpdateTime = savedInstanceState.getString(KEY_LAST_UPDATED_TIME_STRING);
        }
        updateUI();
    }
}
    @Override
public void onResume() {
    super.onResume();
}

    .............//More Location Codes

@Override
public void onRefresh() {
    mSwipeRefreshLayout.setRefreshing(false);
    JSON_WEB_CALL();
}

public void JSON_WEB_CALL(){

    String HTTP_SERVER_URL= String.format("http://pastebin.com/mySampleApi/student/%1$s",mStudentRoll);
    JsonArrayRequest jsArrRequest = new JsonArrayRequest
            (Request.Method.POST, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    //mTxtDisplay.setText("Response: " + response.toString());

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    Log.i(TAG, "Hello");
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                }
            }){

    };
    requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsArrRequest);
}



public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {
        DataModel GetDataModel = new DataModel();
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);
            GetDataModel.setId(json.getString("CLASS"));
            GetDataModel.setPlateNo(json.getString("HOUSE"));
            GetDataModel.setPlateCode(json.getString("IMAGEURL"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
            DataAdapterClassList.add(GetDataModel);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    if (array.length() != 0) {
    recyclerViewadapter = new NewRecyclerViewAdapter(DataAdapterClassList, this);
    recyclerView.setAdapter(recyclerViewadapter);
    SHOW_ALERT(array);
    sendNotification(recyclerView, array);
    }
}

@Override
public void onPause() {
    super.onPause();  
    }

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

}

NewRecyclerViewAdapter.java

public class NewRecyclerViewAdapter extends RecyclerView.Adapter<NewRecyclerViewAdapter.ViewHolder> {

Context context;
List<DataModel> dataModels;
private static int currentPosition = 0;
public Animation animationUp;
public  Animation animationDown;
public int mExpandedPosition=-1;

public NewRecyclerViewAdapter(List<DataModel> getDataAdapter, Context context){
    super();
    this.dataModels = getDataAdapter;
    this.context = context;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_card_view, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    DataModel dataAdapter =  dataModels.get(position)

    viewHolder.SudentClass.setText(dataAdapter.getSudentClass());
    viewHolder.SudentHouse.setText(dataAdapter.getSudentHouse());
    Glide.with(context).load(dataAdapter.getImgUrl()).into(viewHolder.imageView);
    viewHolder.linearLayout.setVisibility(View.GONE);
    final boolean isExpanded = position==mExpandedPosition;
    viewHolder.linearLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
    viewHolder.itemView.setActivated(isExpanded);
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mExpandedPosition = isExpanded ? -1:position;
            //TransitionManager.beginDelayedTransition();
            notifyDataSetChanged();
        }
    });

}

@Override
public int getItemCount() {
    return dataModels.size();
}

dataAdapter.getSudentClass() ViewHolder extends RecyclerView.ViewHolder{

    public TextView StudentdataAdapter.getSudentClass();
    public TextView SudentHouse;
    public TextView StudentClass;
    public ImageView imageView;
    LinearLayout linearLayout;
    public ViewHolder(View itemView) {
        super(itemView);
        SudentHouse = (TextView) itemView.findViewById(R.id.vehicle_SudentHouse) ;
        StudentClass = (TextView) itemView.findViewById(R.id.vehicle_student_class) ;
        imageView = (ImageView) itemView.findViewById(R.id.imageView);
    }
}
}

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/no_conn_layout"
    android:orientation="horizontal"
    android:visibility="visible">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Connection!!!"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Try Again" />
</LinearLayout>

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.newcoder.student.MainActivity">

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

3 个答案:

答案 0 :(得分:0)

您必须以有效的方式处理配置更改

在项目中尝试此代码

   @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    mListState=recyclerView.getLayoutManager().onSaveInstanceState();
    mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, mListState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (mBundleRecyclerViewState != null) {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
                recyclerView.getLayoutManager().onRestoreInstanceState(mListState);

            }
        }, 50);

    }

    recyclerView.setLayoutManager(gridLayoutManager);
 }

我在这里使用了GridLayoutManager,您可以使用LinearLayout

这是完整工作项目的github链接

Image Gallery Using RecyclerView

答案 1 :(得分:0)

请浏览以下链接 -

https://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject

想法很简单,如果我们设置

 // retain this fragment
 setRetainInstance(true);

内部片段,然后在更改配置时不会销毁此片段。 在此片段中调用您的API 并使用getter和setter获取数据,这样您就不会丢失数据。

答案 2 :(得分:0)

当您旋转屏幕时,需要使用以下代码...在mainActivity

中保存状态
    @Override
protected void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState != null) {
        DataAdapterClassList = savedInstanceState.getIntegerArrayList("key");
    }

    // Pass the imagesState to the adapter as a parameter here
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putIntegerArrayList("key", DataAdapterClassList);
}