如何更改适配器recyclerview中单击的片段?

时间:2018-07-16 06:26:25

标签: android android-fragments android-recyclerview

我想在单击适配器项回收器时更改片段,我已经使用了片段事务,但是片段未更改。

如果您解决了这样的问题,请分享。 谢谢

这是我的代码

 @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {


    Handler handler = new Handler();
    final Runnable run =  new Runnable() {
        @Override
        public void run() {
    String urlThumbnails = list_data.get(position).get("Thumbnails");
    if(urlThumbnails != null){
        Glide.with(context)
                .load(urlThumbnails)
                .into(holder.thumbnailImg);
    }

    holder.title.setText(list_data.get(position).get("Title"));
    holder.itemCount.setText(list_data.get(position).get("ItemCount")+" Videos");


        }
    };
    handler.post(run);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String PlaylistId = list_data.get(position).get("Id");
            switchFragment(PlaylistId);

        }
    });
}

切换方法

private void switchFragment(String id){
    playlistFragment = new PlaylistFragment();
    Bundle bundle = new Bundle();
    bundle.putString("Id",id);
    playlistFragment.setArguments(bundle);
    Log.d("response playlist", id);
    if(context != null){
        FragmentManager fragmentManager = context.getActivity().getFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,playlistFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }


}

1 个答案:

答案 0 :(得分:0)

然后更改:

FragmentManager fragmentManager = context.getSupportFragmentManager();

对于后处理,应在onBackPressed中按照以下条件写出以下条件:

if (!(findFragment() instanceof PlayListFragment){
    replaceHomeFragment(ListFragment());
} else {
    super.onBackPressed();
}
....
....
public void replaceDetailFragment(Fragment fragment) {
    getSupportFragmentManager().beginTransaction().replace(R.id.frm_detail, fragment).commit();
}

public Fragment findFragment() {
    return getSupportFragmentManager().findFragmentById(R.id.fragment_container);
}