ListView滚动问题:关于项目选择

时间:2014-09-16 10:56:01

标签: android android-listview listselectionlistener

我有Listview,我正在尝试显示我的自定义Adapter.Everything正常工作,除非我选择列表项并滚动它,未选择的项目已被选中。我真的不明白什么是我的列表视图问题。

这是我的班级:

@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();

}

和我的自定义适配器:

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

2 个答案:

答案 0 :(得分:2)

v.setBackgroundColor(Color.parseColor("#88dfdf"));您不仅应该这样做,而且如果项目被选中,您还应该记住ArrayList<Boolean> selectedList。然后在getView中,您应该“检查”该列表并相应地放置颜色。

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

初始化适配器类中的列表:

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

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

还将此添加到getView()函数

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

还将以下行添加到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();
    }

}

并在CustomAdapter中将selectedList变量设为public。

答案 1 :(得分:0)

另一种解决方案,在自定义列表视图中放置一个简单的int

public class CustomListView extends BaseAdapter {
     private int selected = 0;

检查getView中的位置

public View getView(int position, View convertView, ViewGroup parent) {
   if (position == selected)
        selector.setImageResource(R.drawable.selected);
   else
        selector.setImageResource(R.drawable.not_selected);

最后在您的活动或片段的clickListener中,使用setter / getter方法设置位置并通知适配器。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    customListViewAdapter.setSelectedPosition(position);
    customListViewAdapter.notifyDataSetChanged();
}

它对我来说就像一个魅力;)