为什么在动态添加视图时会覆盖我的侦听器?

时间:2017-12-10 17:36:22

标签: android android-recyclerview android-view

我想动态创建包含Android中的recyclerview的视图列表。 这些观点很好,而不是听众,因为它们会覆盖。

我尝试用里面带有RecyclerView的项目制作一个listView,并且不起作用;将监听器放在回收器视图中的每个视图中并且相同...

现在我尝试使用linearlayout创建一个滚动视图,动态添加包含recyclelerview的视图,并将监听器放入其中。

这是我的代码:

public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_listas_reproduccion, container, false);
        linearToInsert = (LinearLayout)view.findViewById(R.id.linearToInsert);

        /*DBHelper db = new DBHelper(getContext());
        db.insertSection("Anime");
        ClaseLista cl3 = new ClaseLista("Drama", "0", "Anime");
        db.insertNombreListas(cl3);
        ClaseLista cll3 = new ClaseLista("Openings", "0", "Anime");
        db.insertNombreListas(cll3);
        ClaseLista clll3 = new ClaseLista("Endings", "0", "Anime");
        db.insertNombreListas(clll3);*/

        arrays = SingletonArrays.getInstance();
        ArrayList<String> sections = arrays.getSection();

        for(String section : sections)
        {
            listas = arrays.getListFromSection(section);
            if(listas!=null || listas.size()!=0)
            {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v = vi.inflate(R.layout.section_item, null);
                nameSection = (TextView) v.findViewById(R.id.nameSection);
                recycler = (RecyclerView) v.findViewById(R.id.recyclerView2);


                nameSection.setText(section);
                AdapterListasRV ap = new AdapterListasRV(listas);
                GridLayoutManager mLayoutManager = new GridLayoutManager(getContext(),3);
                recycler.setLayoutManager(mLayoutManager);
                recycler.setAdapter(ap);

                recycler.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), recycler, new RecyclerItemClickListener.OnItemClickListener()
                {
                    @Override
                    public void onItemClick(View view2, int position)
                    {
                        DBHelper db = new DBHelper(getContext());
                        if(db.getCancionLista(listas.get(position).getNombre()).size()!=0)
                        {
                            Intent intent = new Intent(getContext(), ActivityListaSelect.class);
                            Bundle b = new Bundle();
                            b.putString("NombreLista", listas.get(position).getNombre());
                            intent.putExtras(b);
                            getContext().startActivity(intent);
                        }
                        else
                        {
                            Intent intent = new Intent(getContext(), ActivityAddLista.class);
                            Bundle b = new Bundle();
                            b.putString("NombreLista", listas.get(position).getNombre());
                            intent.putExtras(b);
                            getContext().startActivity(intent);
                        }
                    }

                    @Override
                    public void onItemLongClick(View view, final int position)
                    {
                        recycler.getAdapter().notifyItemRemoved(position);
                    }
                }));

                linearToInsert.addView(v);
            }
        }

        return view;
    }

1 个答案:

答案 0 :(得分:0)

每次设置:recycler.addOnItemTouchListener(new RecyclerItemClickListener

您只需擦除上一个,然后设置一个新的,因为每次充气时来自同一资源的回收站都是相同的

recycler = (RecyclerView) v.findViewById(R.id.recyclerView2);

如果我想要动态的东西,则应该明确实例化

new RecyclerView()