其他分享
首页 > 其他分享> > android – 轮回后,Recyclerview行中的图像消失

android – 轮回后,Recyclerview行中的图像消失

作者:互联网

在我的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);
    }
}

编辑:我在相应的活动中初始化我的适配器:

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);
}

}

解决方法:

您可以在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);
    }
}

标签:android,android-recyclerview,simpleadapter
来源: https://codeday.me/bug/20190608/1198216.html