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

时间:2019-06-23 15:48:45

标签: android android-recyclerview xamarin.android recyclerview-layout xamarin-binding

我正在绑定此库:

https://github.com/mancj/MaterialSearchBar

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

我收到以下错误:

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

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

但是它没有用,我个人开始复制,我认为主要问题在这里:

  

严重性代码描述项目文件行抑制状态   错误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有效

这是我的VS 2019的配置:

img1

img1

项目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进行测试。

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

2 个答案:

答案 0 :(得分:0)

尝试一下,

1。在您的 Xamarin-MaterialSearchBar - Transforms - Metadata.xml

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

2。在您的 Xamarin-MaterialSearchBar -添加项中,创建 partial 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();
      }
   }
}

您还可以参考:Java Binding Abstract class not being generated.

答案 1 :(得分:0)

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

  1. 添加以下NuGet软件包:

    这些是build.gradle中的最低要求。

  2. 使用这段代码(受Leo Zhu - MSFT' answer启发)从您的 Metadata.xml 中从将来的库中删除类SuggestionsAdapter

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

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

    此外,您可能会问为什么我选择在DefaultSuggestionsAdapter上迁移RecommendationsAdapter。有两个原因:

    • SuggestionsAdapter是基类
    • DefaultSuggestionsAdapter调用您无法从C#访问的 XML代码,您可以在第34、55和56行中看到它们。
  3. 构建您的库。

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

  5. 将代码从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);
            }
        }
    }
    
  6. 再次构建您的项目,仅此而已!您的图书馆可以正常使用。

如果要检查result