Android RecyclerView中的CardView在进入视图时调整自身大小
我制作了一个RecyclerView,其中包含许多不同的CardView。当RecyclerView第一次加载时,屏幕上启动的、在recycler视图中可见的卡的大小是正确的,但当它们从屏幕上滚下时,下一次回到屏幕上时,它们的大小已经调整,大小不正确 行动中的it视频: LocationsListFragment:Android RecyclerView中的CardView在进入视图时调整自身大小,android,android-recyclerview,cardview,Android,Android Recyclerview,Cardview,我制作了一个RecyclerView,其中包含许多不同的CardView。当RecyclerView第一次加载时,屏幕上启动的、在recycler视图中可见的卡的大小是正确的,但当它们从屏幕上滚下时,下一次回到屏幕上时,它们的大小已经调整,大小不正确 行动中的it视频: LocationsListFragment: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
@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
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"));
}
}
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"));
}
}
<?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>
inearLayoutManager布局管理器=新的LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager)
位置查看文件夹:
@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
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"));
}
}
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"));
}
}
<?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>
public class LocationsViewHolder扩展了RecyclerView.ViewHolder{
私有图像查看位置图像;
私有文本视图位置名称;
私有文本视图位置地址;
公共位置查看文件夹(查看项目视图){
超级(项目视图);
locationName=(TextView)itemView.findViewById(R.id.location\u name);
locationAddress=(TextView)itemView.findViewById(R.id.location\u地址);
locationImage=(ImageView)itemView.findViewById(R.id.location\u image);
}
公共void updateUI(HashMap位置){
locationName.setText(place.get(“name”));
}
}
位置适配器:
@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
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"));
}
}
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"));
}
}
<?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>
public class LocationsViewHolder扩展了RecyclerView.ViewHolder{
私有图像查看位置图像;
私有文本视图位置名称;
私有文本视图位置地址;
公共位置查看文件夹(查看项目视图){
超级(项目视图);
locationName=(TextView)itemView.findViewById(R.id.location\u name);
locationAddress=(TextView)itemView.findViewById(R.id.location\u地址);
locationImage=(ImageView)itemView.findViewById(R.id.location\u image);
}
公共void updateUI(HashMap位置){
locationName.setText(place.get(“name”));
}
}
card\u location.xml:
@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
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"));
}
}
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"));
}
}
<?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>
我认为这应该是所有需要的信息。如果你还有什么需要看的,请告诉我
谢谢。将cardview的高度更改为包裹内容
android:layout_height="wrap_parent"
将cardview的高度更改为wrap content=>android:layout\u height=“wrap\u parent”