2

我Recyclerview片段它做工精細,但是當我改變屏幕旋轉,prograss欄沒有顯示出任何更多,我知道片段distory所有的意見,有沒有什麼辦法可以挽救prograss查看OBJ的onSaveInstanceState中或其他任何方式解決售後服務這個問題?進度條不顯示在recyclerview片段Reandroid

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_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 


     android:id="@+id/myRecylerView" /> 

    <ProgressBar 
     style="?android:attr/progressBarStyle" 
     android:layout_marginTop="40dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/progressBar" /> 

    <FrameLayout 
     android:id="@+id/mFramLaout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageButton 
      android:layout_gravity="center" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:srcCompat="@drawable/ic_cloud_off_black_24dp" 
      android:id="@+id/mImageButton" 
      android:elevation="0dp" 
      android:visibility="gone" 
      android:background="#f1f1f1" /> 

    </FrameLayout> 
</LinearLayout> 
+0

試試這個:HTTP:// stackoverflow.com/questions/18354569/maintaining-progress-bar-visibility-with-orientation-change –

回答

0

您可以使用Bundle對象來此目的。在onDestroy()函數中添加該行

@Override 
public void onSaveInstanceState(Bundle outState) 
{ 

    super.onSaveInstanceState(outState); 

    outState.putInt("PROGRESS", progress); 

} 

片段時,將重新創建你可以加入這一行取回您的public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)功能使用此代碼此值:

if(savedInstanceState!=null) 
    { 
     progress = savedInstanceState.getInt("PROGRESS"); 
    } 
+0

它不是我想要什麼 – Hamza

+0

所以,你要達到什麼樣的? –