【问题标题】:Call transition animation from RecyclerView Adapter instead of Activity从 RecyclerView 适配器而不是 Activity 调用过渡动画
【发布时间】:2016-02-07 19:52:08
【问题描述】:

在我制作的自定义 RecyclerView (RV) 适配器的帮助下,我有一个带有 RecyclerView 的应用程序,里面装满了一些卡片。 然后我尝试关注this tutorial,以便在活动之间添加过渡。但是,如教程中所示,在 Activity 中创建 Intent 时会调用动画,而我的 OnClick 代码位于 RVAdapter 中。这使我无法访问该方法所需的 Activity

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());

我尝试在其构造方法中将 Activity 对象传递给 RVAdapter,但它只导致 NullPointerExceptions。

这是我的 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);
    }
}
}

以下是我如何将适配器添加到“HomeActivity”中的 RecyclerView。

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);

有没有一种简单的方法可以从适配器中调用 ActivityCompat.startActivity(...) 方法?

【问题讨论】:

    标签: android android-animation android-recyclerview android-adapter


    【解决方案1】:

    所以,我做了一件非常简单的事情:我将上下文转换为一个活动。哇。

            @Override
            public void onClick(View v) {
                final Intent intent;
                //Opens the AppDetailActivity showing the selected App Card
                //Log.d("Debugtext","Card with position " + getAdapterPosition() + " was touched.");
                intent = new Intent(c, AppDetailActivity.class);
                intent.putExtra("app",apps.get(getAdapterPosition()));
    
                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation((Activity)c, (View)cv, "appcard");
                c.startActivity(intent, options.toBundle());
            }
    

    【讨论】:

    • 我接受了这个答案,因为我没有收到任何 cmets 和几个赞成票。我不确定这是否是更好的做法,但它对我有用。
    • @ArpitPatel ArrayList 应用程序;
    【解决方案2】:
    ActivityOptions options  = ActivityOptions.makeSceneTransitionAnimation((Activity) mContext);
    

    注意:mContext 是在回收器适配器构造函数中初始化的。

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2012-08-21
      • 1970-01-01
      • 2015-03-29
      相关资源
      最近更新 更多