ListView的滚动问题:在项目选择 [英] ListView Scrolling Issue: on Item Selection

查看:224
本文介绍了ListView的滚动问题:在项目选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表视图与我想显示我的自定义Adapter.Everything工作正常,当我选择列表项并滚动它除了,这是没有选择已经被selected.I的项目并不真正了解什么问题与我的列表视图。

下面是我的类:

  @覆盖
保护无效的onCreate(包savedInstanceState)
{
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.select_contact_layout);

    getActionBar()setHomeButtonEnabled(真)。
    getActionBar()setDisplayHomeAsUpEnabled(真)。
    。getActionBar()的setTitle(选择联系人);

    mArrayAdapter =新CustomAdapter(这一点,getContacts());

    setListAdapter(mArrayAdapter);

    联系preferences = getShared preferences(接触preF,MODE_PRIVATE);

    mListView = getListView();

}



@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长ID)
{
    super.onListItemClick(L,V,位置ID);

    字符串名称= mArrayAdapter.getItem(位置).getName();

    v.setBackgroundColor(Color.parseColor(#88dfdf));

    Toast.makeText(getApplicationContext(),项目学术+位置+和名称:+名称,0).show();

}
 

和我的自定义适配器:

 类CustomAdapter扩展ArrayAdapter<联系和GT;
{
     LayoutInflater layoutInflater;
    私人列表<联系和GT; conctactList;


    公共CustomAdapter(上下文的背景下,列表与LT;联系和GT; mList)
    {
        超(背景下,R.layout.single_contact_layout,mList);
        this.conctactList = mList;
        layoutInflater = LayoutInflater.from(上下文);
    }

        @覆盖
        公众诠释getCount将(){
            // TODO自动生成方法存根
            返回conctactList.size();
        }

        @覆盖
        公开联系方式的getItem(INT位置){
            // TODO自动生成方法存根
            返回conctactList.get(位置);
        }

        @覆盖
        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }

        @覆盖
        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
        {
            最终持有人;
            点阵位图= NULL;
            位图scaleBitmap = NULL;

            如果(convertView == NULL)
            {
                持有人=新的持有人();


                convertView = layoutInflater.inflate(R.layout.single_contact_layout,NULL);
                holder.name =(TextView中)convertView.findViewById(R.id.contact_name);
                holder.number =(TextView中)convertView.findViewById(R.id.contact_number);
                holder.contact_img =(ImageView的)convertView.findViewById(R.id.contact_img);


                convertView.setTag(保持器);
                convertView.setTag(R.id.contact_name,holder.name);
            }

            其他{
                支架=(座)convertView.getTag();
            }

            holder.name.setText(conctactList.get(位置).getName());
            holder.number.setText(conctactList.get(位置).getNumber());
            尝试 {
                位= MediaStore.Images.Media.getBitmap(getContentResolver(),Uri.parse(contactsList.get(位置).getImgUri()));

                scaleBitmap = Bitmap.createScaledBitmap(位图,100,100,真正的);

            }赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(scaleBitmap,100));

            返回convertView;
        }


}

私有静态类持有人{
     TextView的名称;
     TextView的数量;
     ImageView的contact_img;
    }
 

解决方案

