4
我有一個應用程序,裏面充滿了一些卡片,在我製作的自定義RecyclerView(RV)適配器的幫助下,充滿了RecyclerView。 然後我試圖按照this tutorial來添加活動之間的轉換。但是,如教程中所示,在Activity中創建Intent時會調用動畫,並且我的OnClick代碼位於RVAdapter中。這使我對法從RecyclerView適配器調用過渡動畫而不是活動
Intent intent = new Intent(HomeActivity.this, TargetActivity.class);
intent.putExtra(TargetActivity.ID, Contact.CONTACTS[position].getId());
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
// the context of the activity
MainActivity.this,
// For each shared element, add to this method a new Pair item,
// which contains the reference of the view we are transitioning *from*,
// and the value of the transitionName attribute
new Pair<View, String>(view.findViewById(R.id.CONTACT_circle),
getString(R.string.transition_name_circle)),
new Pair<View, String>(view.findViewById(R.id.CONTACT_name),
getString(R.string.transition_name_name)),
new Pair<View, String>(view.findViewById(R.id.CONTACT_phone),
getString(R.string.transition_name_phone))
);
ActivityCompat.startActivity(HomeActivity.this, intent, options.toBundle());
我試圖將活動對象傳遞給在其構造方法的RVAdapter所需的活動沒有進入,但它只能導致NullPointerException異常。
這裏是我的RVAdapter代碼:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.AppVH>{
ArrayList<App> apps;
@Override
public AppVH onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new AppVH(v);
}
@Override
public void onBindViewHolder(AppVH holder, int position) {
holder.name.setText(apps.get(position).getName());
holder.artist.setText(apps.get(position).getArtist());
String p = "";
if(apps.get(position).getPrice()==0.0) p="Free";
else p= "$"+apps.get(position).getPrice()+" "+apps.get(position).getCurrency();
holder.price.setText(p);
Picasso.with(AppListActivity.context).load(apps.get(position).getUrlImLarge()).into(holder.icon);
}
@Override
public int getItemCount() {
return apps.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
RecyclerViewAdapter(ArrayList<App> applications){
this.apps = applications;
}
// Clean all elements of the recycler
public void clear() {
apps.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(ArrayList<App> list) {
apps.addAll(list);
notifyDataSetChanged();
}
public class AppVH extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView cv;
TextView name;
TextView artist;
TextView price;
ImageView icon;
private final Context c;
public AppVH(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardview);
name = (TextView) itemView.findViewById(R.id.app_name);
artist = (TextView) itemView.findViewById(R.id.app_artist);
price = (TextView) itemView.findViewById(R.id.app_price);
icon = (ImageView) itemView.findViewById(R.id.app_icon);
itemView.setOnClickListener(this);
c = itemView.getContext();
}
@Override
public void onClick(View v) {
final Intent intent;
Log.d("Debugtext","Card with position " + getAdapterPosition() + " was touched.");
intent = new Intent(c, AppDetailActivity.class);
intent.putExtra("app",apps.get(getAdapterPosition()));
c.startActivity(intent);
}
}
}
以下是我的適配器添加到RecyclerView在我的 「HomeActivity」。
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setHasFixedSize(true);
GridLayoutManager gridlm = new GridLayoutManager(getApplicationContext(),columns);
rv.setLayoutManager(gridlm);
rvadapter = new RecyclerViewAdapter(apps);
rv.setAdapter(rvadapter);
是否有一種簡單的方法來從Adapter中調用ActivityCompat.startActivity(...)方法?
我接受這個答案,因爲我沒有收到任何評論和一些upvotes。我不確定這是否是更好的做法,但它對我有用。 –
apps =列表???? –
@ArpitPatel ArrayList apps; –