首页 文章

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

提问于
浏览
-1

我试图在点击时更改listview项目的颜色,但是当我这样做时,它正在改变另一个列表项目的颜色,这也是不在视图中 . 所以问题是我正在使用的当前命令使用当前可查看的项目来改变颜色 . 因为列表有大约20个项目,每个视图可见5个 . 如果我点击第1项,那么滚动时每滚动第5项就会看到它的颜色已经改变了 . 我想要的只是改变颜色的项目 . 我的小孩数为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

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

    • 在你的 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();
    }
    
    • 在适配器创建方法中
    private void updateChangedColorPostion(int position) {
           positionChangedColor = position;
    }
    
    • 并在 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;
    }
    

相关问题