Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Fragments Introducing Fragments Handling Clicks

alternative OnClick in RecyclerView, please give your argumentation

instead of writing lots of line for onclick callback to the activity, in the fragment, in the adapter,

i think using onClick for the holder is much way better, please give your comment.

  Fragment _fragment;

  @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((CViewHolderRecipe) holder).onBind(position);
        ((CViewHolderRecipe) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                //  1st method, direct access to the holder
                // ((CViewHolderRecipe) holder)._textRecipe.setText("hello");

                //2nd method using the View v
                //TextView textRecipe = (TextView) v.findViewById(R.id.itemText);
                //textRecipe.setText("hello");


                //for fragment
                _fragment.getFragmentManager().beginTransaction().replace(R.id.Holder, new FragmentCustom()).commit()
            }
        });
    }
rajan lagah
rajan lagah
2,539 Points

thanks sir!!! my on click was not working and now use this way and finally it display toast on click

thanku :-)