如何在RecyclerView Adapter中使用“活动方法"? [英] How to use Activity Methods in RecyclerView Adapter?

查看:126
本文介绍了如何在RecyclerView Adapter中使用“活动方法"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要完成一项活动&每当完成Recyclerview onclick时,重新打开另一个活动.我已经在Recyclerview上成功实现了onclick.但是我无法在适配器中重新创建另一个活动.

I need to finish an Activity & Reopen another Activity whenever a Recyclerview onclick is done. I have Implemented onclick on the Recyclerview successfully. But I can't recreate another Activity in my Adapter.

我该如何解决此问题?

How can i solve this Issue ?

public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.MyVH> {

    private final LayoutInflater inflater;
    private List<Theme> ThemeList;

    public ThemeAdapter(Context context, List<Theme> ThemeList){
        inflater = LayoutInflater.from(context);
        this.ThemeList = ThemeList;
    }

    @Override
    public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.theme_card, parent, false);
        MyVH holder = new MyVH(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyVH holder, int position) {
        Theme current = ThemeList.get(position);
        holder.name.setText(current.Name);
        holder.mCardView.setCardBackgroundColor(Color.parseColor(current.Color));
    }

    @Override
    public int getItemCount() {
        return ThemeList.size();
    }

    class MyVH extends RecyclerView.ViewHolder implements View.OnClickListener {

        me.arulnadhan.robototextview.widget.RobotoTextView name;
        CardView mCardView;
        Context context;

        public MyVH(View itemView) {
            super(itemView);
            context = itemView.getContext();
            itemView.setOnClickListener(this);
            name= (me.arulnadhan.robototextview.widget.RobotoTextView) itemView.findViewById(R.id.Theme);
            mCardView = (CardView)itemView.findViewById(R.id.ThemeCard);
        }

        @Override
        public void onClick(View view) {

            switch (getAdapterPosition()){
                case 1:
                    Utility.setTheme(context, 1);
                    ThemeActivity.recreateActivity();
            }

public void recreateActivity() {
    finish();
    final Intent intent = IntentCompat.makeMainActivity(new ComponentName(this, MainActivity.class));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}
        }
    }
}

推荐答案

您可以在构造函数中传递对Activity的引用,如下所示:

You can pass in a reference to your Activity in the constructor, like this:

(...)
private final LayoutInflater inflater;
private List<Theme> ThemeList;

private final Activity mActivity;

public ThemeAdapter(Context context, Activity mActivity, List<Theme> ThemeList){
    inflater = LayoutInflater.from(context);
    this.ThemeList = ThemeList;

    this.mActivity = mActivity;
}
(...)

然后,在创建适配器时在活动"中执行以下操作:

Then, in your Activity when creating the adapter, do something like this:

ThemeAdapter adapter = new ThemeAdapter(getContext(), this, mThemeList);

然后您可以通过调用mActivity.someMethod()在适配器中使用Activity方法.

You can then use Activity methods in your adapter by calling mActivity.someMethod().

免责声明:未经测试(我从未使用过RecyclerView),但这在其他地方都可以使用.

Disclaimer: not tested (I've never used RecyclerView) but this works everywhere else.

这篇关于如何在RecyclerView Adapter中使用“活动方法"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