android listview对最后一项的点击效果

时间:2018-07-03 10:07:49

标签: android listview

我在android中的列表视图有问题。 问题是当我单击任何项​​目时,onclick波纹效果在屏幕的最后一个项目上动画化。不知道为什么吗?在调试时,它会按预期打印正确的位置,但对最后一项仍然会产生连锁反应。

奇怪的东西 如果设置点击项的背景颜色

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

它对最后一项没有显示涟漪效应吗?是的,但是是。

... 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

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

}

1 个答案:

答案 0 :(得分:0)

可以将点击处理程序代码放在onBindViewHolder方法的顶部,然后尝试单击RecyclerView项目

@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 ......