2016-12-25 79 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>);