0

I'm using RecyclerView and in its Adapter each list item has image and some text inside a CardView.

Question : I want to change the weight of each ImageView programmatically.

This is the code that runs on screen rotation:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        SquareImageView imgView = (SquareImageView) findViewById(R.id.thumbnail);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imgView.getLayoutParams();
        lp.weight = 0.1f;
    }
}

The code runs but just on one card and not on all cards.

Edit: My Adapter code if needed:

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Context context, RecyclerView recyclerView, LinkedList<CardData> myDataset)
{
    mContext = context;
    mRecyclerView = recyclerView;
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType)
{
    // create a new view
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card_view, parent, false);
    final ViewHolder viewHolder = new ViewHolder(view);

    view.setOnTouchListener(new View.OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(parent.getContext(), new GestureDetector.SimpleOnGestureListener() {
            Not Relevant
        });

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            return gestureDetector.onTouchEvent(event);
        }});

    return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
    CardData currentCard = mDataset.get(position);

    holder.mHeadline.setText(currentCard.mHeadline);

}
Rani
  • 6,424
  • 1
  • 23
  • 31

1 Answers1

0

This is not the actual way of changing the layout items in the list items in your RecyclerView in onConfigurationChanged

You need to modify your adapter code and check if the orientation is tablet or not.

So inside your adapter you need to set the layout weight with something like this in your onBindViewHolder

if(tablet){
    lp.weight = 0.1f;
} else {
    lp.weight = 1; // Whatever
}

Now in onConfigurationChanged you just need to reset your adapter.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Reset your adapter here.
    myAdapter.resetAdapter();
}
Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Where exactly in the Adapter? OnBindViewHolder or OnCreateViewHolder? Thanks! – Rani Aug 03 '16 at 14:22
  • I read it, but couldn't find anything related to the adapter. moreover, there is no such function resetAdapter. – Rani Aug 03 '16 at 14:28
  • `resetAdapter` is a function you need to write in your adapter. This function will simply reset the views calling `notifyDatasetChanged`. But anyway, try without declaring a `resetAdapter`. The `Adapter` should reset the views automatically. Tell me if it doesn't update the views. Post your adapter code and I'll look into your adapter too. – Reaz Murshed Aug 03 '16 at 14:32
  • I tried to add this to my adapter: `LinearView.LayoutParams lp = (LinearView.LayoutParams) holder.mLayout.getLayoutParams();` but it doesn't work. Do you know why? and why is it better to check if tablet and not the orientation? – Rani Aug 03 '16 at 14:59