我在RecyclerView中遇到Cardview问题。卡包含ImageView和Textbox。开始时,所有卡都在屏幕上,但是在屏幕上向下滚动并向上滚动之后,它们之间都是空白。
cardview布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="245dp"
android:layout_gravity="center_horizontal"
android:id="@+id/eventCardView"
cardview:cardCornerRadius="5dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="240dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/eventImage"
android:scaleType="centerCrop" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
适配器
using System.Collections.Generic;
using Android.Content;
using Android.Content.Res;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using EVENTS_MOBILE.Activities;
using EVENTS_MOBILE.CORE.Models;
using EVENTS_MOBILE.Utilities;
namespace EVENTS_MOBILE.Adapters
{
public class RecyclerAdapter : RecyclerView.Adapter
{
private readonly List<SimplestEventModel> _events;
private readonly RecyclerView _recyclerView;
private readonly Context _context;
public RecyclerAdapter(List<SimplestEventModel> events, RecyclerView recyclerView, Context context)
{
_context = context;
_events = events;
_recyclerView = recyclerView;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (holder is MyView myHolder)
{
//myHolder.Name.Text = _events[position].Name;
//myHolder.Image.SetImageBitmap(ImageHelper.GetImageBitmapFromUrl(_events[position].ImageLink));
myHolder.Image.SetImageResource((int)typeof(Resource.Drawable).GetField("test_image").GetValue(null));
if (!myHolder.Image.HasOnClickListeners)
{
myHolder.Image.Click += delegate
{
var intent = new Intent(_context, typeof(SingleEventActivity));
intent.PutExtra("eventId", position);
_context.StartActivity(intent);
};
}
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.card_layout, parent, false);
//TextView txtName = row.FindViewById<TextView>(Resource.Id.eventTitle);
ImageView image = row.FindViewById<ImageView>(Resource.Id.eventImage);
MyView view = new MyView(row)
{
//Name = txtName,
Image = image
};
return view;
}
public override int ItemCount => _events.Count;
public class MyView : RecyclerView.ViewHolder
{
public View MainView { get; set; }
//public TextView Name { get; set; }
public ImageView Image { get; set; }
public MyView(View view) : base(view)
{
MainView = view;
}
}
}
}
EventsFragment
using System;
using Android.OS;
using Android.Views;
using EVENTS_MOBILE.Adapters;
using EVENTS_MOBILE.CORE.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.Support.V7.Widget;
using Android.Widget;
using EVENTS_MOBILE.CORE.Services;
namespace EVENTS_MOBILE.Fragments
{
public class EventsFragment : Fragment
{
protected EventsService EventsService;
protected List<SimplestEventModel> Events = new List<SimplestEventModel>();
protected RecyclerView _recyclerView;
protected RecyclerView.LayoutManager LayoutManager;
protected RecyclerView.Adapter Adapter;
private ProgressBar _progressBar;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
private void LoadAllEvents(string category)
{
EventsService = new EventsService(category);
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
FindViews();
//Events = EventsService.GetAllEventsForCategory();
Adapter = new RecyclerAdapter(MockSimpleEvents(), _recyclerView, this.Context);
_recyclerView.SetAdapter(Adapter);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.eventsFragment, container, false);
}
protected void FindViews()
{
_recyclerView = this.View.FindViewById<RecyclerView>(Resource.Id.recyclerView);
_progressBar = this.View.FindViewById<ProgressBar>(Resource.Id.progressbar_view);
LayoutManager = new LinearLayoutManager(this.Context);
_recyclerView.SetLayoutManager(LayoutManager);
}
}
EventsActivity
using System;
using Android.App;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using EVENTS_MOBILE.Fragments;
namespace EVENTS_MOBILE.Activities
{
[Activity(Label="EVENTS",Theme = "@style/Theme.AppCompat.Light.DarkActionBar")]
public class EventsActivity : Activity
{
public string Email = String.Empty;
public string Category = String.Empty;
BottomNavigationView _bottomNavigation;
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
base.OnCreate(savedInstanceState);
Email = Intent.GetStringExtra("email");
Category = Intent.GetStringExtra("category");
SetContentView(Resource.Layout.menu);
_bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
_bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
LoadFragment(Resource.Id.events);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
private void LoadFragment(int id)
{
FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
switch (id)
{
case Resource.Id.events:
EventsFragment eventsFragment = new EventsFragment();
fragmentTx.Replace(Resource.Id.fragmentContainer, eventsFragment);
fragmentTx.AddToBackStack(null);
fragmentTx.Commit();
break;
case Resource.Id.map:
var nearestEventsFragment = new NearestEventsFragment();
fragmentTx.Replace(Resource.Id.fragmentContainer, nearestEventsFragment);
fragmentTx.AddToBackStack(null);
fragmentTx.Commit();
break;
case Resource.Id.observed:
WatchListFragment watchlistfragment = new WatchListFragment(Email);
fragmentTx.Replace(Resource.Id.fragmentContainer, watchlistfragment);
Bundle args = new Bundle();
args.PutString("Email", Email);
watchlistfragment.Arguments = args;
fragmentTx.AddToBackStack(null);
fragmentTx.Commit();
break;
}
}
}
}
menu.axml(片段容器)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="start"
android:layout_alignParentBottom="true"
app:elevation="16dp"
android:background="@drawable/gradient"
app:itemIconTint="@drawable/nav_item_color"
app:itemTextColor="@drawable/nav_item_color"
app:menu="@menu/bottom_navigation_bar" />
</RelativeLayout>
此视图的eventsFragment.axml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView" />
</LinearLayout>
开始时一切正常: https://i.stack.imgur.com/9j7Pp.png
但是在滚动后看起来像这样:https://i.stack.imgur.com/gG6q5.png
有人知道这种奇怪的行为吗?
答案 0 :(得分:0)
卡片视图xml中的根LinearLayout
的宽度和高度都使用fill_parent
:
android:layout_width="fill_parent" android:layout_height="fill_parent"
将高度更改为wrap_content
,这应该可以解决问题。