这是适配器类内的我的视图持有者类,它是一个单独的类文件。回收站视图显示为一个片段,单击回收站视图项后,我需要转到一个新的分段活动并获取单击项中显示的文本
public class BookViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public final BookRecyclerAdapter mAdapter;
private TextView titleTextView, authorTextView;
public BookViewHolder(View itemView, BookRecyclerAdapter adapter) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.listItem_title_tView);
authorTextView = (TextView) itemView.findViewById(R.id.listItem_author_tView);
this.mAdapter = adapter;
itemView.setOnClickListener(this);
}
private void setBookCardView(String tText, String aText){
titleTextView.setText(tText);
authorTextView.setText(aText);
}
@Override
public void onClick(View v) {
String title = titleTextView.getText().toString();
String author = authorTextView.getText().toString();
//I NEED TO PASS THIS STRINGS TO NEW FRAGMENT
}
}
如何开始一个新的片段活动,该活动可以访问这两个字符串-title和author
答案 0 :(得分:1)
使用捆绑包将数据适配器传递给您的点击监听器中的片段
Fragment fragment = new FragmentName();
FragmentManager fragmentManager = context.getSupportFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("string1", "your string");
bundle.putString("string2", "your string"); //key and value
//set Fragmentclass Arguments
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
并获取片段中的数据
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
String strtext=getArguments().getString("string1"); //fetching value by key
String strtext1=getArguments().getString("string2");
}
答案 1 :(得分:1)
在视图持有者类的onClick中:
Bundle bundle = new Bundle();
bundle.putString("key", "value");
bundle.putString("key", "value");
SecondFragment secondFragment = new SecondFragment();
secondFragment.setArguments(bundle);
AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, secondFragment).addToBackStack(null).commit();
在接收片段中
View view = inflater.inflate(R.layout.fragment_book, container, false);
Bundle bundle = this.getArguments();
if(bundle != null){
Snackbar.make(view, bundle.get("key").toString(), Snackbar.LENGTH_LONG).show();
}
答案 2 :(得分:1)
在视图持有者类的onClick方法中
Bundle bundle = new Bundle();
bundle.putString("key", "value");
bundle.putString("key", "value");
SecondFragment secondFragment = new SecondFragment();
secondFragment.setArguments(bundle);
AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, secondFragment).addToBackStack(null).commit();
之后,您可以开始检索片段中的数据
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
String strtext=getArguments().getString("string1"); //fetching value by key
String strtext1=getArguments().getString("string2");
}