错误 CS0115“...OnBindViewHolder(Object, int)”:找不到合适的方法来覆盖

Error CS0115 '...OnBindViewHolder(Object, int)': no suitable method found to override

我正在绑定这个库:

https://github.com/mancj/MaterialSearchBar

通常情况下,它是有效的,但是,当我尝试添加对 RecyclerView 的支持时遇到问题,我添加了以下库:

我收到以下错误:

我试着按照这个建议创建一些部分 类:

xamarin.android binding thorw 'does not implement inherited abstract member 'RecyclerView.Adapter.OnCreateViewHolder(ViewGroup, int)'

但它没有用,我开始得到重复的,就个人而言,我认为主要问题在这里:

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的配置:

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

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 file and the project进行测试

如你所见,我都有。任何想法,我错过了什么?谢谢。

试试这个,

1.add Xamarin-MaterialSearchBar - Transforms - Metadata.xml

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

2.in 你的 Xamarin-MaterialSearchBar - Additions,创建一个 partial class DefaultSuggestionsAdapter

namespace Com.Mancj.Materialsearchbar.Adapter
{
  partial class DefaultSuggestionsAdapter
   {
     public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
      {
        throw new NotImplementedException();
      }

     public override void OnBindSuggestionHolder(Object p0, Object p1, int p2)
      {
        throw new NotImplementedException();
      }
   }
}

您还可以参考:

如何解决这个问题?从技术上讲,它不是那么简单,最好的解决方案有 6 个步骤:

  1. 添加以下 NuGet 包:

    这些是在 build.gradle.

  2. 中找到的最低要求
  3. 使用这段代码从 Metadata.xml 中删除未来库中的 class SuggestionsAdapter(启发由 ).

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

    为什么?因为这段代码没有通过binder正确移植到C#;也许,原因是 V 代表 RecyclerView.ViewHolder 并且它对于活页夹来说太通用了。您可以在此处查看原始代码:SuggestionsAdapter.java

    此外,您可能会问我为什么选择迁移 SuggestionsAdapter 而不是 DefaultSuggestionsAdapter。有两个原因:

    • SuggestionsAdapter 是 base class.
    • DefaultSuggestionsAdapter 调用了您无法从 C# 访问的 XML 代码,您可以在第 34、55 和 56 行中看到它们。
  4. 建立你的图书馆。

  5. 在您的 Additions 中创建一个名为 Adapter 的新文件夹,您需要在其中创建一个名为 SuggestionsAdapter[=69] 的 class =].

  6. 将代码从 Java 迁移到 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);
            }
        }
    }
    
  7. 再次构建您的项目,仅此而已!您的图书馆正在全面运作。

如果要检查result