当前位置: 代码迷 >> 综合 >> android 开发 在一个工具类(或者适配器class)里启动activity
  详细解决方案

android 开发 在一个工具类(或者适配器class)里启动activity

热度:95   发布时间:2023-10-30 12:44:19.0

实现思路:

1.需要给工具类里传入context;

2.使用上下文mContext.startActivity启动activity

例子1:

public class SafePlaceRecyclerViewAdapter  {private Context mContext;public SafePlaceRecyclerViewAdapter(Context context){mContext = context;}public void start(){Intent intent = new Intent(mContext,SafeAreaInformation.class);mContext.startActivity(intent);}}


例子2:

public class SafePlaceRecyclerViewAdapter extends RecyclerView.Adapter<SafePlaceRecyclerViewAdapter.ViewHolder> {private List <SafePlaceData> mList;private Context mContext;public SafePlaceRecyclerViewAdapter(Context context, List<SafePlaceData> list){this.mList = list;mContext = context;}@Overridepublic ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.safe_place_row_layout,parent,false);ViewHolder viewHolder = new ViewHolder(view);viewHolder.safePlaceRowLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(v.getContext(),SafeAreaInformation.class);mContext.startActivity(intent);}});return viewHolder;}

  相关解决方案