0

我正在绑定这个库:

https://github.com/mancj/MaterialSearchBar

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

我收到以下错误:

我试图遵循创建一些部分类的建议:

xamarin.android binding thorw '不实现继承的抽象成员'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的配置:

图像1

图像1

项目的 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 文件和项目来测试它。

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

4

2 回答 2

0

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

  1. 添加以下 NuGet 包:

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

  2. 使用这段代码SuggestionsAdapter从您的Metadata.xml中删除未来库中的类(灵感来自Leo Zhu - MSFT 的答案)。

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

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

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

    • 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. 再次构建您的项目,仅此而已!您的图书馆正在充分发挥作用。

如果你想检查结果

于 2019-06-26T17:59:31.787 回答
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 - Additions中,创建一个局部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。

于 2019-06-24T06:53:01.100 回答