2016-12-25 83 views
3

在我的recyclerview中,我根據某些條件將圖像(在recyclerview行中)設置爲可見或不可見。循環播放器行中的圖像在循環後消失

但是經過我旋轉圖像消失在屏幕上,我該如何保持圖像的可見上它`位置在recyclerview 旋轉屏幕後?

謝謝您的幫助和節日快樂;)

我使用SimpleAdapter的實現。

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> implements FollowRedirectsCallback { 

public static class SimpleViewHolder extends RecyclerView.ViewHolder { 
     ... 
     public final View icCancel; 

     public SimpleViewHolder(View view) { 
      super(view); 
      ... 
      icCancel = view.findViewById(R.id.icCancel); 
     } 
    } 

    public SimpleAdapter(Context context, String[] data, long userId, int dlTypeValue) { 
     mContext = context; 
     userID = userId; 
     DLTypeValue = dlTypeValue; 
     if (data != null) 
      mData = new ArrayList<String>(Arrays.asList(data)); 
     else mData = new ArrayList<String>(); 
    } 

    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     final View view = LayoutInflater.from(mContext).inflate(R.layout.simple_item, parent, false); 
     return new SimpleViewHolder(view); 
    } 

@Override 
    public void onBindViewHolder(final SimpleViewHolder holder, final int position) { 
    ... 
    // How to keep this image visible after rotation of the screen? 
    if (somecondition) { 
     holder.icCancel.setVisibility(View.VISIBLE); 
    } 
} 

編輯:我initialze我的適配器中的corresponsent活動:

public class DownloadsActivity extends BaseActivity { 

    RecyclerView recyclerView; 
    Parcelable state; 
    SimpleAdapter mAdapter; 
    SimpleSectionedRecyclerViewAdapter mSectionedAdapter; 
    SimpleSectionedRecyclerViewAdapter.Section[] dummy; 

@AfterViews 
    public void init() { 
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     //setLayout Manager 
     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(linearLayoutManager); 
     recyclerView.setHasFixedSize(true); 

     dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; 
     mSectionedAdapter = new 
       SimpleSectionedRecyclerViewAdapter(this, R.layout.section, R.id.section_text, mAdapter); 
     mSectionedAdapter.setSections(sections.toArray(dummy)); 


     //Apply this adapter to the RecyclerView 
     recyclerView.setAdapter(mSectionedAdapter); 
    } 

@Override 
protected void onPause() { 
    super.onPause(); 
    // save RecyclerView state 
    state = recyclerView.getLayoutManager().onSaveInstanceState(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (inProgress) { 
     progresstxt.setVisibility(View.VISIBLE); 
     downloadprogress.setVisibility(View.VISIBLE); 
    } 

    // restore RecyclerView state 
    recyclerView.getLayoutManager().onRestoreInstanceState(state); 
} 

} 
+0

嘗試添加機器人:configChanges = 「方向|屏幕尺寸」,以在您的清單問題的活動。 –

+0

嗨布拉德利,我已經在清單中做過,因爲我收集了一些信息如何解決,但沒有幫助。 – Simon

+0

您是在哪裏初始化適配器的?不要在onCreateView中添加它。這將在每次創建視圖時(例如在旋轉過程中)重新創建適配器。顯示新的適配器的代碼。 –

回答

3

你也許可以保存在ArrayList圖像的可見性狀態,然後將它作爲參數傳遞到適配器。在您的活動中,使用onSaveInstanceState保存ArrayList

public class YourActivity extends BaseActivity { 
    /* some codes */ 

    // possible values are: VISIBLE, INVISIBLE, or GONE 
    private ArrayList<Integer> imagesState = new ArrayList<>(); 

    /* some codes */ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     if(savedInstanceState != null) { 
      imagesState = 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", imagesState); 
    } 

    /* some codes */ 
} 

然後在您的適配器:

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> implements FollowRedirectsCallback { 

    /* some codes */ 

    public SimpleAdapter(Context context, String[] data, long userId, int dlTypeValue, ArrayList<Integer> imagesState) { 
     /* some codes */ 
     this.imagesState = imagesState; 
    } 

    @Override 
    public void onBindViewHolder(SimpleViewHolder holder, int position) { 
     /* some codes */ 

     if (imagesState.get(position) == View.VISIBLE) 
      holder.icCancel.setVisibility(View.VISIBLE); 
     else if(imagesState.get(position) == View.INVISIBLE) 
      holder.icCancel.setVisibility(View.INVISIBLE); 
     else 
      holder.icCancel.setVisibility(View.GONE); 
    } 
} 
+0

這似乎是一個好主意,我填充像arraylist,imagesState.add(View.VISIBLE); etc但holder.icCancel.setVisibility(imagesState.get(position));說必須是View.VISIBLE等之一? 你能否詳細說明這是如何工作的? – Simon

+0

對不起@Simon我沒有在Android Studio中檢查我的答案。我編輯了答案。 –

+0

偉大的珍妮絲,很多thx。 – Simon

0

使用

int position = recyclerView.getLayoutManager().findFirstVisibleItemPosition(); 

獲得可見的第一個位置。然後只需滾動到位置

recyclerView.getLayoutManager().scrollToPosition(<position of interest>);