我想在我的自定义适配器中使用 overridePendingTransition
方法,遗憾的是,此方法无法检测到!
我用它如下:
v.getContext().overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
我开始将活动放入适配器,因此我想在Adapter中运行此方法。
我的自定义适配器代码:
public class levels_card_adapter extends
RecyclerView.Adapter<levels_card_adapter.ViewHolder> {
Context mContext;
Activity activity = (Activity) mContext;
private static String[] adapter_levels_number;
public levels_card_adapter(String[] adapter_levels_num_data) {
adapter_levels_number = adapter_levels_num_data;
}
@Override
public levels_card_adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.levels_recycler_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.adapter_levels_text.setText(adapter_levels_number[position].toString());
viewHolder.adapter_levels_version = adapter_levels_number[position];
}
@Override
public int getItemCount() {
return adapter_levels_number.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView adapter_levels_text;
public String adapter_levels_version;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
adapter_levels_text = (TextView) itemLayoutView
.findViewById(R.id.level_thumnail_text);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent question_page_intent = new Intent(v.getContext(), com.tellfa.braingame.activities.question_Page.class);
v.getContext().startActivity(question_page_intent);
v.getContext().overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
}
});
}
}
我的自定义适配器新代码:
public class levels_card_adapter extends
RecyclerView.Adapter<levels_card_adapter.ViewHolder> {
Context mContext;
Activity activity = (Activity) mContext;
private static String[] adapter_levels_number;
public levels_card_adapter(Activity activity, String[] adapter_levels_num_data) {
this.activity = activity;
adapter_levels_number = adapter_levels_num_data;
}
@Override
public levels_card_adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.levels_recycler_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.adapter_levels_text.setText(adapter_levels_number[position].toString());
viewHolder.adapter_levels_version = adapter_levels_number[position];
}
@Override
public int getItemCount() {
return adapter_levels_number.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView adapter_levels_text;
public String adapter_levels_version;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
adapter_levels_text = (TextView) itemLayoutView
.findViewById(R.id.level_thumnail_text);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent question_page_intent = new Intent(v.getContext(), com.tellfa.braingame.activities.question_Page.class);
v.getContext().startActivity(question_page_intent);
activity.overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
}
});
}
}
如何在适配器中使用此方法( overridePendingTransition )?
答案 0 :(得分:5)
overridePendingTransition
是Activity
的一种方法。您已将活动context
作为成员变量。你可以使用它。
Intent question_page_intent = new Intent(v.getContext(), com.tellfa.braingame.activities.question_Page.class);
activity.startActivity(question_page_intent);
activity.overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
还要确保在构造函数中初始化activity
。
public levels_card_adapter(Activity activity,String[] adapter_levels_num_data) {
this.activity =activity;
adapter_levels_number = adapter_levels_num_data;
}