Java 如何将项目位置从适配器传递到片段?
我想从适配器获取项目位置到片段,我正在使用bundle。看看我的代码: 适配器类中的onClick方法:Java 如何将项目位置从适配器传递到片段?,java,android,android-studio,android-recyclerview,bundle,Java,Android,Android Studio,Android Recyclerview,Bundle,我想从适配器获取项目位置到片段,我正在使用bundle。看看我的代码: 适配器类中的onClick方法: int p1 = getAdapterPosition() @Override public void onClick(View view) { AppCompatActivity activity = (AppCompatActivity) view.getContext(); SevenFragment myFragment = new SevenFragment();
int p1 = getAdapterPosition()
@Override
public void onClick(View view) {
AppCompatActivity activity = (AppCompatActivity) view.getContext();
SevenFragment myFragment = new SevenFragment();
Bundle bundle=new Bundle();
bundle.putString("position", String.valueOf(p1)); //key and value
myFragment.setArguments(bundle);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, myFragment)
.addToBackStack(null)
.commit();
}
我想在这里打印片段:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
String user_id = bundle.getString("position");
}