v.setBackgroundColor(Color.parseColor(#88dfdf)); 你不应该只做这一点,但也有一个的ArrayList<布尔> selectedList 在其中记住如果该项目被选中。然后,在getView你应该检查的名单,并把相应的颜色。

 如果(selectedList.get(位置))
      convertView.setBackgroundColor(Color.parseColor(#88dfdf));
 其他
      convertView.setBackgroundColor(Color.parseColor(正常颜色));
 

在适配器类初始化列表:

 的ArrayList<布尔> selectedList =新的ArrayList<布尔>();

公共CustomAdapter(上下文的背景下,列表与LT;联系和GT; mList)
{
 .........
 INT NR = -1;
 而(+ NR< mList.size())
    selectedList.add(假);
 }
}
 

也将它添加到getView()函数

 公开查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
    {...............................
        holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(scaleBitmap,100));

        如果(selectedList.get(位置)==真)
        {
          convertView.setBackgroundColor(Color.parseColor(#88dfdf));
        }
        其他
        {
          convertView.setBackgroundColor(Color.background_light);
        }
        返回convertView;
    }
 

还添加以下行到你的onListItemClick()。

 保护无效onListItemClick(ListView的L,视图V,INT位置,长ID)
{
    ..................
    如果(mArrayAdapter.selectedList.get(位置)==真)
    {
      v.setBackgroundColor(Color.background_light));
      mArrayAdapter.selectedList.set(位置,假);
    }
    其他
    {
      v.setBackgroundColor(Color.parseColor(#88dfdf));
      mArrayAdapter.selectedList.set(位置,真正的);
      Toast.makeText(getApplicationContext(),项目学术+位置+和名称:+名称,0).show();
    }

}
 

,也使selectedList变量公众CustomAdapter。

I have Listview with which I'm trying to display my custom Adapter.Everything works fine except when I select list item and scroll it,the items which were not selected are already being selected.I don't really understand what's the issue with my listview.

Here's my class:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.select_contact_layout);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setTitle("Select Contact");

    mArrayAdapter = new CustomAdapter(this,getContacts());

    setListAdapter(mArrayAdapter);

    contactPreferences = getSharedPreferences("contactPref", MODE_PRIVATE);

    mListView = getListView();

}



@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);

    String name = mArrayAdapter.getItem(position).getName();

    v.setBackgroundColor(Color.parseColor("#88dfdf"));

    Toast.makeText(getApplicationContext(), "Items Pos: " + position +"and Name : "+ name, 0).show();

}

and My Custom Adapter:

 class CustomAdapter extends ArrayAdapter<Contacts>
{
     LayoutInflater layoutInflater;
    private List<Contacts> conctactList;


    public CustomAdapter(Context context, List<Contacts> mList)
    {
        super(context, R.layout.single_contact_layout,mList);
        this.conctactList = mList;
        layoutInflater = LayoutInflater.from(context);
    }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return conctactList.size();
        }

        @Override
        public Contacts getItem(int position) {
            // TODO Auto-generated method stub
            return conctactList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            final Holder holder;
            Bitmap bitmap = null;
            Bitmap scaleBitmap = null;

            if(convertView == null)
            {
                holder = new Holder();


                convertView = layoutInflater.inflate(R.layout.single_contact_layout, null);
                holder.name = (TextView) convertView.findViewById(R.id.contact_name);
                holder.number = (TextView) convertView.findViewById(R.id.contact_number);
                holder.contact_img = (ImageView)convertView.findViewById(R.id.contact_img);


                convertView.setTag(holder);
                convertView.setTag(R.id.contact_name, holder.name);
            }

            else{
                holder = (Holder) convertView.getTag();
            }

            holder.name.setText(conctactList.get(position).getName());
            holder.number.setText(conctactList.get(position).getNumber());
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(contactsList.get(position).getImgUri()));

                scaleBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(scaleBitmap, 100));

            return convertView;
        }


}

private static class Holder{
     TextView name;
     TextView number;
     ImageView contact_img;
    }

解决方案

v.setBackgroundColor(Color.parseColor("#88dfdf")); You should not only do that but also have an ArrayList<Boolean> selectedList in which you 'remember' if the item is selected. Then in getView you should 'inspect' that list and put the color accordingly.

 if ( selectedList.get(position) )
      convertView.setBackgroundColor(Color.parseColor("#88dfdf"));
 else      
      convertView.setBackgroundColor(Color.parseColor( normal color ));

Initializing the list in the adapter class:

ArrayList<Boolean> selectedList = new ArrayList<Boolean>();

public CustomAdapter(Context context, List<Contacts> mList)
{
 .........
 int nr = -1;
 while (++nr < mList.size() )
    selectedList.add(false);
 }
}

also add this to getView() function

   public View getView(final int position, View convertView, ViewGroup parent)
    { ...............................
        holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(scaleBitmap, 100));

        if(selectedList.get(position)== true)
        {
          convertView.setBackgroundColor(Color.parseColor("#88dfdf")); 
        }   
        else
        {
          convertView.setBackgroundColor(Color.background_light); 
        }
        return convertView;
    }

also add the following line to your onListItemClick().

protected void onListItemClick(ListView l, View v, int position, long id) 
{
    ..................
    if(mArrayAdapter.selectedList.get(position)==true)
    {
      v.setBackgroundColor(Color.background_light));
      mArrayAdapter.selectedList.set(position,false);
    }
    else
    {
      v.setBackgroundColor(Color.parseColor("#88dfdf"));
      mArrayAdapter.selectedList.set(position,true);
      Toast.makeText(getApplicationContext(), "Items Pos: " + position +"and Name : "+ name, 0).show();
    }

}

and also make selectedList variable to public in CustomAdapter.

这篇关于ListView的滚动问题:在项目选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