android 值将适配器传递给片段

faz*_* tm 1 android android-fragments android-recyclerview

我正在开发一个用于显示图像和文本的应用程序。单击该项目时,它会转到另一个片段。列表显示是正确的,但是当我单击该项目时,它不会碎片化。我正在使用回收器适配器来列出项目。代码如下所示。

public class MyRecyclerAdapter extends RecyclerView.Adapter < MyRecyclerAdapter.MyViewHolder > {

 String categoryId;

 private List < NewsFeeds > feedsList;
 private Context context;
 private LayoutInflater inflater;

 public MyRecyclerAdapter(Context context, List < NewsFeeds > feedsList) {

  this.context = context;
  this.feedsList = feedsList;
  inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

  View rootView = inflater.inflate(R.layout.singleitem_recyclerview_categories, parent, false);
  return new MyViewHolder(rootView);
 }

 @Override
 public void onBindViewHolder(MyViewHolder holder, int position) {
  NewsFeeds feeds = feedsList.get(position);
  //Pass the values of feeds object to Views
  holder.title.setText(feeds.getName());
  //holder.categoryId.setText(feeds.getCategory_id());
  categoryId = feeds.getCategory_id();
  Log.d("LOGTAG", "id : " + categoryId);
  holder.imageview.setImageUrl(feeds.getImgURL(), NetworkController.getInstance(context).getImageLoader());

  Log.d("LOGTAG", "feeds.getFeedName():" + feeds.getName());
 }

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

 public class MyViewHolder extends RecyclerView.ViewHolder {

  private TextView title;
  private NetworkImageView imageview;
  private CardView cardView;


  public MyViewHolder(View itemView) {
   super(itemView);
   title = (TextView) itemView.findViewById(R.id.title_view);
   //categoryId = (TextView) itemView.findViewById(R.id.content_view);
   // Volley's NetworkImageView which will load Image from URL
   imageview = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
   cardView = (CardView) itemView.findViewById(R.id.card_view);

   cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     //Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();



     // I want to send values to SubCategoryFragment and start SubCategoryFragment
     Bundle args = new Bundle();
     args.putString("category_id", categoryId);
     //set Fragmentclass Arguments
     SubCategoryFragment fragobj = new SubCategoryFragment();
     fragobj.setArguments(args);

     Log.d("LOGTAG", categoryId);
     Log.d("LOGTAG", "clicked");



     //newInstance(categoryId);



    }
   });




  }


 }


}
Run Code Online (Sandbox Code Playgroud)

我想将值发送到 SubCategoryFragment 并启动 SubCategoryFragment。

我的 SubCategoryFragment 代码

public class SubCategoryFragment extends Fragment {

 public SubCategoryFragment() {
  // Required empty public constructor
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);


 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_sub_category, container, false);
  //Bundle bundle = this.getArguments();


  Bundle args = getArguments();
  //String categoryId = args.getString("index");

  String categoryId = getArguments().getString("category_id");
  //String categoryId = getArguments().getString("category_id");
  TextView textView = (TextView) rootView.findViewById(R.id.label);
  textView.setText(categoryId);



  // Inflate the layout for this fragment
  return rootView;
 }

 @Override
 public void onAttach(Activity activity) {
  super.onAttach(activity);
 }

 @Override
 public void onDetach() {
  super.onDetach();
 }
}
Run Code Online (Sandbox Code Playgroud)

请帮我

Mav*_*eňツ 5

从适配器发送数据的目的是:

Fragment fragment = new tasks();
FragmentManager fragmentManager = context.getSupportFragmentManager(); // this is basically context of the class
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("name", "From Adapter"); //key and value
//set Fragmentclass Arguments
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Run Code Online (Sandbox Code Playgroud)

并在 Fragment onCreateView 方法中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    String strtext=getArguments().getString("name"); //fetching value by key 
    return inflater.inflate(R.layout.fragment, container, false);
}
Run Code Online (Sandbox Code Playgroud)