簡體   English   中英

如何在每個項目的列表視圖中實現簡單的按鈕

[英]how to implement simple like button in listview of each item

在此輸入圖像描述

我的列表視圖項中有一些條目。 在那里,我有一個簡單的“喜歡按鈕”(不像Facebook按鈕)。 你可以看到上面提到的SCREENSHOT; 供參考。 我點擊按鈕的那一刻; 當我再次登錄時,我希望更改類似按鈕的顏色,並且類似按鈕的顏色應該保持不變( 更改類似 )。

此外,所有條目必須使用json,使用cust_id,bus_id,Offer_id填充數據庫; 我非常清楚。

當我再次點擊相同的按鈕(如按鈕)時,其顏色已被更改。 必須將其更改回默認顏色,並且必須從數據庫中刪除數據。

我怎樣才能做到這一點...? 1.如何獲得點擊按鈕的價值。 2.如何將更改的顏色恢復為默認值; 一旦按鈕被重新點擊。

Plz建議我......

這是按鈕代碼

holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clicked) {
                    holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
                } else {
                    holder.b1.setBackgroundResource(R.drawable.like_icon);
                }
                clicked = true;
            }
        });

您需要為按鈕添加一個監聽器,並使用ValueAnimator,您可以更改按鈕顏色,並在再次單擊時將其反轉。

這是實現場景的簡單而最好的方法。 為列表項中的按鈕添加onClick監聽器,如下所示..我已經解釋了每一行..

    // set a default background color to the button
    placeHolder.likeButton.setBackgroundColor(Color.RED);
    placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
        ValueAnimator buttonColorAnim = null; // to hold the button animator

        @Override
        public void onClick(View v) {
            // first time this will be null
            if(buttonColorAnim != null){
                // reverse the color
                buttonColorAnim.reverse();
                // reset for next time click
                buttonColorAnim = null;
                // add your code here to remove from database
            }
            else {
                final Button button = (Button) v;
                // create a color value animator
                buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
                // add a update listener for the animator.
                buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        // set the background color
                        button.setBackgroundColor((Integer) animator.getAnimatedValue());
                    }
                });
                // you can also set a delay before start
                //buttonColorAnim.setStartDelay(2000); // 2 seconds
                // start the animator..
                buttonColorAnim.start();
                // add your code here to add to database
            }
        }
    });

這將更改第一次單擊時的按鈕顏色,然后在下次單擊時將顏色恢復。 您還可以設置延遲以更改顏色。

注意:您必須根據邏輯設置默認按鈕顏色。

            @Override
            public void onClick(View view) {
                if(!check)
                {

                    personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
                    check = true;
                }
                else
                {
                    personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
                    check = false;

                }
            }

您可以使用自定義適配器作為列表視圖(它有自己的layout.xml),您可以在其中設置clicklistener。

你可以改變顏色或你想要的。 實際上我確實有你想要的項目。如果你不能這樣做,我會提供一些鏈接。

試試這個鏈接:

具有多個可單擊按鈕的ListView元素

使用Android中的列表(ListView) - 教程

對此的解決方案實際上比我想象的要容易。 您可以在自定義適配器的getView()方法中添加一個setOnClickListener(),用於您正在使用的按鈕。

試試以下:

  1. button上使用setOnClickListener()

例如。

viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() { 
   @Override public void onClick(View v) {
         // TODO :
         // 1. make webservice call to update like status (Assuming a web service call)
         // 2. Implement a callback for webservice call, to get the status of request.
            if(success) 
             a) change the colour of like btn. and insert the data in Db.
             b) Also maintain a column in db for likestatus(by default set it false).                 
            } 
        }
   );
  1. 假設您在登錄時從數據庫中獲取數據,您可以檢查likestatus並相應地設置按鈕的顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM