setOnClickListener在片段内的我的适配器中不起作用

时间:2019-02-25 09:35:23

标签: android android-recyclerview fragment android-cardview

大家好,我在让我的Cardview在片段中可点击时遇到问题,基本上是recyclerview,如果我们得到一个结果,它将具有cardadapter,如果图像可以点击 这是我的代码,任何帮助将不胜感激

XML:

 <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"

    card_view:cardCornerRadius="4dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_products_chosen"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:clickable="true"
        android:focusable="true"/>
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_marginTop="70dp"
        android:id="@+id/progress_product"/>

 <TextView
    android:id="@+id/nameproducts"
    android:layout_width="193dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image_products"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="2dp"
    android:layout_marginRight="20dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:id="@+id/imageurlproducts"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:id="@+id/priceitem"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:id="@+id/iditem"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

片段代码

searchProductFromCategory fragment = new searchProductFromCategory();
    FragmentManager FM = getSupportFragmentManager();
    FragmentTransaction FT = FM.beginTransaction();


    FT.add(R.id.listproduct, fragment);
    FT.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    FT.commit();

卡片适配器:

public class CardProductFromCategory extends RecyclerView.Adapter <CardProductFromCategory.ViewHolder>{

    private ImageLoader imageLoader;
    private Context context;
    private Context context1;
    public ProgressBar progressbar1;

    //List of promoFeeds
    List<Products> PRODUCTS;

    public CardProductFromCategory(List<Products> product, Context context) {
        this.PRODUCTS = product;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_products, parent, false);
        ViewHolder viewHolder = new CardProductFromCategory.ViewHolder(v);

        return viewHolder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Products feed = PRODUCTS.get(position);
        Picasso.with(context).load(feed.getImageUrl()).into(holder.imageView);

        holder.textViewItemName.setText(feed.getItemName());
        holder.iditemtextview.setText(feed.getIDitem());
        holder.priceitemtextview.setText(feed.getPrice());
        holder.imageurl1.setText(feed.getImageUrl());
        progressbar1.setVisibility(View.GONE);



    }

    @Override
    public int getItemCount() {
        return  PRODUCTS.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder  {
        public ImageView imageView;
        public TextView imageurl1, textViewItemName, iditemtextview, priceitemtextview;


        public ViewHolder(View itemView) {
            super(itemView);



            this.imageView = (ImageView) itemView.findViewById(R.id.image_products);

            textViewItemName = (TextView) itemView.findViewById(R.id.nameproducts);
            imageurl1 = (TextView) itemView.findViewById(R.id.imageurlproducts);
            iditemtextview = (TextView) itemView.findViewById(R.id.iditem);
            priceitemtextview = (TextView) itemView.findViewById(R.id.priceitem);
            progressbar1 = (ProgressBar) itemView.findViewById(R.id.progress_product);
            //imageurl1 = (TextView) itemView.findViewById(R.id.imageurl);



            imageView.setOnClickListener(new  android.widget.AdapterView.OnClickListener(){
                @Override
                public void onClick(View v) {

                   //code



                }
            });
        }
    }

}

