4

In my recyclerview I set an image (in recyclerview row) visible or not visible according some condition.

However after I rotate the screen the image disappears, how do I keep the image visible on it`s position in the recyclerview after rotating the screen?

Thank you for your help and happy holidays ;)

I am using an implementation of the 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);
    }
}

EDIT: I initialze my adapter in the the corresponsent activity:

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

}
Simon
  • 1,531
  • 2
  • 9
  • 24
  • try adding android:configChanges="orientation|screenSize" to your Activity in question in your manifest. – Bradley Wilson Dec 25 '16 at 11:44
  • Hi Bradley, I did that already in the manifest, as I did gather some info how to solve but it doesn`t help. – Simon Dec 25 '16 at 11:45
  • where did you initialise your adapter? do not add it during the onCreateView. this will re-create the adapter everytime the view is created (e.g. during a rotation). show the codes where you new your adapter. – Angel Koh Dec 25 '16 at 11:46
  • 2
    Obviously you need to save it either while adding or at least in right activity's life cycle callback. – Selvin Dec 25 '16 at 11:47
  • Hi Angel, Selvin, see my edit how I intialize the adapter and save the instance. – Simon Dec 25 '16 at 11:52
  • My problem is the viewholder, I don`t know how to keep a reference to the row where the image is and set it to visisble. in onResume in the activity. – Simon Dec 25 '16 at 11:58

2 Answers2

4

You might be able to save the visibility state of the images in an ArrayList, then pass it as a parameter to the adapter. In your Activity, save that ArrayList using onSaveInstanceState.

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 */
}

Then in your Adapter:

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);
    }
}
Bink
  • 1,246
  • 1
  • 16
  • 29
Janice Kartika
  • 492
  • 1
  • 3
  • 14
  • This seems to be a good idea, I filled the arraylist like, imagesState.add(View.VISIBLE); etc but holder.icCancel.setVisibility(imagesState.get(position)); says must be one of View.VISIBLE etc? Could you elaborate a bit how this works? – Simon Dec 26 '16 at 06:04
  • Sorry @Simon I didn't check my answer in Android Studio. I have edited the answer. – Janice Kartika Dec 27 '16 at 03:49
1

use

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

to get the first position visible. then just scroll to the position

recyclerView.getLayoutManager().scrollToPosition( <position of interest> ); 
Angel Koh
  • 10,460
  • 5
  • 53
  • 75