带有分隔符的listView自定义适配器上的Android图标

时间:2012-01-24 11:13:21

标签: android listview custom-adapter

我想在用户点击一行时在列表视图中添加一个播放图标。

我正在使用自定义适配器(Lazy Load with separator)...我遇到了分隔符与其他行重叠的问题,所以我实现了getViewTypeCount()方法来解决它。

现在当我在列表视图的onItemClickListener上包含一个播放图标时,图标被添加正常,但它与其他行重叠(分隔符除外)...

继承我的代码:

OnItemCLickListener:

    listChannels.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub


            if (oldView != null) {
                oldView.setDrawingCacheEnabled(true);
                ImageView icon = (ImageView) oldView.findViewById(R.id.imageViewPlay);
                icon.setImageBitmap(null);
                icon.setVisibility(ImageView.GONE);
                icon.invalidate();
            }


                oldView = arg1;
                selectedItem = arg2;
                ImageView icon = (ImageView) arg1.findViewById(R.id.imageViewPlay);
                icon.setImageResource(R.drawable.play_small);
                icon.setVisibility(ImageView.VISIBLE);


                Intent intent = new Intent(StationList2.this,
                        ServiceLauncher.class);

                intent.putExtra("objectPassed", feeds.get(arg2));

                startActivity(intent);
            }

    });

我的自定义适配器:

private class MyCustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<BeanChannelList> mData = new ArrayList<BeanChannelList>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public MyCustomAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public void addItem(final BeanChannelList beanChannelList) {
        mData.add(beanChannelList);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final BeanChannelList beanChannelList) {
        mData.add(beanChannelList);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
                : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position).getStrTitle();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
            case TYPE_ITEM:
                convertView = mInflater
                        .inflate(R.layout.listitem_row, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.textview2 = (TextView) convertView
                        .findViewById(R.id.textView2);
                holder.image = (ImageView) convertView
                        .findViewById(R.id.imageView1);

                holder.imagePlay = (ImageView) convertView
                        .findViewById(R.id.imageViewPlay);




                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.item2, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.textview2 = (TextView) convertView
                        .findViewById(R.id.textView2);
                holder.image = (ImageView) convertView
                        .findViewById(R.id.imageView1);

                holder.imagePlay = (ImageView) convertView
                        .findViewById(R.id.imageViewPlay);

                convertView.setClickable(false);
                convertView.setFocusable(false);
                convertView.setFocusableInTouchMode(false);
                convertView.setEnabled(false);

                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        try {



            if (mData.get(position).getStrChannelNo().equals("")) {

                holder.textView.setText(mData.get(position)
                        .getStrChannelNo()
                        + "   "
                        + mData.get(position).getStrTitle());

            }

            else {

                holder.textView.setText(mData.get(position)
                        .getStrChannelNo()
                        + ": "
                        + mData.get(position).getStrTitle());
            }



            if (selectedItem == position) {


                holder.imagePlay.setImageResource(R.drawable.play_small);
                holder.imagePlay.setVisibility(ImageView.VISIBLE);


            }

            holder.textview2.setText(mData.get(position).getStrLocation());

            imageLoader.DisplayImage(mData.get(position).getStrImage()
                    .toString(), activity, holder.image);




        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:-1)

我猜问题出现在ViewHolder中。一旦我也有同样的问题。不要使用View Holder。