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