简体   繁体   English

滚动时显示空白的Cardview

[英]Empty cardview when scrolling

I have problem with Cardview inside RecyclerView. 我在RecyclerView中遇到Cardview问题。 Card contains ImageView and Textbox. 卡包含ImageView和Textbox。 At the beggining all cards are on the screen, but after scrolling down and scrolling up on the screen between are empty spaces. 开始时,所有卡都在屏幕上,但是在屏幕上向下滚动并向上滚动后,之间是空白。

cardview layout 卡片视图布局

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

Adapter 适配器

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 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 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 (container for fragments) 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 layout for this view 此视图的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>

At the beginning everything is Okay: https://i.stack.imgur.com/9j7Pp.png 一开始一切都很好: https : //i.stack.imgur.com/9j7Pp.png

But after scrolling it looks like that: https://i.stack.imgur.com/gG6q5.png 但是滚动后看起来像这样: https : //i.stack.imgur.com/gG6q5.png

Does anybody know something about this strange behavior? 有人知道这种奇怪的行为吗?

The root LinearLayout in your card view xml uses fill_parent for both width and height: 卡视图xml中的根LinearLayout使用fill_parent作为宽度和高度:

 android:layout_width="fill_parent" android:layout_height="fill_parent" 

Change the height to wrap_content and this should solve the problem. 将高度更改为wrap_content ,这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM