错误CS0115'... OnBindViewHolder(Object,int)':找不到合适的方法来覆盖 [英] Error CS0115 '...OnBindViewHolder(Object, int)': no suitable method found to override

查看:212
本文介绍了错误CS0115'... OnBindViewHolder(Object,int)':找不到合适的方法来覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绑定此库:

https://github.com/mancj/MaterialSearchBar

通常,它可以工作,但是,当我尝试添加RecyclerView的支持时,我遇到了一个问题,我添加了以下库:

And generally, it works, however, I have an issue when I try to add the support of the RecyclerView, I added the following libraries:

我收到以下错误:

我试图遵循以下建议创建一些局部类:

I tried to follow this advice of creating some partial classes:

xamarin.android绑定了thorw没有实现继承的抽象成员'RecyclerView.Adapter.OnCreateViewHolder(ViewGroup,int)'

但是它没有用,我个人开始复制副本,我相信主要问题在这里:

But it didn't work and I started to get duplicates, personally, I believe the main issue is here:

严重性代码描述项目文件行抑制状态 错误CS0115'SuggestionsAdapter.OnBindViewHolder(Object,int)':未找到合适的方法来覆盖Xamarin-MaterialSearchBar C:\ Users \ feder \ source \ repos \ Xamarin-MaterialSearchBar \ Xamarin-MaterialSearchBar \ obj \ Release \ Generated \ src \ Com .Mancj.Materialsearchbar.Adapter.SuggestionsAdapter.cs 666有效

Severity Code Description Project File Line Suppression State Error CS0115 'SuggestionsAdapter.OnBindViewHolder(Object, int)': no suitable method found to override Xamarin-MaterialSearchBar C:\Users\feder\source\repos\Xamarin-MaterialSearchBar\Xamarin-MaterialSearchBar\obj\Release\generated\src\Com.Mancj.Materialsearchbar.Adapter.SuggestionsAdapter.cs 666 Active

这是我的VS 2019的配置:

This is the configuration of my VS 2019:

项目Gradle中唯一的依赖项如下:

The only dependencies in the Gradle of the project are the following ones:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}

如果您希望将已编译的 aar文件和项目进行测试.

If you want the compiled aar file and the project to test it.

如您所见,我拥有了所有这些.任何想法,我想念什么?谢谢.

Which as you can see I have them all. Any idea, what am I missing? Thanks.

推荐答案

如何解决此问题?从技术上讲,这并不是那么简单,这是最佳解决方案,需要执行6个步骤:

How to fix this issue? Technically, it's not so simple, the best solution and there are 6 steps to follow:

  1. 添加以下NuGet软件包:

  1. Add the following NuGet Packages:

这些是在 build.gradle 中找到的最低要求. a>.

These are the minimum requirements found in the build.gradle.

使用这段代码从 Metadata.xml 中的将来库中删除类SuggestionsAdapter(受

Remove the class SuggestionsAdapter from the future library from your Metadata.xml with this piece of code (inspired by the Leo Zhu - MSFT' answer).

<remove-node path="/api/package[@name='com.mancj.materialsearchbar.adapter']/class[@name='SuggestionsAdapter']" />

为什么?由于绑定程序未正确将代码的此部分移植到C#,因此,请参见参考资料.也许是因为 V 代表 RecyclerView.ViewHolder ,对于活页夹而言,它太通用了.您可以在此处看到原始代码: SuggestionsAdapter.java

Why? Because this section of the code is not properly ported to C# by the binder; perhaps, the reason is that the V represents the RecyclerView.ViewHolder and it's too generic for the binder. You can see the original code here: SuggestionsAdapter.java

此外,您可能会问为什么我选择在

Also, you might ask why I chose to migrate the SuggestionsAdapter over the DefaultSuggestionsAdapter. There are 2 reasons:

  • SuggestionsAdapter是基类.
  • DefaultSuggestionsAdapter调用您无法从C#访问的 XML代码,您可以在第34、55和56行中看到它们.
  • SuggestionsAdapter is the base class.
  • DefaultSuggestionsAdapter calls XML codes that you cannot access from C#, you can see them in the lines 34, 55 and 56.

构建您的库.

在添加项中创建一个名为 Adapter 的新文件夹,您需要在其中创建一个名为 SuggestionsAdapter 的类.

Create a new folder in your Additions called Adapter where you need to create a class called SuggestionsAdapter.

将代码从Java迁移到C#.

Migrate the code from Java to C#.

namespace Com.Mancj.Materialsearchbar.Adapter
{
    public abstract class SuggestionsAdapter<S, V> : RecyclerView.Adapter, IFilterable
    {
        private readonly LayoutInflater Inflater;
        protected List<S> Suggestions = new List<S>();
        protected List<S> Suggestions_clone = new List<S>();
        protected int MaxSuggestionsCount = 5;

        public void AddSuggestion(S r)
        {
            if (MaxSuggestionsCount <= 0)
            {
                return;
            }

            if (r == null)
            {
                return;
            }
            if (!Suggestions.Contains(r))
            {
                if (Suggestions.Count >= MaxSuggestionsCount)
                {
                    Suggestions.RemoveAt(MaxSuggestionsCount - 1);
                }
                Suggestions.Insert(0, r);
            }
            else
            {
                Suggestions.Remove(r);
                Suggestions.Insert(0, r);
            }
            Suggestions_clone = Suggestions;
            NotifyDataSetChanged();
        }

        public void SetSuggestions(List<S> suggestions)
        {
            Suggestions = suggestions;
            Suggestions_clone = suggestions;
            NotifyDataSetChanged();
        }

        public void ClearSuggestions()
        {
            Suggestions.Clear();
            Suggestions_clone = Suggestions;
            NotifyDataSetChanged();
        }

        public void DeleteSuggestion(int position, S r)
        {
            if (r == null)
            {
                return;
            }
            //delete item with animation
            if (Suggestions.Contains(r))
            {
                NotifyItemRemoved(position);
                Suggestions.Remove(r);
                Suggestions_clone = Suggestions;
            }
        }

        public List<S> GetSuggestions()
        {
            return Suggestions;
        }

        public int GetMaxSuggestionsCount()
        {
            return MaxSuggestionsCount;
        }

        public void SetMaxSuggestionsCount(int maxSuggestionsCount)
        {
            MaxSuggestionsCount = maxSuggestionsCount;
        }

        protected LayoutInflater GetLayoutInflater()
        {
            return Inflater;
        }

        public SuggestionsAdapter(LayoutInflater inflater)
        {
            Inflater = inflater;
        }

        public abstract int GetSingleViewHeight();

        public int GetListHeight()
        {
            return ItemCount * GetSingleViewHeight();
        }

        public abstract void OnBindSuggestionHolder(S suggestion, RecyclerView.ViewHolder holder, int position);

        public override int ItemCount => Suggestions.Count;

        public Filter Filter => null;

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            OnBindSuggestionHolder(Suggestions[position], holder, position);
        }

        public interface IOnItemViewClickListener
        {
            void OnItemClickListener(int position, View v);
            void OnItemDeleteListener(int position, View v);
        }
    }
}

  • 再次构建您的项目,仅此而已!您的图书馆正在正常工作.

  • Build your project again and that's all! Your library is fully working.

    如果要检查结果.

    这篇关于错误CS0115'... OnBindViewHolder(Object,int)':找不到合适的方法来覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