RecyclerView itemView的childView getHeight()始终为0

时间:2018-06-16 00:21:25

标签: android android-recyclerview

我的RecyclerView项目布局如图片,左边是RelativeLayout,包括图标和阴影视图,右边是ViewPager,我设置它的 layout_height =“300dp”

但在代码中, viewPager.getHeight()始终等于0 ,而relativeLayout则如此。但是图标ImageView和阴影视图可以正常显示,我尝试使用TextView替换ViewPager,它也可以工作,只是viewPager不起作用,为什么?

并且,在ViewHolder类中,我在OnClick方法中记录itemView.getId(),结果总是等于-1

RecyclerView itemView layout

这是itemView布局:

    <?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>

这是itemView ViewHolder类:

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

0 个答案:

没有答案