我的標題可能不會因爲一個字符的限制非常描述。我的目標是,當我點擊一個元素中的recyclerView
它打印出的RecyclerView
位置(基本指標)。RecyclerView的onClick()不工作,而不是隻迴應,當我點擊一個元素
使用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:找不到支持Android父母或祖先上下文方法deleteTorrent(查看):在視圖類android.support.v7.widget.AppCompatImageView onclick屬性定義id爲「zbrisi」
您可以添加更多的細節,你到底想? –