Reputation: 61
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener()
{
@Override
public void onClick(View view, int position) {
//Values are passing to activity & to fragment as well
int id = view.getId();
if(id == R.id.carasoul_button) {
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position));
intent.putExtra("from", "mo2lfatActivity");
startActivity(intent);
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
what this code does is when i click on the item in recycle view another activity will be opened, i wanna change that with a button that can be pressed that will open the activity.
I tried the above code and it didn't work. i put the button outside the RecycleView
The button is not in this xml layout.gallery_thumbnail.If i put it here it will be in all the views. The button is in the main xml which has recycle view itself.
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.gallery_thumbnail, parent, false);
return new MyViewHolder(itemView);
}
Upvotes: 0
Views: 1037
Reputation: 297
This is working code. I have tested it. You have to define your button click inside adapter. And it will work like charm. Let me know if you face any issue.
/**
* Created by Alok on 4/18/2017.
*/
public class DealsRecyclerAdapter extends RecyclerView.Adapter<DealsRecyclerAdapter.ViewHolder> {
private Activity activity;
private Context context;
private List<Deal> dealsLists;
private static String listType = "OFFER";
public DealsRecyclerAdapter(){
}
public DealsRecyclerAdapter(Activity a, Context c, List<Deal> d, String listType){
this.context = c;
this.dealsLists = d;
this.activity = a;
this.listType = listType;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public CardView dealListCardView;
public ImageView imgDish;
public TextView txtRestaurantName, txtDishName, txtDishPrice, txtDishAvailabilty;
public Button btnOrderNow, btnBookTable;
public ImageButton btnCallNow;
public ViewHolder(View itemView) {
super(itemView);
dealListCardView = (CardView) itemView.findViewById(R.id.dealListCardView);
imgDish = (ImageView) itemView.findViewById(R.id.imgDish);
txtRestaurantName = (TextView) itemView.findViewById(R.id.txtRestaurantName);
txtDishName = (TextView) itemView.findViewById(R.id.txtDishName);
txtDishPrice = (TextView) itemView.findViewById(R.id.txtDishPrice);
txtDishAvailabilty = (TextView) itemView.findViewById(R.id.txtDishAvailabilty);
btnOrderNow = (Button) itemView.findViewById(R.id.btnOrderNow);
btnBookTable = (Button) itemView.findViewById(R.id.btnBookTable);
btnCallNow = (ImageButton) itemView.findViewById(R.id.btnCallNow);
if(listType == "OFFER"){
txtDishAvailabilty.setVisibility(View.VISIBLE);
}else{
txtDishAvailabilty.setVisibility(View.GONE);
}
}
}
@Override
public DealsRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.special_offer, parent, false);
v.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Deal deals = dealsLists.get(position);
Picasso
.with(context)
.load(DEV_URL_SERVER + deals.getImgpath())
.placeholder(R.drawable.no_image)
.error(R.drawable.no_image)
.into(holder.imgDish);
holder.txtRestaurantName.setText(deals.getRestaurantName());
holder.txtDishName.setText(deals.getName());
holder.txtDishPrice.setText(String.valueOf(deals.getFinalcost()));
if(deals.getSoldout()) {
holder.txtDishAvailabilty.setVisibility(View.GONE);
holder.btnOrderNow.setClickable(false);
holder.btnOrderNow.setEnabled(false);
holder.btnBookTable.setClickable(false);
holder.btnBookTable.setEnabled(false);
}
holder.btnOrderNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((SearchActivity)activity).setShPref(KEY_REST_ID, String.valueOf(deals.getRestaurantId()));
Intent intent = new Intent(activity, RestaurantOverviewActivity.class);
intent.putExtra("FROM_ACTIVITY", "SearchActivity");
intent.putExtra("RESTID", deals.getRestaurantId());
activity.startActivity(intent);
}
});
holder.btnBookTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((SearchActivity)activity).setShPref(KEY_REST_ID, String.valueOf(deals.getRestaurantId()));
Intent intent1 = new Intent(activity, BookTableActivity.class);
intent1.putExtra("FROM_ACTIVITY", "SearchActivity");
intent1.putExtra("RESTID", deals.getRestaurantId());
activity.startActivity(intent1);
}
});
holder.btnCallNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent3 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + deals.getPhone()));
try {
activity.startActivity(intent3);
}catch (Exception e){
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public int getItemCount() {
return dealsLists == null ? 0 : dealsLists.size();
}
public void notify(List<Deal> list) {
if (dealsLists != null) {
dealsLists.clear();
dealsLists.addAll(list);
} else {
dealsLists = list;
}
notifyDataSetChanged();
}
}
Upvotes: 0
Reputation: 7075
If you want to position of your item then just add a tag of that view in your onBindViewHolder
method. Here is the code example:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.yourButton.setTag(position);
}
In your ViewHolder Constructor method, add onClickListener on your button. here is sample:
public MyViewHolder(View view) {
super(view);
Button yourButton = (Button) view.findViewById(R.id.YOUR_BUTTON_ID);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v.findViewById(R.id.YOUR_BUTTON_ID);
Intent intent = new Intent(context, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, ""+button.getTag());
intent.putExtra("from","mo2lfatActivity");
context.startActivity(intent);
}
}
Here, context is the activity context, that can you pass by Adapter Constructor.
Thanks :)
Upvotes: 1
Reputation: 7281
In your ViewHolder
class you can set OnClickListener
for that button and override onClick()
method to open another activity.
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Button btMyButton;
public ViewHolder(View itemView) {
super(itemView);
btMyButton = (Button) itemView.findViewById(R.id.bt_my_button);
btMyButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.bt_my_button){
Intent intent = new Intent(v.getContext(), PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(getAdapterPosition()));
intent.putExtra("from","mo2lfatActivity");
startActivity(intent);
}
}
}
Upvotes: 0
Reputation: 91
First, add implementation to your activity:
public class Main extends ActionBarActivity implements View.OnClickListener {
// do your stuff
}
This:
buttonname.setOnClickListener(this);
means that you want to assign listener for your Button "on this instance" -> this instance represents OnClickListener and for this reason your class have to implement that interface.
It's similar with anonymous listener class (that you can also use):
buttonname.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
// Values are passing to activity & to fragment as well
// Toast.makeText(mainActivityCarasoul.this, "Single Click on position: "+position,
// Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position));
intent.putExtra("from","mo2lfatActivity");
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
});
Upvotes: 0
Reputation: 130
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
itemViewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent Code
}
});
}
This will work..
Upvotes: 0