【发布时间】:2017-07-20 17:44:48
【问题描述】:
我制作了一个包含许多不同 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>
我认为这应该是所需的所有信息。如果您还有什么需要看的,请告诉我。
谢谢。
【问题讨论】:
-
将卡片视图的高度更改为包装内容 => android:layout_height="wrap_parent"
标签: android android-recyclerview cardview