如何在Android的recyclerview中处理点击事件?

时间:2016-06-13 10:12:26

标签: android list radio-button android-recyclerview

我将单选按钮放在回收站视图中。当我单击一个单选按钮导航到具有所选值的其他页面时。

4 个答案:

答案 0 :(得分:1)

 @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

    viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // handle click event here
            }
        });

        viewHolder.itemView.setTag(your_item);

    }

答案 1 :(得分:0)

使用:
  在onBind中,只需将数据设置为标记

即可
    radioButton.setTag(yourData);

    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                yourData = buttonView.getTag();
                //Do code here
            }
        });

答案 2 :(得分:0)

如果您正在使用Radio组,则必须执行以下代码

          Button bt = (Button) findViewById(R.id.btnDisplay);

          bt.setOnClickListener(new OnClickListener() {

          @Override
         public void onClick(View v) {

        // get selected radio button from radioGroup
        int selectedId = radiogroup .getCheckedRadioButtonId();

        // find the radio button by returned id
        RadioButton radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MainActivity.this, 
           radioButton.getText(),Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(this,nextActivity.class);

            Then put the information you want to pass on the intent:

            intent.putExtra("checked",selectedId );

            Then start the next activity:

            startActivity(intent);

            On the next activity you recover that info like this:

            Intent intent = getIntent();

            int checked = intent.getIntExtra("checked");

    }
   });
  

如果仅使用单选按钮,则必须执行以下代码

     public class MainActivity extends AppCompatActivity {

RadioButton android;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    android = (RadioButton)findViewById(R.id.rdbAndroid);

}
public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    String str="";

    // Check which radio button was clicked

    switch(view.getId()) {

        case R.id.rdbAndroid:

            if(checked)

              str = "Android Selected";

              android.setchecked(true); 

               Intent intent = new Intent(this,nextActivity.class);

            Then put the information you want to pass on the intent:

            intent.putExtra("checked",str );

            Then start the next activity:

            startActivity(intent);

            On the next activity you recover that info like this:

            Intent intent = getIntent();

            int checked = intent.getIntExtra("checked");


            break;

    }

    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}

}

答案 3 :(得分:0)

  

在Recyclerview中,您必须这样做

     // declare variable :

     String typeId,fueltype,droidId,droidType ;

     Radiobutton droid,android  ;

    // get the id of the view 

    android = (RadioButton)findViewById(R.id.rdbAndroid);

   droid = (RadioButton)findViewById(R.id.rdbdroid );

  //Click listner 

 android .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

 if (android .isChecked()) {

fuelType = petrol.getText().toString();

typeId="1";

 Intent intent = new Intent(this,nextActivity.class);

        Then put the information you want to pass on the intent:

        intent.putExtra("checked",typeId);

        Then start the next activity:

        startActivity(intent);

        On the next activity you recover that info like this:

        Intent intent = getIntent();

        int checked = intent.getIntExtra("checked");

        }else if(droid.ischecked()){
        droidType = droid.getText().toString();

       droidId="2";

       Then put the information you want to pass on the intent:

        intent.putExtra("checked",droidId);

        Then start the next activity:

        startActivity(intent);

        On the next activity you recover that info like this:

        Intent intent = getIntent();

        int checked = intent.getIntExtra("checked");


       // or you can use tag

       android.setTag(yourData);

       droid.setTag(yourData);

       yourData = compoundButton.getTag();
       }



        }
        }
         });