使用按钮

时间:2017-04-18 05:21:17

标签: android android-recyclerview

   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) {

            }
        }));

这段代码的作用是当我点击循环视图中的项目时,将打开另一个活动,我想用一个可以按下的按钮来改变它,这将打开活动。

我尝试了上面的代码但它没有用。我把按钮放在RecycleView

之外

该按钮不在此xml layout.gallery_thumbnail中。如果我把它放在这里,它将在所有视图中。该按钮位于主xml中,它具有循环视图本身。

@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);
    }

5 个答案:

答案 0 :(得分:1)

如果您想要定位商品,只需在onBindViewHolder方法中添加该视图的标记即可。这是代码示例:

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
     holder.yourButton.setTag(position);
}

在ViewHolder构造函数方法中,在按钮上添加onClickListener。 这是样本:

    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);
        }
    }

这里,context是活动上下文,你可以通过Adapter Constructor传递。

谢谢:)

答案 1 :(得分:0)

ViewHolder课程中,您可以为该按钮设置OnClickListener并覆盖onClick()方法以打开其他活动。

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);               
        }
    }
 }

答案 2 :(得分:0)

@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
    itemViewHolder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Intent Code
        }
    });
}

这将有效..

答案 3 :(得分:0)

这是有效的代码。我测试了它。您必须在适配器内定义按钮单击。它会像魅力一样工作。如果您遇到任何问题,请告诉我。

/**
 * 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();
    }

}

答案 4 :(得分:0)

首先,在您的活动中添加实施:

public class Main extends ActionBarActivity implements View.OnClickListener {
   // do your stuff
}

此:

buttonname.setOnClickListener(this);

意味着您要为此实例&#34;分配Button&#34;的监听器。 - &GT;此实例表示OnClickListener,因此您的类必须实现该接口。

与匿名监听器类(您也可以使用)类似:

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) {

        }
    }));
   }
});