在recyclerview片段Reandroid中屏幕旋转后进度条不显示
Posted
技术标签:
【中文标题】在recyclerview片段Reandroid中屏幕旋转后进度条不显示【英文标题】:progress bar not showing after screen rotation in recyclerview fragment Reandroid 【发布时间】:2017-02-04 00:07:14 【问题描述】:我有 Recyclerview Fragment 它工作正常,但是当我更改屏幕旋转时,进度条不再显示,我知道所有视图的片段混乱,有什么方法可以将进度视图 obj 保存在 onsaveinstancestate 或其他任何方式来解决这个问题问题?
package com.example.ameerhamza6733.expressdaily.UI;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import com.example.ameerhamza6733.expressdaily.MainActivity;
import com.example.ameerhamza6733.expressdaily.R;
import com.example.ameerhamza6733.expressdaily.Utils.Constant;
import com.example.ameerhamza6733.expressdaily.Utils.RssFeed;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* Demonstrates the use of @link RecyclerView with a @link LinearLayoutManager and a
* @link GridLayoutManager.
*/
public class RecyclerViewFragment extends Fragment implements MainActivity.UpdateUI
private static final String TAG = "RecyclerViewFragment";
private static final String KEY_LAYOUT_MANAGER = "layoutManager";
private static final int SPAN_COUNT = 2;
protected int indexofp = 0, indexOfPdash = 0;
protected LayoutManagerType mCurrentLayoutManagerType;
protected RecyclerView mRecyclerView;
protected CustomAdapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
protected ArrayList<RssFeed> mDataset = new ArrayList<>();
protected RssFeed rssFeed;
protected Context context;
protected String url = Constant.HOME_URL;
protected ProgressBar progressBar;
protected ImageButton mImageButton;
protected boolean dataSetClrear=false;
protected View view;
@Override
public void onArticleNavigationSeleted(String url)
this.url = url;
mDataset.clear();
dataSetClrear=true;
Log.i(TAG, "Current URL" + url);
private enum LayoutManagerType
GRID_LAYOUT_MANAGER,
LINEAR_LAYOUT_MANAGER
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// Initialize dataset, this data would usually come from a local content provider or
// remote server.
context = getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
rootView.setTag(TAG);
this.view = rootView;
// BEGIN_INCLUDE(initializeRecyclerView)
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.myRecylerView);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
mImageButton = (ImageButton) rootView.findViewById(R.id.mImageButton);
// LinearLayoutManager is used here, this will layout the elements in a similar fashion
// to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
// elements are laid out.
// mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(Color.RED).sizeResId(R.dimen.divider).marginResId(R.dimen.leftmargin, R.dimen.rightmargin).build());
mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).build());
mLayoutManager = new LinearLayoutManager(getActivity());
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
else
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
if (savedInstanceState != null )
// Restore saved layout manager type.
// mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
// .getSerializable(KEY_LAYOUT_MANAGER);
progressBar.setVisibility(View.VISIBLE);
mDataset = savedInstanceState.getParcelableArrayList(Constant.MY_DATA_SET_PARCE_ABLE_ARRAY_KEY);
mAdapter = new CustomAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
else
if (mDataset.isEmpty())
progressBar.setVisibility(View.VISIBLE);
initDataset();
setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
return rootView;
/**
* Set RecyclerView's LayoutManager to the one given.
*
* @param layoutManagerType Type of layout manager to switch to.
*/
public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType)
int scrollPosition = 0;
// If a layout manager has already been set, get current scroll position.
if (mRecyclerView.getLayoutManager() != null)
scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
switch (layoutManagerType)
case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
break;
case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
break;
default:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.scrollToPosition(scrollPosition);
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
// Save currently selected layout manager.
// Save currently selected layout manager.
// savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
// savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, (Serializable) progressBar);
savedInstanceState.putParcelableArrayList(Constant.MY_DATA_SET_PARCE_ABLE_ARRAY_KEY, mDataset);
super.onSaveInstanceState(savedInstanceState);
/**
* Generates Strings for RecyclerView's adapter. This data would usually come
* from a local content provider or remote server.
*/
private void initDataset()
if (mDataset.isEmpty())
progressBar.setVisibility(View.VISIBLE);
new LoadRssFeedsItems().execute("");
Log.i(TAG, "" + mDataset.size());
Log.i(TAG, "initDataset");
public class LoadRssFeedsItems extends AsyncTask<String, Void, Void>
private String mTitle, mDescription, mLink, mPubDate;
private String mCategory, mImageLn;
private String date, mContent;
private ProgressBar bar;
public void setProgressBar(ProgressBar bar)
this.bar = bar;
@Override
protected void onPreExecute()
super.onPreExecute();
if (progressBar != null)
progressBar.setVisibility(View.VISIBLE);
@Override
protected void onProgressUpdate(Void... values)
progressBar.setProgress(1);
super.onProgressUpdate(values);
@Override
protected Void doInBackground(String... params)
Document rssDocument = null;
try
rssDocument = Jsoup.connect(url).timeout(6000).ignoreContentType(true).parser(Parser.xmlParser()).get();
Elements mItems = rssDocument.select("item");
RssFeed rssItem;
for (Element element : mItems)
mTitle = element.select("title").first().text();
mDescription = element.select("description").first().text();
mLink = element.select("link").first().text();
mPubDate = element.select("pubDate").first().text();
mCategory = element.select("category").first().text();
mImageLn = element.select("media|content").attr("url").toString();
date = new SimpleDateFormat("dd-MMM-yyyy").format(new Date());
mContent = element.select("content|encoded").first().text();
mContent = Jsoup.parse(mContent).text();
indexofp = mDescription.indexOf(Constant.P);
indexOfPdash = mDescription.indexOf(Constant.P_DASH);
mDescription = mDescription.substring(indexofp + 3, indexOfPdash);
Log.i(TAG, "Item title: " + (mContent == null ? "N/A" : mContent));
Log.i(TAG, "Item title: " + (mTitle == null ? "N/A" : mTitle));
Log.i(TAG, "Item Description: " + (mDescription == null ? "N/A" : mDescription));
Log.i(TAG, "Item link: " + (mLink == null ? "N/A" : mLink));
Log.i(TAG, "Item data: " + (mImageLn == null ? "N/A" : mImageLn));
Log.i(TAG, "Item data: " + (mPubDate == null ? "N/A" : mPubDate));
Log.i(TAG, "system date: " + (date == null ? "N/A" : date));
rssFeed = new RssFeed(mTitle, mLink, mPubDate, mCategory, mLink, mDescription, mImageLn, context, mContent);
mDataset.add(rssFeed);
catch (IOException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
if (mDataset.isEmpty()) try
// mImageButton.setVisibility(View.VISIBLE);
Snackbar mSnackbar = Snackbar.make(view, "انٹرنیٹ دستیاب نہیں ہے", Snackbar.LENGTH_INDEFINITE)
.setAction("دوبارہ کوشش کریں", new View.OnClickListener()
@Override
public void onClick(View view)
// Snackbar.make(getView(), "CheckIn Cancelled", Snackbar.LENGTH_LONG).show();
new LoadRssFeedsItems().execute("");
);
mSnackbar.show();
catch (NullPointerException n)
n.printStackTrace();
else
progressBar.setVisibility(View.INVISIBLE);
mAdapter = new CustomAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
这是我的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_
android:layout_>
<android.support.v7.widget.RecyclerView
android:layout_
android:layout_
android:id="@+id/myRecylerView" />
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_marginTop="40dp"
android:layout_
android:layout_
android:id="@+id/progressBar" />
<FrameLayout
android:id="@+id/mFramLaout"
android:layout_
android:layout_>
<ImageButton
android:layout_gravity="center"
android:layout_
android:layout_
app:srcCompat="@drawable/ic_cloud_off_black_24dp"
android:id="@+id/mImageButton"
android:elevation="0dp"
android:visibility="gone"
android:background="#f1f1f1" />
</FrameLayout>
</LinearLayout>
【问题讨论】:
试试这个:***.com/questions/18354569/… 【参考方案1】:您可以使用Bundle
对象来实现此目的。在onDestroy()
函数中添加该行
@Override
public void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
outState.putInt("PROGRESS", progress);
当 Fragment 将被重新创建时,您可以通过添加以下行在 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
函数中使用此代码取回该值:
if(savedInstanceState!=null)
progress = savedInstanceState.getInt("PROGRESS");
【讨论】:
这不是我想要的 那你想达到什么目的?【参考方案2】:在 onSaveInstanceState 中保存变量对我不起作用,因为在 Activity 被销毁时您不会获得更新。
我最终在 Fragment 中使用了一个 ResultReceiver,它不会被 setRetainInstance(true) 破坏。
关于这个问题的好文章可以在这里找到:https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
也可以在这里查看我的答案:https://***.com/a/54334864/6747171
【讨论】:
以上是关于在recyclerview片段Reandroid中屏幕旋转后进度条不显示的主要内容,如果未能解决你的问题,请参考以下文章
Android:以编程方式在片段中添加多个 RecyclerView
我应该使用 ScrollView 还是 RecyclerView 在片段中滚动?
为啥 recyclerview$adapter 在片段中为空
带有 RecyclerView 的 SwipeRefreshLayout 在片段中不起作用