0

I'm having a problem with list-view in android. the problem is when i click on any item, onclick ripple effect animated on last item of screen. don't know why ? in debugging it prints correct position as expected but still ripple effect on last item too.

strange thing if i set background color of clicked item

(view).setBackgroundColor(Color.parseColor("#3C8C3F")); //LINEX

it doesn't show ripple effect on last item,strange? right but it is.

... onBindViewHolder(.......){
.....
.....

    ((ViewHolderBlasterDevice)holder).btn_test
            .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //(view).setBackgroundColor(Color.parseColor("#3C8C3F"));   //LINEX
                        Log.d(TAG, "onClick: "+position);
                        try {
                           //
                                /***
                                 non ui code
                                ***/
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
              }//end of onClickListener

Adapter class for RecyclerView

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

JSONArray devices;
String node;
Device device;
Activity activity;
Map<String,Integer>RemoteStatus;
String remoteID;
Drawable defaultColor  =null;

public XclassAdapter(JSONArray devices, String node, Device device, Activity activity){
    this.devices = devices;
    this.node = node;
    this.device = device;
    this.activity = activity;
    RemoteStatus = new HashMap<>();

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = ((LayoutInflater) (parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))).inflate(R.layout.item_test_existingremote, parent, false);
    return new XclassAdapter.ViewHolderXclass(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    try {
        String name = ((JSONObject)devices.get(position)).optString("name");
        ((ViewHolderXclass)holder).textView.setText(name+position);
        ((JSONObject)devices.get(position)).opt("buttons");
        ((ViewHolderXclass)holder).btn_test.setText("TEST");

        if(RemoteStatus.containsKey(((JSONObject)devices.get(position)).optString("_id")))
        switch (RemoteStatus.get(((JSONObject)devices.get(position)).optString("_id")).intValue())
        {
            case 0:
                ((ViewHolderXclass)holder).btn_test.setBackgroundColor(Color.parseColor("#A62C23"));
                break;
            case 1:
                ((ViewHolderXclass)holder).btn_test.setBackgroundColor(Color.parseColor("#3C8C3F"));
                break;
            default:

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    ((ViewHolderXclass)holder).btn_test.setBackground(defaultColor);
                }
                else {
                    ((ViewHolderXclass)holder).btn_test.setBackgroundColor(Color.parseColor("#FF9800"));
                }


        }
        else{
            if(defaultColor==null)
                defaultColor = ((((ViewHolderXclass)holder).btn_test).getBackground());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                ((ViewHolderXclass)holder).btn_test.setBackground(defaultColor);
            }

        }



        ((ViewHolderXclass)holder).btn_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //(view).setBackgroundColor(Color.parseColor("#3C8C3F"));
                Log.d("HHHHHH", "onClick: "+position);
                try {

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    } catch (JSONException e) {
        e.printStackTrace();
    }
}



public void executeCode( String code, final String id){
   /// code removed  non ui only logic
}


public void xfunction(String id){
   //code reoved no ui noly logic
}


@Override
public int getItemCount() {
    return devices.length();
}

public void setJSONArray(JSONArray jsonArray){
    this.devices = jsonArray;
}


class ViewHolderXclass extends RecyclerView.ViewHolder{

    TextView textView;
    Button btn_test;


    public ViewHolderXclass(View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.textView);
        btn_test = itemView.findViewById(R.id.btn_test);
    }
}

}

Amit Kumar
  • 547
  • 3
  • 10
Meena Pintu
  • 162
  • 1
  • 9
  • Use recycler view or see this https://stackoverflow.com/questions/27080859/listview-item-duplicate-on-scroll-down, follow viewholder pattern. – vbp Jul 03 '18 at 10:13
  • try using holder.getAdapterPosition() instead of position – Brijesh Joshi Jul 03 '18 at 10:13
  • by position also i'm getting correct value but the problem is, onclick ripple effect , except that everything working correctly . it animated on last item too @BrijeshJoshi – Meena Pintu Jul 03 '18 at 10:14
  • Have you applied RippleAnimation programmatically? – Brijesh Joshi Jul 03 '18 at 10:18
  • no, i haven't applied programmatically, it's as a default behavior of click. @BrijeshJoshi – Meena Pintu Jul 03 '18 at 10:20
  • Can you keep your whole adapterClass code in the question – Brijesh Joshi Jul 03 '18 at 10:21
  • the question is when i do ((view).setBackgroundColor(Color.parseColor("#3C8C3F")); //LINEX) this operation it works fine . my logical operations are working as expected, only problem it with ripple effect of onclick. ( i'll try to put my whole class after making some dummy changes ) – Meena Pintu Jul 03 '18 at 10:25
  • refer https://stackoverflow.com/questions/28072817/ripple-effect-only-working-on-first-listview-item – Nidhin Jul 03 '18 at 10:27
  • @BrijeshJoshi , I have added adapter class code . – Meena Pintu Jul 03 '18 at 10:47
  • I thought if you would use Recyclerview then repel effect will automatically apperars...So use some good tutorial of recyclerview .. – Vishwa Pratap Jul 03 '18 at 11:27

1 Answers1

0

Can put your click handler code at top of onBindViewHolder method and try clicking RecyclerView Item

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

((ViewHolderXclass)holder).btn_test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //(view).setBackgroundColor(Color.parseColor("#3C8C3F"));
            Log.d("HHHHHH", "onClick: "+position);
            try {

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

try {
    String name = ((JSONObject)devices.get(position)).optString("name");
    ((ViewHolderXclass)holder).textView.setText(name+position);
    ((JSONObject)devices.get(position)).opt("buttons");
    ((ViewHolderXclass)holder).btn_test.setText("TEST");

.........
.........
.....your code ......
Amit Kumar
  • 547
  • 3
  • 10