【问题标题】:Error CS0115 '...OnBindViewHolder(Object, int)': no suitable method found to override错误 CS0115 '...OnBindViewHolder(Object, int)': 找不到合适的方法来覆盖
【发布时间】:2019-11-05 14:33:22
【问题描述】:

我正在绑定这个库:

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

项目的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 测试一下。

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

【问题讨论】:

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


    【解决方案1】:

    试试这个,

    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 - Additions,创建一个partialDefaultSuggestionsAdapter

    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.

    【讨论】:

    • 嗨,我注意到这个想法很复杂,因为 SuggestionsAdapter 中的 OnBindSuggestionHolder、SingleViewHeight 或 OnBindViewHolder 等方法在生成的代码中消失了,因为它们在崩溃代码中。知道为什么吗?我会说在另一种情况下也会发生同样的情况。
    • 我将您的扩展从 java.lang.Object 更改为 RecyclerView.Adapter,然后我得到同样的错误,我的猜测是绑定器正在替换从 RecyclerView.Adapter 到 java.lang 的继承。对象
    • 你能分享你的项目吗?这可能有助于更快地发现错误
    • 您好,项目在这里:dropbox.com/home/MaterialSearchBar,但它已被使用,您在 Java 中有此代码:@Override public void onBindViewHolder(V holder, int position) { onBindSuggestionHolder(suggestions.get(position )、持有人、职位);然而,奇怪的是活页夹找不到它,这是来自 Recycler developer.android.com/reference/android/support/v7/widget/… 的基本方法
    • 谢谢,因为这很奇怪,我不明白为什么它不能覆盖 RecyclerView.Adapter 的基本方法。
    【解决方案2】:

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

    1. 添加以下 NuGet 包:

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

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

      &lt;remove-node path="/api/package[@name='com.mancj.materialsearchbar.adapter']/class[@name='SuggestionsAdapter']" /&gt;

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

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

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

    4. 在 Additions 中创建一个名为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2018-09-29
      • 1970-01-01
      • 2020-07-01
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多