Annotations Recycler View ItemClick

Posted

技术标签:

【中文标题】Annotations Recycler View ItemClick【英文标题】: 【发布时间】:2018-06-23 07:47:20 【问题描述】:

我正在尝试根据官方 androidAnnotations 网站的以下教程在我的 Activity 中实现简单的回收器视图:adapter link

但是,当我完成所有步骤并尝试实现 @ItemClick 方法时,它给了我以下错误:

Error:(84, 36) 错误:不兼容的类型:RecyclerView 无法转换为 AdapterView

在我生成的活动代码下突出显示以下代码:

...((AdapterView<?> ) this.miestnostiList).setOnItemClickListener(new OnItemClickListener() ...

根据指南,我创建了以下基类:

public abstract class RecyclerViewAdapterBase<T, V extends View> extends RecyclerView.Adapter<ViewWrapper<V>> 

    protected List<T> items = new ArrayList<T>();

    @Override
    public int getItemCount() 
        return items.size();
    

    @Override
    public final ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) 
        return new ViewWrapper<V>(onCreateItemView(parent, viewType));
    

    protected abstract V onCreateItemView(ViewGroup parent, int viewType);



public class ViewWrapper<V extends View> extends RecyclerView.ViewHolder 

    private V view;

    public ViewWrapper(V itemView) 
        super(itemView);
        view = itemView;
    

    public V getView() 
        return view;
    

然后我创建了我的行视图项如下:

@EViewGroup(R.layout.activity_list_miestnosti_row)
public class MiestnostItemView extends LinearLayout
    @ViewById(R.id.txt_miestnost_row_nazov)
    TextView nazov;

    public MiestnostItemView(Context context) 
        super(context);
    

    public void bind(Miestnost miestnost)
        nazov.setText(miestnost.getNazov());
    

最后是我的适配器类:

public class ListMiestnostiAdapter extends RecyclerViewAdapterBase<Miestnost, MiestnostItemView> 
    @RootContext
    Context context;


    @Override
    protected MiestnostItemView onCreateItemView(ViewGroup parent, int viewType) 
        return MiestnostItemView_.build(context);
    

    @Override
    public void onBindViewHolder(ViewWrapper<MiestnostItemView> holder, int position) 
        MiestnostItemView view = holder.getView();
        Miestnost miestnost = items.get(position);

        view.bind(miestnost);
    

在我的活动下,我在@AfterViews 部分初始化我的适配器:

    @ViewById
    RecyclerView miestnostiList;

    ListMiestnostiAdapter adapter;

    @AfterViews
    void bindAdapter() 
        GetData();//fetch data

        adapter = new ListMiestnostiAdapter();
        adapter.addData(miestnosti);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        miestnostiList.setLayoutManager(layoutManager);
        miestnostiList.setItemAnimator(new DefaultItemAnimator());
        miestnostiList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        miestnostiList.setAdapter(adapter);

    

这是我的@ItemClick 方法:

@ItemClick
void miestnostiListItemClicked(Miestnost miestnost)
    Log.i("", "iteClicked: ");

我在这里缺少什么?

【问题讨论】:

编译时出现这个错误? 【参考方案1】:

我认为 @ItemClick 不适用于 RecyclerView。您可以通过两种方式进行:

1- 更改您的 RecyclerViewAdapterBase 添加 onClick 支持,然后将该方法覆盖到子适配器中,如下所示:

public abstract class RecyclerViewAdapterBase<T, V extends View & ViewWrapper.Binder<T>> extends RecyclerView.Adapter<ViewWrapper<T, V>> 

    protected List<T> mItems = new ArrayList<T>();

    @Override
    public final ViewWrapper<T, V> onCreateViewHolder(ViewGroup parent, int viewType) 
        return new ViewWrapper<T, V>(onCreateItemView(parent, viewType));
    

    protected abstract V onCreateItemView(ViewGroup parent, int viewType);

    @Override
    public final void onBindViewHolder(ViewWrapper<T, V> viewHolder, int position) 
        V view = viewHolder.getView();
        T data = mItems.get(position);

    /*************************************************************/
    *       AT THIS POINT IT TRY TO SET A CLICK LISTENER     */
    /*************************************************************/
        setOnClickListener(view, data);

        view.bind(data);
    

    @Override
    public int getItemCount() 
        return null == mItems ? 0 : mItems.size();
    

    public void add(T item) 
        mItems.add(item);
        notifyDataSetChanged();
    

    public void addAll(Collection<T> collection) 
        mItems.addAll(collection);
        notifyDataSetChanged();
    

    public void clear() 
        mItems.clear();
    


    /**
     * override this into child adapter to manage click event
     */
    public void setOnClickListener(View v, T item) 

这是子适配器

@EBean
public class ChildAdapter extends RecyclerViewAdapterBase<Child, ChildView> 

@RootContext
Context ctx;
@Bean(ChildFinder.class)
IChildFinder finder;

@Bean
OttoBus mOttoBus;

/**
 * get data by finder
 */
public void initAdapter() 
    mItems = finder.findAll();


@Override
public int getItemViewType(int position) 
    return position;


@Override
protected ChildView onCreateItemView(ViewGroup parent, int viewType) 
    return ChildView_.build(ctx);



@Override
public void setOnClickListener(View v, final Child item) 
    v.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            mOttoBus.post(new RequestChildEvent(item));
        
    );


2- 否则,如果您使用自定义视图,则可以遵循事件驱动方法绑定,您直接单击视图(例如自定义视图的根视图),然后向组件发送事件必须对点击做出反应

【讨论】:

感谢您的帮助。最终决定采用数据绑定方式

以上是关于Annotations Recycler View ItemClick的主要内容,如果未能解决你的问题,请参考以下文章

Recycler View 不显示数据

xml Recycler View

67.Recycler View

xml 在ViewHolder中为Recycler View扩展视图

java 无尽的Recycler View Scroll监听器

Android recycler view onItemClick 识别适配器