在RecyclerView中访问同一行中的所有元素

时间:2016-11-21 13:31:12

标签: java android xml android-recyclerview

RecyclerView

我有一个Recyclerview,如每行包含的图片所示 一个两个按钮

我已将onClickListener添加到Button-A

如果用户点击Button-A,

无论如何都要访问同一行中的Button-B 在同一行?

class BooksHolder extends RecyclerView.ViewHolder{

    public BooksHolder(View itemView) {
        super(itemView);
        Download=(Button)itemView.findViewById(R.id.download);
        pb=(ProgressBar)itemView.findViewById(R.id.progressBar1) ;
        Download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Download.setVisibility(Button.INVISIBLE);
                new DelayTask().execute();

            }
        });
    }
}

public class DelayTask extends AsyncTask<Void, Integer, String> {
    int count=0 ;
    @Override
    protected void onPreExecute() {
        pb.setVisibility(ProgressBar.VISIBLE);

    }

    @Override
    protected String doInBackground(Void... params) {
        while (count < 5) {
            SystemClock.sleep(1000);
            count++;
            publishProgress(count * 20);
        }
        return "Complete";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        pb.setProgress(values[0]);

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        SystemClock.sleep(100);
        pb.setVisibility(ProgressBar.INVISIBLE);
        Download.setVisibility(Button.VISIBLE);
        Download.setEnabled(false);
    }
}

下载是Button已在Adapter中声明 pb是ProgressBar在适配器中声明的

2 个答案:

答案 0 :(得分:1)

您可以在viewHolder中实现onClickListener

实施例

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Button button1;
Button button2;

public MyViewHolder(View itemView) {
    super(itemView);
    button1 = (Button) itemView.findViewById(R.id.button1);
    button2 = (Button) itemView.findViewById(R.id.button2);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == button1) {
        // your action here
    } else if (view == button2) {
        // your action here
    }
}
}

答案 1 :(得分:0)

我想当你点击按钮A时,你问如何访问按钮B?

如填充RecyclerView

的适配器类中的代码片段所示
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public Button buttonA, buttonB;

        public MyViewHolder(View view) {
            super(view);
            buttonA = (Button) view.findViewById(R.id.btnA);
            buttonA.setOnClickListener(this);
            buttonB = (Button) view.findViewById(R.id.btnB);
        }

        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.btnA){
                buttonB.setText("Button A is clicked");
            }
        }
    }

运行你的应用,最初显示屏幕就像..

enter image description here

然后单击任何行的按钮A,您将看到按钮B的标题在同一行中更改。如下图所示.. enter image description here