My RecyclerView item layout like the picture, the left is a RelativeLayout including an icon and a shadow view, the right is a ViewPager and I set it's layout_height="300dp"
but in the code, viewPager.getHeight() always equal 0,and the relativeLayout so. but the icon ImageView and the shadow view can display normally, I try to use a TextView replace ViewPager, it can work too, just the viewPager didn't work,why?
and, in the ViewHolder class, I log itemView.getId() in OnClick method, the result always equal -1
this is itemView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="150dp">
<RelativeLayout
android:id="@+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:background="@color/blueItemDark">
<View
android:id="@+id/height_view"
android:layout_width="0dp"
android:layout_height="150dp" />
<ImageView
android:id="@+id/weather_icon"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:padding="30dp"
android:tint="#fff"
tools:src="@mipmap/sun" />
<View
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@drawable/shadow_vertical" />
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_weight="2"
tools:background="@color/blueItem"/>
</LinearLayout>
this is itemView ViewHolder class:
public class WeatherItemView extends RecyclerView.ViewHolder implements View.OnClickListener {
private static final String TAG = "WeatherItemView";
private RelativeLayout left;
private RecyclerView.LayoutParams params;
private int expandHeight;
private int shrinkHeight;
public ImageView weatherIcon;
public ViewPager viewPager;
public WeatherItemView(final View itemView) {
super(itemView);
left = itemView.findViewById(R.id.left);
weatherIcon = itemView.findViewById(R.id.weather_icon);
viewPager = itemView.findViewById(R.id.view_pager);
//expandHeight = viewPager.getHeight();
//shrinkHeight = itemView.findViewById(R.id.height_view).getHeight();
expandHeight = DisplayUtil.dp2px(itemView.getContext(), 300);
shrinkHeight = DisplayUtil.dp2px(itemView.getContext(), 150);
Log.d(TAG, "WeatherItemView: leftHeight = " + left.getHeight() + ",rightHeight = " + viewPager.getHeight());
Log.d(TAG, "WeatherItemView: expandHeight = " + expandHeight + ",shrinkHeight = " + shrinkHeight);
params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
itemView.setOnClickListener(this);
}
public void setBackground(int[] color) {
left.setBackgroundColor(itemView.getResources().getColor(color[1]));
viewPager.setBackgroundColor(itemView.getResources().getColor(color[0]));
}
@Override
public void onClick(View view) {
int itemHeight = itemView.getHeight();
int changedHeight;
if (itemHeight == shrinkHeight) {
changedHeight = expandHeight;
} else {
changedHeight = shrinkHeight;
viewPager.setCurrentItem(0);
}
params.height = changedHeight;
Log.d(TAG, "onClick: itemView change height = " + params.height);
itemView.setLayoutParams(params);
}
}