android - 在某个View(或者Adapter中)中获得activity的实例

访问量: 76

参考:https://stackoverflow.com/questions/8276634/android-get-hosting-activity-from-a-view

代码:

    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.market_item_view, parent, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    // 在这里就可以使用了.
                    Activity activity = (Activity)view.getContext();
                    Intent intent = new Intent(activity, TipsForMarketActivity.class);
                    activity.startActivity(intent);
            }

        });

    }

订阅/RSS Feed

Subscribe

分类/category