单击支架上的 itemView 时无法打开片段

Cannot open fragment when click on itemView on holder

我尝试尽可能多地使用片段而不是 activity,在我的自定义适配器中 PostAdapter 我使用 holder.itemView.setOnClickListener 方法在 DetailsFragment 处传递和打开 DetailsFragment结束,但是当我点击 recyclerView 上的任何项目时,它没有做任何事情

这是 PostAdapter Class

    public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
    private Context context;
    private List<Item> items;

    public PostAdapter(Context context, List<Item> items) {
        this.context = context;
        this.items = items;
    }

    @NonNull
    @Override
    public PostAdapter.PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.post_item, parent, false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        final Item item = items.get(position);
        holder.postTitle.setText(item.getTitle());
        final Document document = Jsoup.parse(item.getContent());
        Elements elements = document.select("img");

//        Log.d("CODE", "Image: "+elements.get(0).attr("src"));
//        Log.d("Text",document.text());
        holder.postDescription.setText(document.text());
        Glide.with(context).load(elements.get(0)
                .attr("src"))
                .into(holder.postImage);

        holder.itemView.setOnClickListener(view -> {
            DetailsFragment detailsFragment = new DetailsFragment();

            Bundle bundle = new Bundle();

            bundle.putString("url", item.getUrl());
            bundle.putString("itemTitle", item.getTitle());
            bundle.putString("title", item.getTitle());
            bundle.putString("content", item.getContent());
            int youtubeThumbnailImagesetVisibility = 0;

            Element element = document.body();

            String youtubeThumbnailImageSrc = "";
            String youTubeLink = "";
            for (Element e : element.getElementsByClass
                    ("YOUTUBE-iframe-video")) {
                youtubeThumbnailImageSrc = e.attr("data-thumbnail-src");
                youTubeLink = e.attr("src");
                Log.e("YouTube thumbnail", youtubeThumbnailImageSrc);
                Log.e("Youtube link", youTubeLink);
            }

            if (youtubeThumbnailImageSrc.isEmpty()) {
                youtubeThumbnailImagesetVisibility = 8;
                bundle.putInt("youtubeThumbnailImageSetVisibility",
                        youtubeThumbnailImagesetVisibility);
            } else {
                bundle.putString("youtubeThumbnailImageSrc", youtubeThumbnailImageSrc);
                bundle.putString("youTubeLink", youTubeLink);
            }

//             String imageSrc = elements.get(0).attr("src");
//             bundle.putString("blogImage",imageSrc);

            detailsFragment.setArguments(bundle);

            FragmentManager fragmentManager = ((Activity)context).getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.nav_view,detailsFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        });

    }

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

    public class PostViewHolder extends RecyclerView.ViewHolder {

        ImageView postImage;
        TextView postTitle;
        TextView postDescription;

        public PostViewHolder(View itemView) {
            super(itemView);
            postImage = (ImageView) itemView.findViewById(R.id.postImage);
            postTitle = (TextView) itemView.findViewById(R.id.postTitle);
            postDescription = (TextView) itemView.findViewById(R.id.postDescription);

        }
    }
}

DetailsActivity 片段

    public class DetailsFragment extends android.app.Fragment {

    private TextView titleTextView, contentTextView;
    //   private ImageView blogImage;
    private PicassoImageGetter imageGetter;
    private ImageView youtubeThumbnailImage;
    private FloatingActionButton fab;
    private ScrollView scrollView;
    private SpinKitView spinKitView;
    private Button visitSite;
    private View view;
    private String url,title,content, youtubeThumbnailImageSrc, youTubeLink,sharingContent;
    private int youtubeThumbnailImageSetVisibility;

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

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_details, container, false);
                spinKitView = (SpinKitView) view.findViewById(R.id.spin_kit);
        scrollView = (ScrollView) view.findViewById(R.id.scrollView);
        titleTextView = (TextView) view.findViewById(R.id.titleTextView);
        contentTextView = (TextView)view.findViewById(R.id.blogContent);
        fab = (FloatingActionButton) view.findViewById(R.id.fab);
        youtubeThumbnailImage = (ImageView) view.findViewById(R.id.youtubeThumbnailImage);
        visitSite = (Button) view.findViewById(R.id.visitSite);

        if(getArguments() != null){
            Bundle bundle = getArguments();
            url = bundle.getString("url");
            title = bundle.getString("itemTitle");
            content = bundle.getString("content");
            youtubeThumbnailImageSrc = bundle.getString("youtubeThumbnailImageSrc");
            youTubeLink = bundle.getString("youTubeLink");
            youtubeThumbnailImageSetVisibility = bundle.getInt("youtubeThumbnailImageSetVisibility");

        }

