我有一个非活动类(恰好是一个持有cardviews的RecyclerView适配器),我正在尝试使用"真实的动作"使用共享图像元素从cardview转换到最终活动。我很容易在两个活动类之间实现它,但是从非活动类到活动类,我无法这样做?
这是我的代码(RecyclerViewAdapter.java):
公共类RVAdapter扩展了RecyclerView.Adapter {
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
TextView myid;
front ft = new front();
PersonViewHolder(View itemView) {
super(itemView);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
myid = (TextView)itemView.findViewById(R.id.IDTAG);
cv = (CardView)itemView.findViewById(R.id.cv);
cv.setUseCompatPadding(true);
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String cardid = myid.getText().toString().trim();
Intent callIntent = new Intent("io.brushed.databasecardview.VIEWDATAINSIDE");
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.putExtra("THEID",cardid);
String transitionName = "oktrans";
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this,
personPhoto, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(this, callIntent, options.toBundle());
}
});
}
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.myid.setText(persons.get(i).id);
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return persons.size();
}
}
它无法解决这个&#34;这个&#34;关键字,我应该用什么替换它,因为RecyclerView适配器是一个非活动类?
答案 0 :(得分:3)
您可以将活动类的上下文传递给非活动类(恰好是持有cardview的RecyclerView适配器)
e.g。
RecyclerViewAdapter1(Context context...)
然后为此目的使用该上下文:
String transitionName = "oktrans";
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this,
personPhoto,transitionName);
ActivityCompat.startActivity(context, callIntent, options.toBundle());
编辑:
根据您的新修改。
将活动作为参数传递。
PersonalViewHolder(View view, Activity activity) {
...
ActivityCompat.startActivity(activity, ...);
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
TextView myid;
front ft = new front();
PersonViewHolder(View itemView, final Context context) {
super(itemView);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
myid = (TextView)itemView.findViewById(R.id.IDTAG);
cv = (CardView)itemView.findViewById(R.id.cv);
cv.setUseCompatPadding(true);
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String cardid = myid.getText().toString().trim();
Intent callIntent = new Intent("io.brushed.databasecardview.VIEWDATAINSIDE");
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.putExtra("THEID",cardid);
String transitionName = "oktrans";
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(context,
personPhoto, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(context, callIntent, options.toBundle());
}
});
}
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v, viewGroup.getContext());
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.myid.setText(persons.get(i).id);
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return persons.size();
}
}
答案 1 :(得分:0)
您无法使用this
,因为您位于持有其引用的块内。
另外,如果你看到startActivity()方法的正确实现。
需要以下参数:
startActivity(Activity activity, Intent intent, Bundle options)
如果在片段
中,请将您的活动设为ActivityName.this
或getActivity()
编辑:
根据您的新修改。
将活动作为参数传递。
PersonalViewHolder(View view, Activity activity) {
...
ActivityCompat.startActivity(activity, ...);
}