列表视图项错误(正在更改多个项的样式)

时间:2018-01-08 08:47:44

标签: java android android-layout listview

我试图在点击时更改listview项目的颜色,但是当我这样做时,它也会改变另一个不在视图中的列表项目的颜色。所以问题在于当前的命令使用视图中的当前可视项目来改变颜色。因为列表有大约20个项目,每个视图可见5个。如果我点击第1个项目,那么滚动时每滚动5个项目就可以改变它的颜色。我想要的只是改变颜色的项目。我将chilcount定为5,但listview项目的位置数最多为20。

lv.setOnItemClickListener(
                    new AdapterView.OnItemClickListener()
                    {
                        @Override
                        public void onItemClick(AdapterView<?> parent, final View view,
                                                int position, long id) {
                            System.out.println("ArrayList Of Values :"+ lv.getItemAtPosition(position) + position + " " + position + "-" + id);
                           /* if(view.getVisibility() == View.VISIBLE){

                                view.setBackgroundColor(0xff39fff7);
                            }*/
                            String values= lv.getItemAtPosition(position).toString();

                            System.out.println(position + " " + id);
                            JSONArray jsonArray;
                            final ArrayList<String> listOfValues = new ArrayList<String>();
                            try {
                                JSONObject jsonObj = new JSONObject(values);
                                String date = jsonObj.getString("date");
                                String slot1 = jsonObj.getString("slot1");
                                listOfValues.add( slot1);
                                listOfValues.add( date);
                            } catch (final JSONException e) {
                                Log.e(TAG, "Json parsing error: " + e.getMessage());

                            }
                            if(listOfValues.get(0) == ""){
                                selectedtime.setText("not available");
                            }else{
                                Integer ki = 0;
                                String alpha = "yes";

                                if(alpha == "yes"){

                                    int color = Color.TRANSPARENT;
                                    Drawable background = getViewByPosition(position,lv).getBackground();
                                    if (background instanceof ColorDrawable)
                                        color = ((ColorDrawable) background).getColor();
                                    if(color == Color.parseColor("#ff39fff7")){
                                        System.out.println(lv.getChildCount()+"position is="+position);

                                     //lv.getChildAt(position).setBackgroundColor(0xffffffff);
getViewByPosition(position,lv).setBackgroundColor(0xffffffff);
                                    }else{


                                        getViewByPosition(position,lv).setBackgroundColor(0xff39fff7);
                                        //lv.getChildAt(position).setBackgroundColor(0xff39fff7);
                                    }
                                }





                            }
                        }
                    }
            );

适配器功能

public View getView(final int position,  View convertView, ViewGroup parent) {

        String s = "";

        final ViewHolder holder;
        if (convertView == null) {

            convertView = layoutInflater.inflate(R.layout.list_date, null);

            final ListView gridView = (ListView) convertView.findViewById(R.id.list_viewsub);
            holder = new ViewHolder();

            holder.text = (TextView)convertView.findViewById(R.id.email);
            holder.text.setId(94171+position);
            //final ViewParent bd = holder.icon.getParent();
           final Context acontext = new TabFragment1().getContext();
 convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
try{
            s = contactList.get(position).toString();
            JSONObject c = contactList.getJSONObject(position);


            String slots;
            String id;
            String date;
            String names;
            String slot1_value;
            slots = c.getString("slot1");
            JSONArray names_json = c.getJSONArray("names");
            holder.text.setText(slots);
}
        catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());


        }

        return convertView;



    }

Truong的解决方案 - 确定它适用于单一选择,但我必须在应用程序中进行多项选择。这又是造成错误

 int color = Color.TRANSPARENT;
        Drawable background = convertView.getBackground();
        if (background instanceof ColorDrawable)
            color = ((ColorDrawable) background).getColor();
        if(positionChangedColor != null){
if(position == positionChangedColor) {
    // color your item here

    + Color.parseColor("#ffffff"));
    if(color == 0 || color == -1) {
        convertView.setBackgroundColor(0xff39fff7);

    }else if(color == Color.parseColor("#39fff7")){
        convertView.setBackgroundColor(0xffffffff);

    }
}else {
    // you have to change color to normal to avoid it is recycled

    if(color == Color.parseColor("#ff39fff7")) {

    }else{
        convertView.setBackgroundColor(0xffffffff);

    }

}

1 个答案:

答案 0 :(得分:0)

首先,我建议您不要在onItemClick中为项目视图着色。而是将位置更新到适配器。我将变量命名为int positionChangedColor;

保存位置(将更改颜色)
  1. onItemClick

    @Override
    public void onItemClick(AdapterView<?> parent, final View view,
                                            int position, long id) {
          // your logic as normal
         // now you want to change color
         yourAdapter.updateChangedColorPostion(position);
         notifyDataSetChanged();
    }
    
  2. 在适配器中创建方法

    private void updateChangedColorPostion(int position) {
           positionChangedColor = position;
    }
    
  3. 并在getView

    public View getView(final int position,  View convertView, ViewGroup parent) {
        // your code as normal
    
       if(position == positionChangedColor) {
          // color your item here
       }else {
          // you have to change color to normal to avoid it is recycled
       }
    
       return convertView;
    }