在自定义 ListView 适配器中按下项目时突出显示效果

Highlight effect while pressing item in custom ListView adapter

每次点击 Android 中的视图时都会出现系统视觉效果。在 Lollipop 中,这是涟漪效应。当我创建一个 ListView 并将其关联到一个普通的 ArrayAdapter 时,就会出现这种效果。现在我添加了一个自定义的ListView,这个效果就没有了。

现在,我已尝试找出问题所在,并且由于将相同的列表项布局与默认适配器一起使用效果很好,我会说问题出在我的自定义适配器上。

我见过许多与此案例相关的解决方案,它们只是实现了调用一些可绘制对象的连锁反应;这不是我想要做的。涟漪效果显示只是因为我是 运行 Android 5 上的应用程序,现在我想做的是在我的项目被点击时让它们具有默认的系统高亮效果。

以下是我的自定义适配器的(希望)相关部分:

public class CustomCardSetsAdapter extends BaseAdapter {
    List<Card> totalList;
    ArrayList<Boolean> hiddenItems;
    ListView parentLV;
    Integer curPosition = -1;

    public static int selectedRowIndex;

    public CustomCardSetsAdapter(CardSets cardList, ListView parentListView)
    {
        this.parentLV = parentListView;
        assignSetValues(cardList);

        totalList =      cardList.getBlackrockMountain();
        totalList.addAll(cardList.getClassic());
        totalList.addAll(cardList.getCurseofNaxxramas());
        totalList.addAll(cardList.getGoblinsvsGnomes());

        Collections.sort(totalList,
                new Comparator<Card>() {
                    public int compare(Card f1, Card f2) {
                        return f1.toString().compareTo(f2.toString());
                    }
                });

        hiddenItems = new ArrayList<>();

        for (int i = 0; i < totalList.size(); i++) {
            if(!totalList.get(i).getCollectible())
                hiddenItems.add(true);
            else
                hiddenItems.add(false);
        }
    }

    @Override
    public int getCount() {
        return (totalList.size() - getHiddenCount());
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final int index = getRealPosition(position);

        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.card_list_item, parentLV, false);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer prevPosition = curPosition;
                curPosition = position;

                if(prevPosition >= parentLV.getFirstVisiblePosition() &&
                        prevPosition <= parentLV.getLastVisiblePosition())
                {
                    View view = parentLV.getChildAt(prevPosition- parentLV.getFirstVisiblePosition());
                    parentLV.getAdapter().getView(prevPosition,view, parentLV);
                }

                v.setBackgroundColor(Color.WHITE);
            }
        });


        Card curCard = totalList.get(index);

        TextView cardName = (TextView) convertView.findViewById(R.id.cardName);
        cardName.setText(curCard.getName());
        setRarityColor(curCard,cardName);

        TextView manaCost = (TextView) convertView.findViewById(R.id.manaCost);
        manaCost.setText((curCard.getCost()).toString());

        ImageView setIcon = (ImageView) convertView.findViewById(R.id.setIcon);
        setSetIcon(curCard,setIcon);

        if(position == curPosition)
            convertView.setBackgroundColor(Color.WHITE);
        else
            convertView.setBackgroundColor(Color.TRANSPARENT);

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.card_list_item;
    }

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

    @Override
    public boolean isEmpty() {
        return false;
    }

    private int getHiddenCount()
    {
        int count = 0;
        for(int i = 0;i <totalList.size();i++)
            if(hiddenItems.get(i))
                count++;
        return count;
    }

    private int getRealPosition(int position) {
        int hElements = getHiddenCountUpTo(position);
        int diff = 0;
        for(int i=0;i<hElements;i++) {
            diff++;
            if(hiddenItems.get(position+diff))
                i--;
        }
        return (position + diff);
    }

    private int getHiddenCountUpTo(int location) {
        int count = 0;
        for(int i=0;i<=location;i++) {
            if(hiddenItems.get(i))
                count++;
        }
        return count;
    }
}

提前致谢。

在您的 ListView XML 中,添加:

 android:drawSelectorOnTop="true"

我也觉得你用错了适配器...

在您的适配器上使用 ViewHolder 模式:

public class CustomCardSetsAdapter extends BaseAdapter {
    List<Card> totalList;
    ArrayList<Boolean> hiddenItems;
    ListView parentLV;
    Integer curPosition = -1;
    public static int selectedRowIndex;

    private class ViewHolderRow{
        TextView cardName;
        TextView manaCost;
        ImageView setIcon;
    }



   public CustomCardSetsAdapter(CardSets cardList, ListView parentListView)
   {
       this.parentLV = parentListView;
       assignSetValues(cardList);

       totalList =      cardList.getBlackrockMountain();
       totalList.addAll(cardList.getClassic());
       totalList.addAll(cardList.getCurseofNaxxramas());
       totalList.addAll(cardList.getGoblinsvsGnomes());

       Collections.sort(totalList,
            new Comparator<Card>() {
                public int compare(Card f1, Card f2) {
                    return f1.toString().compareTo(f2.toString());
                }
            });

       hiddenItems = new ArrayList<>();

       for (int i = 0; i < totalList.size(); i++) {
           if(!totalList.get(i).getCollectible())
               hiddenItems.add(true);
           else
               hiddenItems.add(false);
       }
    }

    @Override
    public int getCount() {
        return (totalList.size() - getHiddenCount());
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final int index = getRealPosition(position);
         ViewHolderRow theRow;
        if(convertView == null) {
           theRow = new ViewHolderRow();
           LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.card_list_item, parentLV, false);

            // Cache your views
            theRow.cardName = (TextView) convertView.findViewById(R.id.cardName);
            theRow.manaCost = (TextView) convertView.findViewById(R.id.manaCost);
            theRow.setIcon = (ImageView) convertView.findViewById(R.id.setIcon);

            // Set the Tag to the ViewHolderRow
            convertView.setTag(theRow);
        }else{
           // get the Row to re-use
           theRow = (ViewHolderRow) convertView.getTag();
        }

    //... Removed convertView.setOnClickListener  

    Card curCard = totalList.get(index);

    // Set Items
    theRow.cardName.setText(curCard.getName());
    setRarityColor(curCard,theRow.cardName);
    theRow.manaCost.setText((curCard.getCost()).toString());
    setSetIcon(curCard,theRow.setIcon);

       if(position == curPosition){
            convertView.setBackgroundColor(Color.WHITE);
       }else{
            convertView.setBackgroundColor(Color.TRANSPARENT);
       }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.card_list_item;
    }

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

    @Override
    public boolean isEmpty() {
        return false;
    }

    private int getHiddenCount()
    {
        int count = 0;
        for(int i = 0;i <totalList.size();i++)
            if(hiddenItems.get(i))
                count++;
         return count;
    }

    private int getRealPosition(int position) {
        int hElements = getHiddenCountUpTo(position);
        int diff = 0;
        for(int i=0;i<hElements;i++) {
            diff++;
            if(hiddenItems.get(position+diff))
                i--;
        }
        return (position + diff);
    }

    private int getHiddenCountUpTo(int location) {
       int count = 0;
       for(int i=0;i<=location;i++) {
             if(hiddenItems.get(i))
                count++;
             }
           return count;
     }
}

设置一个 onListItemClickListener 而不是在整个 convertView 上使用它...

yourListView.setOnItemClickListener(ListListener);


private final OnItemClickListener ListListener = new OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)     {
          // ... Do something on click       
    }
}