-1

My portrait layout has a RecycleView that displays only the first name and last name of contacts.

I want to make it that when I change the orientation to landscape, the RecycleView now shows the first name, last name, phone number and picture. This requires me to tell the adapter that the orientation has changed so that it initializes more new views (like the imageview which wasn't in portrait mode) than it would have done in portrait mode so how do I capture the orientation and tell the adapter how to initialize?

I've already created the landscape xml file, I just need the adapter to initalize to that one instead when in landscape view, so how do I capture this event?

AleiusN
  • 17
  • 1
  • 5

1 Answers1

0

Check the screen orientation (see here) and set up two ViewHolders.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    int orientationLand = context.getResources().getBoolean(R.bool.is_landscape)

    if (orientationLand) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_land , parent, false);
        return new ViewHolderLand(v);
    }
    else {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_port, parent, false);
        return new ViewHolderPort(v);
    } 
    return null;
}

    @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    if (holder instanceof ViewHolderPort) {
        ViewHolderPort holderPort = (ViewHolderPort) holder;
        // Initialize views
    } else if (holder instanceof ViewHolderLand) {
        ViewHolderLandholderLand = (ViewHolderLand) holder;
        // Initialize views
    }
}