4

我创建了一个意图对象以及单击Bundlea 时CardView,然后我启动Activity. 我如何overridePendingTransition在里面声明?代码如下:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.textView.setText(strings[position]);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(holder.itemView.getContext(),SecondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("list",strings[position]);
            intent.putExtras(bundle);
            holder.itemView.getContext().startActivity(intent);
            //overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
        }
    });
}
4

4 回答 4

5

声明时必须将 an 传递Activity给您的RecyclerView.Adapter班级:

首先在适配器的构造函数中添加Activity字段:

public class MyAdapter extends RecyclerView.Adapter{

Activity mActivity;
ArrayList<YourModel> models;

public MyAdapter(ArrayList<YourModel> models,Activity mActivity){
    this.mActivity=mActivity;
    this.models=models;
}
}

然后将此行添加到您的Activity

Activity thisActivity=(Activity)this;

然后在您Activity声明您的适配器时:

MyAdapter myAdapter=new MyAdapter(models,thisActivity);

最后,您可以Activity在适配器中使用它进行转换,例如:

mActivity.overridePendingTransition();
于 2016-06-23T22:48:58.187 回答
1

文档

在 startActivity(Intent) 或 finish() 风格之一之后立即调用,以指定接下来要执行的显式过渡动画。

您需要使用上下文调用它。

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.textView.setText(strings[position]);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override 
    public void onClick(View view) {
        Intent intent = new Intent(holder.itemView.getContext(),SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("list",strings[position]);
        intent.putExtras(bundle);
        holder.itemView.getContext().startActivity(intent);
        mContext.overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out); 
    } 
}); 
} 
于 2016-06-23T21:53:59.467 回答
1

这对我有用,可能不是最好的方法,但至少它有效。在这种情况下, parent 是包含所有项目的父布局,但您可以将点击侦听器设置为任何视图。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout parent;
        ...
        ViewHolder(View itemView) {
            super(itemView);

            parent                  = itemView.findViewById(R.id.parent);
            ...
        }
    }

    private Context mContext;
    ...

    public MyAdapter(Context context, List<Something> something) {
        mContext = context;
        ...
    }
    ...
    private Context getContext() {
        return mContext;
    }
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int position) {
        ...
        viewHolder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SomeActivity.class);
                getContext().startActivity(intent);
                ((Activity) getContext()).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }
于 2018-04-18T13:41:30.640 回答
0
usersviewHolder.mview.setOnClickListener(new View.OnClickListener(){

    public Activity mContext;
    @Override
    public void onClick(View view){

        Intent eventpage_intent = new Intent(EventFragment.this.getActivity(), Event_Main_Page.class);
        eventpage_intent

        startActivity(eventpage_intent);

        mContext = (Activity) usersviewHolder.itemView.getContext();
        mContext.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }
});

这有效

于 2018-03-11T18:17:57.837 回答