Я пытаюсь добавить кнопку в держатель карты моего реселлера.
Когда кнопка нажата, пользователь перенаправляется на внешний веб-сайт при нажатии. Я также хочу добавить несколько кнопок к каждому просмотру карты, чтобы каждая кнопка приводила к отдельному веб-сайту при нажатии.Как это сделать?
Мой код:
Класс фрагмента (я использую фрагменты):
public class BooksFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_books, container, false);
mLayoutManager = new LinearLayoutManager(this.getActivity());
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<exampleItemBooks> exampleList = new ArrayList<>();
exampleList.add(new exampleItemBooks(R.drawable.thritysixstrategems, "The Thirty-Six Stratagems", "300 years ago, an unknown author compiled the Stratagems, a strangely seductive meditation on deception.", "-Unknown Author"));
exampleList.add(new exampleItemBooks(R.drawable.medidations, "Meditations", "A book by the great Stoic Roman Emperor Marcus Aurelius. The book was written in Koine Greek, during the Emperor's reign.", "-Marcus Aurelius"));
mRecyclerView = view.findViewById(R.id.recyclerViewBooks);
mRecyclerView.setHasFixedSize(false);
mAdapter = new exampleBooksAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Адаптер:
public class exampleBooksAdapter extends RecyclerView.Adapter<exampleBooksAdapter.ExampleViewHolder> {
private ArrayList<exampleItemBooks> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageViewCards);
mTextView1 = itemView.findViewById(R.id.textViewCard1);
mTextView2 = itemView.findViewById(R.id.textViewCard2);
mTextView3 = itemView.findViewById(R.id.textViewCard3);
}
}
public exampleBooksAdapter(ArrayList<exampleItemBooks> exampleItemBooks) {
mExampleList = exampleItemBooks;
}
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_books, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
exampleItemBooks currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mTextView3.setText(currentItem.getText3());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
Класс ArrayList:
private int mImageResource;
private String mText1;
private String mText2;
private String mText3;
public exampleItemBooks(int imageResource, String text1, String text2, String text3) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
mText3 = text3;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
public String getText3() {
return mText3;
}
}