onClick Recyclerview Android

Asked

Viewed 715 times

-1

I’m following this implementation tutorial of Tablayout and Viewpager with Recyclerview:

Tablayout and Viewpager with Recyclerview

I’m using this project to test how to catch the click action in Recyclerview. To complement, I’m following this post that deals with this subject and the same project at Stackoverflow:

Post do Stackoverflow

What I did:

  1. I created the Clicklistener Interface;
  2. I installed the interface in the Recyclerview adapter;
  3. I used the interface function implementing onClickListener on Viewholder; and
  4. I implemented the interface in Fragment.

My interface

public interface ClickListener {
    void itemlistener(int position);
}

Recyclerviewadapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<TextItemViewHolder> {

String[] items;
ClickListener clicklistener;

public RecyclerViewAdapter(String[] items, ClickListener clicklistener) {
    this.items = items;
    this.clicklistener = clicklistener;
}

@Override
public TextItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_list_item, parent, false);
    return new TextItemViewHolder(view);
}

@Override
public void onBindViewHolder(TextItemViewHolder holder, int position) {
    holder.bind(items[position]);
}

@Override
public long getItemId(int position) {
    return position;
}

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

Textitemviewholder

public class TextItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView textView;


public TextItemViewHolder(View itemView) {
    super(itemView);
    textView = (TextView) itemView.findViewById(R.id.list_item);
    itemView.setOnClickListener(this);
}


@Override
public void onClick(View view) {
    clicklistener.itemlistener(getAdapterPosition());
}

public void bind(String text) {
    textView.setText(text);
}

}

Fragment

public class FragmentA extends Fragment implements ClickListener{

RecyclerView recyclerView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment, container, false);
    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    String[] items = getResources().getStringArray(R.array.tab_A);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(items, this);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

@Override
public void itemlistener(int position) {
    Toast.makeText(getContext(), "RecyclerView position is: " + position, Toast.LENGTH_SHORT).show();
}
}

Can’t find the reference

Imagem relacionada à referência

Why is this implementation error occurring to click on Recyclerview?

1 answer

1


Don’t think because you don’t have any Clicklistener type attributes declared in the Textitemviewholder class.

Two possible solutions:

  1. Make the Textitemviewholder class one Inner class recyclerviewadapter.
    Textitemviewholder will use the attribute declared in Recyclerviewadapter.

  2. Declare the attribute in the Textitemviewholder class and pass the Listener when instantiating it.

  • Ramaral , thanks for the vast content made available, helped me a lot. I will go deeper into these contents.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.