如何使用 RecyclerView 从 Adapter class 完成一个 activity
How to finish an activity from Adapter class using RecyclerView
我正尝试在单击 Recyclerview
项时从适配器 class 完成 activity
使用代码
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
这样做:
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();
}
}
});
在适配器构造函数中获取 Activity 实例:
public class MyAdapter extends IAdapter<RecyclerView.ViewHolder,MyDS> {
private Activity activity;
public MyAdapter(Activity activity) {
this.activity = activity;
}
}
在您的 recyclerView 中创建一个接口回调,在您的 activity 中实现该方法并在单击项目时调用该方法。
// in your recyclerView
public interface RecyclerViewCallback {
void onItemClick(Item item)
}
// in your activity
@Override
void onItemClicked(Item item) {
this.finish();
}
为适配器制作一个 interface
(在适配器内部 class),如下所示:
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();
}
我正尝试在单击 Recyclerview
项时从适配器 class 完成 activity
使用代码
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
这样做:
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();
}
}
});
在适配器构造函数中获取 Activity 实例:
public class MyAdapter extends IAdapter<RecyclerView.ViewHolder,MyDS> {
private Activity activity;
public MyAdapter(Activity activity) {
this.activity = activity;
}
}
在您的 recyclerView 中创建一个接口回调,在您的 activity 中实现该方法并在单击项目时调用该方法。
// in your recyclerView
public interface RecyclerViewCallback {
void onItemClick(Item item)
}
// in your activity
@Override
void onItemClicked(Item item) {
this.finish();
}
为适配器制作一个 interface
(在适配器内部 class),如下所示:
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();
}