滚动Listview时,也会显示取消选择的图像

时间:2012-10-20 12:40:47

标签: android android-listview

我有一个问题,当我向下滚动列表视图时显示的隐藏图像......这个问题是如何解决的?提前致谢... enter image description here 这是我的代码: -     @覆盖             public View getView(final int position,View convertView,ViewGroup parent)             {

            View v = convertView;

            if(v==null)
            {
                LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=li.inflate(R.layout.custom_row,parent,false);

            }

            TextView Title = (TextView) v.findViewById(R.id.custom_text);
            final RelativeLayout content = (RelativeLayout) v.findViewById(R.id.main);
            final ImageView img1 =(ImageView) v.findViewById(R.id.cust_img);

final BinForAll listitem = mList.get(position);


            Title.setText(listitem.getTxt());

            //Desc.setText(listitem.getDesc());
            content.setOnClickListener(new OnClickListener()
            {

                public void onClick(View arg0) 
                {
                    if(img1.getVisibility()==View.VISIBLE && count>=0)
                    {
                        count++;
                        img1.setVisibility(View.GONE);
                        m1.remove(mList.get(position).getTxt());
                        Log.d("remove", mList.get(position).getTxt());
                    }
                    else if(img1.getVisibility()==View.GONE && count>0)
                    {
                        count--;
                        img1.setVisibility(View.VISIBLE);
                        m1.add(mList.get(position).getTxt());
                        Log.d("add", mList.get(position).getTxt());
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "You can not add more than 10 values..", 1).show();
                    }

            }
        });

2 个答案:

答案 0 :(得分:1)

ListView始终重用视图。因此,每次通过Adapter.getView(final int position, View convertView, ViewGroup parent)为ListView准备视图时,都应检查视图是否处于适当的状态。

为了解决您的问题,您可以在final BinForAll listitem = mList.get(position);对象中保持可见性状态(只需将boolean feald添加到BinForAll类)并重写您的getView方法,如下所示:

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

        View v = convertView;

        if(v==null)
        {
            LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=li.inflate(R.layout.custom_row,parent,false);

        }

        TextView Title = (TextView) v.findViewById(R.id.custom_text);
        final RelativeLayout content = (RelativeLayout) v.findViewById(R.id.main);
        final ImageView img1 =(ImageView) v.findViewById(R.id.cust_img);

        final BinForAll listitem = mList.get(position);


        Title.setText(listitem.getTxt());
        // each time set visibility for your image view
        img1.setVisibility(listitem.isVisible() ? View.VISIBLE : View.GONE);

        //Desc.setText(listitem.getDesc());
        content.setOnClickListener(new OnClickListener()
        {

            public void onClick(View arg0) 
            {
                if(img1.getVisibility()==View.VISIBLE && count>=0)
                {
                    count++;
                    img1.setVisibility(View.GONE);
                    // save visibility state
                    listitem.setVisible(false);
                    m1.remove(mList.get(position).getTxt());
                    Log.d("remove", mList.get(position).getTxt());
                }
                else if(img1.getVisibility()==View.GONE && count>0)
                {
                    count--;
                    img1.setVisibility(View.VISIBLE);
                    // save visibility state
                    listitem.setVisible(true);
                    m1.add(mList.get(position).getTxt());
                    Log.d("add", mList.get(position).getTxt());
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "You can not add more than 10 values..", 1).show();
                }

        }
    });

答案 1 :(得分:0)

将此属性添加到listView

    android:scrollingCache="false"
    android:persistentDrawingCache="scrolling"
    android:fastScrollEnabled="true"
    android:cacheColorHint="@android:color/transparent"