RecyclerView onClick()无法正常工作,而不是仅在单击单个元素时才响应

时间:2016-12-26 13:39:30

标签: android onclick android-recyclerview

由于字符限制,我的标题不具有描述性。我的目标是当我单击recyclerView内的元素时,它会打印出RecyclerView位置(基本上是索引)。

OnClick()与XML和常规OnClick方法一起使用只会导致我的应用崩溃。

public class torrentAdapter extends RecyclerView.Adapter<torrentAdapter.MyViewHolder> {

    private List<torrent> seznamTorrentov;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView upSpeed, downSpeed, progress, name;
        public ImageView delete, stanje;

        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            upSpeed = (TextView) view.findViewById(R.id.upSpeed);
            downSpeed = (TextView) view.findViewById(R.id.downSpeed);
            progress = (TextView) view.findViewById(R.id.progress);
            delete = (ImageView) view.findViewById(R.id.zbrisi);
            stanje = (ImageView) view.findViewById(R.id.stanje);
        }
    }


    public torrentAdapter(List<torrent> seznamTorrentov) {
        this.seznamTorrentov = seznamTorrentov;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.torrenti_vrsta, parent, false);

        return new MyViewHolder(itemView);

    }

    @Override

    public void onBindViewHolder(MyViewHolder holder, final int position) {
        torrent torrent = seznamTorrentov.get(position);
        holder.name.setText(torrent.getName());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("POZICIJA " + position);
            }
        });
        holder.progress.setText(String.valueOf(torrent.getProgress() + "%"));
        //TUKAJ ZAOKROŽUJEMO VREDNOSTI
        //TRENUTNO NI PODPORE ZA GIGABITNE PRENOSE
        //S SPREMENLJIVKAMA sizeUname in Dname spreminjamo, ali napišemo na koncu hitrosti downloada MB/S ali KB/s
        int sizeD = 1024;
        int sizeU = 1024;
        String sizeUname = "KB/s";
        String sizeDname = "KB/s";
        if (torrent.getDownSpeed() > 1000000) {
            sizeD = 1024 * 1024;
            sizeDname = "MB/s";
        }
        if (torrent.getUpSpeed() > 1000000) {
            sizeU = 1024 * 1024;
            sizeUname = "MB/s";
        }

        holder.downSpeed.setText(String.valueOf(Math.round(torrent.getDownSpeed()) / sizeD) + sizeDname);
        holder.upSpeed.setText(String.valueOf(Math.round(torrent.getUpSpeed()) / sizeU) + sizeUname);
        //TODO: ZRIHTAJ,DA SE POJAVIJO SLIKE IN PROGRESS BAR
        if (torrent.getDownSpeed() == 0 && torrent.getUpSpeed() == 0) {
            holder.stanje.setImageResource(R.drawable.check);

        }
        if (torrent.getStanje().contains("Stopped") || torrent.getStanje().contains("Paused")) {
            holder.stanje.setImageResource((R.drawable.stop));
        }
        if (torrent.getStanje().contains("Downloading")) {
            holder.stanje.setImageResource(R.drawable.down);


        }
        if (torrent.getStanje().contains("Seeding")) {
            holder.stanje.setImageResource(R.drawable.up);
        }

        holder.delete.setImageResource(R.drawable.delete);
    }

    @Override
    public int getItemCount() {
        return seznamTorrentov.size();
    }
}

这是我的RecyclerViewAdapter。删除是drawable我想OnClick听。通过我当前的实现,无论我在哪里点击RecyclerView,我都会收到回复。这是一个问题,因为我应该有两个不同的“按钮”,点击时有自己的功能。

如果相关,这是我在OnClick使用XML时遇到的错误:

  

java.lang.IllegalStateException:找不到父或祖先的方法deleteTorrent(View)for android:onClick属性在视图类android.support.v7.widget.AppCompatImageView上定义,id为'zbrisi'

3 个答案:

答案 0 :(得分:0)

我认为你已经在你的适配器布局xml标签中提到了这个id的属性  像这样的zbrisi

android:onclick="deleteTorrent"

将其删除,因为它会干扰您当前的onClickListener方法

答案 1 :(得分:0)

如果在xml中定义了onclick,那么它必须在java中实现
即。

android:onclick="delete"

java必须有

public void delete(){//codes here}

但我建议不要这样做,因为有一种更好的方法是在视图本身上实现onClickListener

答案 2 :(得分:0)

如果我理解正确,上面的代码会返回错误的位置?这个职位是什么? 0? 1?或者每次都不同?尝试将“public void onBindViewHolder(MyViewHolder holder,final int position)”更改为“public void onBindViewHolder(MyViewHolder holder,int position)”。