0

当用户单击一行时,我想在列表视图中包含一个播放图标。

我正在使用自定义适配器(带有分隔符的延迟加载)...我遇到了分隔符与其他行重叠的问题,因此我实现了 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;
    }

}
4

1 回答 1

-1

我想问题出在 ViewHolder 中。曾经我也遇到过同样的问题。不要使用视图支架。

于 2012-01-24T12:22:47.363 回答