//        blogImage = (ImageView) findViewById(R.id.blogImage);

        spinKitView.setVisibility(View.VISIBLE);

        titleTextView.setText(title);
        contentTextView.setMovementMethod(LinkMovementMethod.getInstance());


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scrollView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
                //   Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY);
                if (scrollY > 0 && fab.isShown()) {
                    fab.hide();
                } else if (scrollY < 22) {
                    fab.show();

                }
            });
        } else {
            scrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
                int mScrollY = scrollView.getScrollY();
                if (mScrollY > 0 && fab.isShown()) {
                    fab.hide();
                } else if (mScrollY < 22) {
                    fab.show();
                }
            });
        }

        fab.setOnClickListener(view -> {
            sharingContent = "\n" + url;
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(Intent.EXTRA_TEXT,sharingContent);
            sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        });

        //       String imageSrc = getIntent().getStringExtra("blogImage");
        //       Glide.with(getApplicationContext()).load(imageSrc).into(blogImage);

      youtubeThumbnailImage.setVisibility(youtubeThumbnailImageSetVisibility);
        youtubeThumbnailImage.setAdjustViewBounds(true);
        Picasso.get().load(youtubeThumbnailImageSrc).into(youtubeThumbnailImage);

        youtubeThumbnailImage.setOnClickListener(view -> {
            Intent youTube = new Intent
                    (Intent.ACTION_VIEW, Uri.parse(youTubeLink));
            startActivity(youTube);
        });

        imageGetter = new PicassoImageGetter(contentTextView, getActivity());
        Spannable html;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            html = (Spannable) Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY, imageGetter, null);
        } else {
            html = (Spannable) Html.fromHtml(content, imageGetter, null);
        }

        contentTextView.setText(html);
        spinKitView.setVisibility(View.GONE);

        visitSite.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putString("url", url );

            VisitSiteFragment visitSiteFragment = new VisitSiteFragment();
            visitSiteFragment.setArguments(bundle);

            FragmentManager manager = getFragmentManager();
            FragmentTransaction fragmentTransaction = manager.beginTransaction();

            fragmentTransaction.replace(R.id.detailsFragment,visitSiteFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        });
        return view;
    }

}

我尝试使用 android.support.v4.app 并替换此行

FragmentManager fragmentManager = ((Activity)context).getFragmentManager();

有了这个

FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();

如本 中所述,但这也不起作用

onBindViewHolder 中,您必须像这样实现它:

holder.nameImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             ...
            //Toast.makeText(mContext,position+  " pozicioni",Toast.LENGTH_LONG).show();
        }
    });

你必须替换“nameImage”的地方。

已解决

这一行问题的原因

fragmentTransaction.replace(R.id.nav_view,detailsFragment);

我试图替换导航抽屉视图,但我应该放置包含 recyclerView 的视图

fragmentTransaction.replace(R.id.parentLayout,detailsFragment);

在 activity class

上调用片段

片段应附加到活动。在您的 activity 中创建一个接口来实现它,然后在您的适配器中初始化该接口。在 clickListener 中调用该方法,然后您可以在 activity 中通过单击调用的正确方法中执行任何操作。

holder.nameImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

       yourInterface.OnItemclick();

    }
});

确保对其进行初始化。