虽然调试似乎无法访问setOnClickListener 伙计们在等你的答案:D

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);


    }
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_search_product_from_category, container, false);
    try {
        connectedUser = new getUserID().execute(mParam2).get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    recyclerView = (RecyclerView) view.findViewById(R.id.listcategory);

    getproduct(mParam1);
    CategoryFeed = new ArrayList<>();




    recyclerView.setHasFixedSize(true);
    GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
    recyclerView.setAdapter(adapter1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(glm);


}

private String getproduct(final String product) {

    //progressbar1 = (ProgressBar) findViewById(R.id.progress_product);

    //Displaying Progressbar
    //progressbar1.setVisibility(View.VISIBLE);
    // setProgressBarIndeterminateVisibility(true);

    class getProducts extends AsyncTask<String, Void, String> {
        // ProgressDialog loading;

        @Override
        protected void onPostExecute(String s) {
            //  progressbar1.setVisibility(View.GONE);
            super.onPostExecute(s);
            try {
                JSONObject JSONOBJ = new JSONObject(s);
                CategoryFeedJson = JSONOBJ.getJSONArray("Products");
                for (int i = 0; i < CategoryFeedJson.length(); i++) {
                    Products feed = new Products();
                    JSONObject json = CategoryFeedJson.getJSONObject(i);
                    feed.setItemtName(json.getString("ItemName"));
                    feed.setImageUrl(json.getString("image"));
                    feed.setPrice(json.getString("price"));
                    feed.setIDitem(json.getString("IDItems"));
                    CategoryFeed.add(feed);
                }
                context = getActivity();
                adapter1 = new CardProductFromCategory(CategoryFeed, context);
                recyclerView.setAdapter(adapter1);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            // loading.dismiss();
        }

        @Override
        protected String doInBackground(String... params) {
            HashMap<String, String> data = new HashMap<>();
            data.put("products", mParam1);
            RegisterUserClass ruc = new RegisterUserClass();
            String result;
            result = ruc.sendPostRequest(Config.DATA_PRODUCTS, data);

            return result;
        }
    }
    getProducts uns = new getProducts();
    uns.execute(product);

    return product;
}



public class CardProductFromCategory extends RecyclerView.Adapter <CardProductFromCategory.ViewHolder>{

    private ImageLoader imageLoader;
    private Context mcontext;
    public ProgressBar progressbar1;

    //List of promoFeeds
    List<Products> PRODUCTS;

    public CardProductFromCategory(List<Products> product, Context context) {
        this.PRODUCTS = product;
        this.mcontext = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_products, parent, false);
        ViewHolder viewHolder = new CardProductFromCategory.ViewHolder(v);

        return viewHolder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Products feed = PRODUCTS.get(position);
        Picasso.with(context).load(feed.getImageUrl()).into(holder.imageView);

        holder.textViewItemName.setText(feed.getItemName());
        holder.iditemtextview.setText(feed.getIDitem());
        holder.priceitemtextview.setText(feed.getPrice());
        holder.imageurl1.setText(feed.getImageUrl());
        progressbar1.setVisibility(View.GONE);



    }

    @Override
    public int getItemCount() {
        return  PRODUCTS.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView imageurl1, textViewItemName, iditemtextview, priceitemtextview;
        public LinearLayout rl;


        public ViewHolder(View itemView) {
            super(itemView);
            rl = (LinearLayout)itemView.findViewById(R.id.rl_product);

            this.imageView = (ImageView) itemView.findViewById(R.id.image_products);
            textViewItemName = (TextView) itemView.findViewById(R.id.nameproducts);
            imageurl1 = (TextView) itemView.findViewById(R.id.imageurlproducts);
            iditemtextview = (TextView) itemView.findViewById(R.id.iditem);
            priceitemtextview = (TextView) itemView.findViewById(R.id.priceitem);
            progressbar1 = (ProgressBar) itemView.findViewById(R.id.progress_product);
            //imageurl1 = (TextView) itemView.findViewById(R.id.imageurl);
            //imageView.setOnClickListener(this);
            imageView.setOnClickListener(new  View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    String a,b,c,d;
                    c =imageurl1.getText().toString();
                    b = textViewItemName.getText().toString();
                    d =priceitemtextview.getText().toString();
                    a = iditemtextview.getText().toString();

                    uploadProduct (a,b,c,d);
                }
            });
        }
    }

}

5 个答案:

答案 0 :(得分:0)

已编辑:

在XML文件中为<ImageView>

设置属性
android:clickable="true"
android:focusable="true"

它将起作用。


我认为您在实现Listener时遇到问题。

您可以更改

View.OnClickListener

代替

android.widget.AdapterView.OnClickListener

编辑:

使用这一个:

imageView.setOnClickListener(new  View.OnClickListener(){
    @Override
    public void onClick(View v) {
        //code
    }
 });

代替这个:

imageView.setOnClickListener(new  android.widget.AdapterView.OnClickListener(){
    @Override
    public void onClick(View v) {
        //code
    }
 });

希望它对您有用。

谢谢。

答案 1 :(得分:0)

您可以使用此代码

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

      ViewHolder(View itemView) {
       super(itemView);
       imageView.setOnClickListener(this);
       }

        @Override
        public void onClick(View v) {
            int pos = getAdapterPosition();

            switch (v.getId()) {
                case R.id.imageView:
                    break;
            }
        }
    }

pos-单击的行的位置

答案 2 :(得分:0)

setOnClickListener中设置onBindViewHolder

赞:

 holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code here
            }
        });

答案 3 :(得分:0)

您似乎在ViewHolder中获取了错误的视图引用。如果给定的xml代码是针对item_products xml的,则像这样纠正它们。

        this.imageView = (ImageView) itemView.findViewById(R.id.image_products_list);

        textViewItemName = (TextView) itemView.findViewById(R.id.nameproducts_list);
        imageurl1 = (TextView) itemView.findViewById(R.id.imageurlproducts);
        iditemtextview = (TextView) itemView.findViewById(R.id.iditem);
        priceitemtextview = (TextView) itemView.findViewById(R.id.price_list);
        progressbar1 = (ProgressBar) itemView.findViewById(R.id.progress_product);

答案 4 :(得分:0)

在Viewholder中尝试以下代码:

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //TODO...
            }
        });