视图绑定和onBindViewHolder在同一类中

视图绑定和onBindViewHolder在同一类中

本文介绍了Recyclerview ViewHolder:onCreateViewHolder,视图绑定和onBindViewHolder在同一类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerView.ViewHolder中,将视图传递给构造函数.此视图是放大的布局. RecyclerView.ViewHolder仅将视图与findViewById绑定.

In RecyclerView.ViewHolder , a view is passed to the constructor. This view is an inflated layout. The RecyclerView.ViewHolder only bind the views with findViewById.

RecyclerView.Adapter的作用是:

  • onCreateViewHolder
  • 中增加布局
  • 将ViewHolder与使用onBindViewHolder
  • 设置的数据绑定
  • inflate the layout in onCreateViewHolder
  • bind the ViewHolder with the data set with onBindViewHolder

我有几个RecyclerViews显示相同的数据列表.我希望每个RecyclerView与其各自的ViewHolder显示不同.我的目标是为每个RecyclerView使用相同的通用RecyclerView.Adapter.因此,必须将ViewHolder传递到此RecyclerView.Adapter.我正在尝试实现一个可以实现所有3种方法的ViewHolder.知道如何实现吗?

I have several RecyclerViews displaying the same list of data. I want each RecyclerView to display differently with their respective ViewHolder. My goal is to use the same generic RecyclerView.Adapter for each RecyclerView.So the ViewHolder has to be passed to this RecyclerView.Adapter.I'm trying to implement a ViewHolder that can implement all 3 methods.Any idea how to achieve this ?

我看了不同的项目.迄今为止最好的 AdapterDelegates 可以解决此问题.但是您仍然必须处理AdapterDelegate和ViewHolder类.如何将两者合并在同一个班级? (没有内部类)

I looked at different projects. The best so far, AdapterDelegates circumvents this problem. But you still have to deal with AdapterDelegate and ViewHolder classes . How to combine both in the same class ? ( without inner class )

推荐答案

按照承诺,我创建了一个新的答案,该答案不使用反射.从技术上讲,它使用两个类(工厂类和持有者类),而不是一个,但是是相同的.该代码已经过测试,并且可以正常工作.

As promised I have created a new answer, which does not use reflection. It technically uses two classes (a factory class and a holder class), not one, but is just the same. The code is tested, and it works.

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>
{
    List<Object> _data;
    MyViewHolder.Factory _factory;

    MyAdapter(List data, MyViewHolder.Factory factory)
    {
        _data = data;
        _factory = factory;
        if (_data == null || _factory == null)
        {
            throw new NullPointerException("Both data and factory cannot be null!");
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return _factory.createViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        holder.bindViewHolder(_data.get(position));
    }

    @Override
    public int getItemCount()
    {
        return _data.size();
    }
}
public abstract class MyViewHolder extends RecyclerView.ViewHolder
{
    public interface Factory
    {
        public abstract MyViewHolder createViewHolder(ViewGroup parent, int viewType);
    }

    public MyViewHolder(View itemView)
    {
        super(itemView);
    }

    public abstract void bindViewHolder(Object data);
}
public class MainActivity extends AppCompatActivity
{

    static class NameViewHolder extends MyViewHolder
    {
        // If preferred, the following can be created anonymously in code
        static class Factory implements MyViewHolder.Factory
        {
            @Override
            public MyViewHolder createViewHolder(ViewGroup parent, int viewType)
            {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_recycler_item, parent, false);
                return new NameViewHolder(v);
            }
        };

        TextView tv;
        NameViewHolder(View v)
        {
            super(v);
            tv = (TextView)v.findViewById(R.id.textView);
        }

        @Override
        public void bindViewHolder(Object data)
        {
            tv.setText((String)data);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
        rv.setHasFixedSize(true);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);

        ArrayList<String> data = new ArrayList();
        for (int i = 1; i < 100; ++i)
            data.add(Integer.toString(1000 + i));

        MyAdapter adapter = new MyAdapter(data, new NameViewHolder.Factory());
        rv.setAdapter(adapter);
    }

}

这篇关于Recyclerview ViewHolder:onCreateViewHolder,视图绑定和onBindViewHolder在同一类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:14