旋转后recyclerview行中的图像消失
Posted
技术标签:
【中文标题】旋转后recyclerview行中的图像消失【英文标题】:Image in row of recyclerview disappears after rotation 【发布时间】:2017-05-10 07:17:21 【问题描述】:在我的 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);
编辑:我在对应的活动中初始化我的适配器:
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);
【问题讨论】:
尝试将 android:configChanges="orientation|screenSize" 添加到清单中的相关 Activity。 嗨布拉德利,我已经在清单中这样做了,因为我确实收集了一些如何解决的信息,但它没有帮助。 你在哪里初始化你的适配器?不要在 onCreateView 期间添加它。这将在每次创建视图时重新创建适配器(例如在旋转期间)。显示新适配器的代码。 显然您需要在添加或至少在正确活动的生命周期回调中保存它。 嗨,Angel,Selvin,看看我的编辑我如何初始化适配器并保存实例。 【参考方案1】:您可以将图像的可见性状态保存在ArrayList
中,然后将其作为参数传递给适配器。在您的 Activity 中,使用 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);
【讨论】:
这似乎是个好主意,我填充了数组列表,比如 imagesState.add(View.VISIBLE);等等但是 holder.icCancel.setVisibility(imagesState.get(position));说必须是 View.VISIBLE 等之一?你能详细说明一下这是如何工作的吗? 对不起@Simon 我没有在 Android Studio 中检查我的答案。我已经编辑了答案。【参考方案2】:使用
int position = recyclerView.getLayoutManager().findFirstVisibleItemPosition();
让第一个位置可见。然后只需滚动到该位置
recyclerView.getLayoutManager().scrollToPosition( <position of interest> );
【讨论】:
以上是关于旋转后recyclerview行中的图像消失的主要内容,如果未能解决你的问题,请参考以下文章
Firebase recyclerview 图像在重新加载应用程序后消失
Android:在 Recyclerview 中更改图像旋转