我正在使用产品的回收商视图。我想在回收商项目点击上显示产品描述。
我还在recyclerview行上放了一个按钮和复选框(收藏夹)。
现在我需要获得所有点击事件,这意味着我想要recyclelerview项目点击和子控件点击。
我的布局如下:
RecyclerView Touch事件
productRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
final int action = e.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
return false;
} else if (actionMasked == MotionEvent.ACTION_UP) {
HomeActivity.productclicked = (productDatas.get
(2));
HomeActivity.loadView(new SingleProductFragment(), "SingleProduct");
return false; //intercepted by the viewgroup and passed down to child
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
在适配器中添加到购物车按钮单击
viewHolder.cart_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UserCart.addProductToCart(Integer.parseInt(view.getTag().toString()));
HomeActivity.increaseCartCount();
}
});
如果我没有在recyclerview上实现touchlistener,那么每个按钮都会起作用,否则它就无法工作。
基本上我想知道如何实施点击recyclelerview项目及其子项
有关我还在RecyclerView上使用Scroll Listener的信息。但我认为这对单击事件没有影响
我的适配器类
package net.techdesire.swarnasikha.adapter_package;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.common.soloader.SoLoaderShim;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import net.techdesire.swarnasikha.R;
import net.techdesire.swarnasikha.activity_package.HomeActivity;
import net.techdesire.swarnasikha.data_package.ProductData;
import net.techdesire.swarnasikha.data_package.UserCart;
import net.techdesire.swarnasikha.fragments_package.SingleProductFragment;
import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by Naitik Kundalia on 2/28/2017.
*/
public class ProductDataAdapter extends RecyclerView.Adapter<ProductDataAdapter
.ProductViewHolder> {
private ArrayList<ProductData> productDataArrayList;
private Context context;
public ProductDataAdapter(Context context, ArrayList<ProductData> countries) {
this.context=context;
this.productDataArrayList = countries;
}
@Override
public ProductDataAdapter.ProductViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product, viewGroup,
false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder viewHolder,final int position) {
try {
ProductData currentProduct = productDataArrayList.get(position);
viewHolder.product_name.setText(currentProduct.getName());
viewHolder.product_image.setImageURI(currentProduct.getImages().get(0).getSrc());
viewHolder.cart_action.setTag(currentProduct.getId());
viewHolder.cart_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UserCart.addProductToCart(Integer.parseInt(view.getTag().toString()));
HomeActivity.increaseCartCount();
}
});
viewHolder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();
HomeActivity.productclicked=productDataArrayList.get(position);
HomeActivity.loadView(new SingleProductFragment(),"SingleProduct");
}
});
}
catch(Exception ex)
{
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public int getItemCount() {
return productDataArrayList.size();
}
public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public View container;
public TextView addedtocart;
public TextView product_name;
SimpleDraweeView product_image;
Button cart_action;
public ProductViewHolder(View view) {
super(view);
container=view;
product_name = (TextView)view.findViewById(R.id.row_product_name_textview);
addedtocart = (TextView)view.findViewById(R.id.row_added_to_cart_textview);
product_image=(SimpleDraweeView) view.findViewById(R.id.row_product_imageview);
cart_action=(Button)view.findViewById(R.id.row_cart_action_button);
//view.setOnClickListener(this);
}
public void onClick(View v)
{
Toast.makeText(context, "View Clicked", Toast.LENGTH_SHORT).show();
}
}
}