如何使用RecyclerView完成Adapter类的活动

时间:2018-10-17 13:04:22

标签: android android-recyclerview adapter

我正在尝试完成适配器类的活动,同时单击Recyclerview项目 使用代码

public void onBindViewHolder(@NonNull CountryCodeAdapter.CountryViewHolder holder, int position{
    CountryModel countryMode = countryModels.get(position);
    final String cCode = countryModel.getName();
    holder.llcountryCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(),RegisterActivity.class);                        
            i.putExtra("countryCode", cCode);
            v.getContext().startActivity(i);
            ((AppCompatActivity)context).finish();
        }
    }
}

也尝试过这种方法,但是没有用

((Activity)context).finish();

我收到此错误:

cannot be cast to android.support.v7.app.AppCompatActivity

4 个答案:

答案 0 :(得分:4)

为适配器(在适配器类内部)创建interface,像这样:

public interface YourAdapterInteraction {
        void onClickCountryCode();
 }

将您的Activity implement变成interface,如下所示:

public class YourActivity extends AppCompatActivity implements YourAdapter.YourAdapterInteraction

YourActivity内部:

@Override
    public void onClickCountryCode() {
        Intent i = new 
        Intent(this,RegisterActivity.class);
        i.putExtra("countryCode", cCode);
        startActivity(i);
        finish();
 }

答案 1 :(得分:1)

在Adapter构造函数中获取Activity实例:

 public class MyAdapter extends IAdapter<RecyclerView.ViewHolder,MyDS> {
    private Activity activity;

    public MyAdapter(Activity activity) {
        this.activity = activity;
    }

  }

答案 2 :(得分:0)

执行此操作:

public void onBindViewHolder(@NonNull                                                                                     
    CountryCodeAdapter.CountryViewHolder holder, int position{
      CountryModel countryModel = countryModels.get(position); 

      final String name = countryModel.getName();

      holder.llcountryCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new Intent(context,RegisterActivity.class);
        i.putExtra("countryCode", cCode);
        context.startActivity(i);
        ((Activity)context).finish();
          }
        }
    });

是:

Public void onBindViewHolder(@NonNull                                                                                     
    CountryCodeAdapter.CountryViewHolder holder, int position{
      CountryModel countryModel = countryModels.get(position); 

      final String name = countryModel.getName();

      holder.llcountryCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new 
        Intent(v.getContext(),RegisterActivity.class);
        i.putExtra("countryCode", cCode);
        v.getContext().startActivity(i);
        ((AppCompatActivity)context).finish();
          }
        }
    });

答案 3 :(得分:0)

在recyclerView中创建一个接口回调,在您的活动中实现该方法,并在单击某项时调用该方法。

// in your recyclerView
public interface RecyclerViewCallback {
        void onItemClick(Item item)
}
// in your activity
@Override
void onItemClicked(Item item) {
        this.finish();
}