RecyclerView中的CardView在进入视图时会自行调整大小

时间:2017-07-20 17:44:49

标签: android android-recyclerview cardview

我制作了一个包含许多不同CardView的RecyclerView。当RecyclerView第一次加载时,在回收站视图中可见的屏幕上显示的卡尺寸正确,但当它们在屏幕外滚动时,下次再次返回屏幕时,它们会被调整大小并且是不正确的大小。

动态视频:https://gfycat.com/gifs/detail/SoupyPowerlessFulmar

LocationsListFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_locations_list, container, false);
    adapter = new LocationsAdapter(mParam1);

    RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.recycler_locations);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);

    L

inearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);         recyclerView.setLayoutManager(的layoutManager);

    return v;
}

LocationsViewHolder:

public class LocationsViewHolder extends RecyclerView.ViewHolder{

private ImageView locationImage;
private TextView locationName;
private TextView locationAddress;

public LocationsViewHolder(View itemView) {
    super(itemView);

    locationName = (TextView)itemView.findViewById(R.id.location_name);
    locationAddress = (TextView)itemView.findViewById(R.id.location_address);
    locationImage = (ImageView) itemView.findViewById(R.id.location_image);
}

public void updateUI(HashMap<String, String > place){
    locationName.setText(place.get("name"));
}

}

LocationsAdapter:

public class LocationsViewHolder extends RecyclerView.ViewHolder{

private ImageView locationImage;
private TextView locationName;
private TextView locationAddress;

public LocationsViewHolder(View itemView) {
    super(itemView);

    locationName = (TextView)itemView.findViewById(R.id.location_name);
    locationAddress = (TextView)itemView.findViewById(R.id.location_address);
    locationImage = (ImageView) itemView.findViewById(R.id.location_image);
}
public void updateUI(HashMap<String, String > place){
    locationName.setText(place.get("name"));
}

}

card_location.xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/card">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <LinearLayout
            android:layout_width="274dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_margin="15dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textColor="#2f2f2f"
                android:textSize="18dp"
                android:id="@+id/location_name"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Address Goes Here"
                android:textSize="16dp"
                android:paddingTop="5dp"
                android:id="@+id/location_address"/>

        </LinearLayout>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="80dp"
            android:src="@drawable/ic_restaurant_black_24dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:id="@+id/location_image"
            android:layout_margin="10dp"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

我认为应该是所有需要的信息。如果您还需要查看其他内容,请告诉我们。

感谢。

1 个答案:

答案 0 :(得分:0)

将cardview的高度更改为换行内容

android:layout_height="wrap_parent"