1

我在我的代码中遇到了一些奇怪的行为,您将在下面找到。我的每一行都有一个按钮,ListView如果单击它,我想将该行变灰并隐藏该按钮。为了使更改永久化,我在自定义中设置了一个属性,ViewHolder因此更改是持久的。

但是,单击项目 1 上的按钮也会将更改应用到项目 6。在其他项目上,它不会影响其他行或影响超过一行。我怀疑这与我声明一些final变量有关,但我不能确定。我尝试实施不同的解决方案(减少最终变量的数量,使用不同的变量等),但似乎没有任何效果。

我需要一些帮助。谢谢!

public class FruitLoopsAdapter extends BaseAdapter
{
    private ArrayList<FruitLoopsNode> provider;
    private Activity parent;
    /**
     * Constructor.
     * @param parent 
     *      The parent activity of the fragment using the FruitLoop. Needed as the adapter will
     *      be used in a fragment which only its parents has access to the data providers. 
     */
    public FruitLoopsAdapter(Activity parent)
    {
        this.parent = parent;
        provider = ((Box)parent).getFruitLoops();
    }

    /**
     * Method returning the size of the underlying data provider.
     * @return
     *      The size of the data provider.
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount()
    {
            return provider.size();
    }

    /**{@inheritDoc}*/
    @Override
    public Object getItem(int position)
    {
            return provider.get(position);
    }

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

    /**
     * Method building the composite custom view and returning it as one view.
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parentViewGroup)
    {       
            FruitLoopsViewHolder viewHolder;
            final FruitLoopsNode FruitLoopsNode = provider.get(position);

            if (convertView == null)
            {
                    convertView = LayoutInflater.from(parentViewGroup.getContext()).inflate(R.layout.list_FruitLoops, null);

                    viewHolder = new FruitLoopsViewHolder(
                                    (TextView) convertView.findViewById(R.id.time),
                                    (Button) convertView.findViewById(R.id.consume),
                                    (ImageView) convertView.findViewById(R.id.thumbnail),
                                    (TextView) convertView.findViewById(R.id.content_title),
                                    (TextView) convertView.findViewById(R.id.content_brief),
                                    (RatingBar) convertView.findViewById(R.id.ratingBar));
                    convertView.setTag(viewHolder);
            } else {
                    viewHolder = (FruitLoopsViewHolder) convertView.getTag();
                    determinePresentationMode(convertView, viewHolder);
            }

            /** [Omitted some code setting many of the fields above to] */

            final Button button = viewHolder.getButton();
            final View finalConvertView = convertView;
            button.setOnClickListener(new OnClickListener()
            {
                    public void onClick(View view)
                    {
                            button.setEnabled(false);
                            button.setVisibility(View.INVISIBLE);
                            finalConvertView.setAlpha(0.5f);
                            finalConvertView.setBackgroundColor(Color.GRAY);
                    }
            });

            /**
             * As using a final viewHolder to set the value inside the onClick() method of the 
             * button will cause the images not to update anymore, investigating the alpha value
             * of the convertView seemed the only easy way to get there. Also, as it is a float, 
             * I want to leave a bit of extra padding in the test.
             */
            if(finalConvertView.getAlpha() < 0.9)
            {
                    viewHolder.setConsumed(true);
            }

    return convertView;
    }

    private void determinePresentationMode(View convertView, FruitLoopsViewHolder viewHolder)
    {
            if(viewHolder.wasConsumed())
            {
                    convertView.setAlpha(0.5f);
                    convertView.setBackgroundColor(Color.GRAY);
            }
    }
}
4

2 回答 2

2

wasConsumed()如果为假,我相信您需要将 alpha 重置为初始状态。

就像是:

private void determinePresentationMode(View convertView, FruitLoopsViewHolder viewHolder)
    {
            if(viewHolder.wasConsumed())
            {
                    convertView.setAlpha(0.5f);
                    convertView.setBackgroundColor(Color.GRAY);
            } else {
                    convertView.setAlpha(1f); // A value higher of 0.9
                    convertView.setBackgroundColor(Color.WHITE); // or whatever color
            }
    }

您的问题是您正在重用相同的视图,但您将拥有相同的 alpha。我会保存状态信息,ViewHolder而不是根据convertView#alpha

于 2013-06-21T08:19:38.523 回答
0

删除 if (convertView == null)条件,然后在 getView() 中尝试

@Override
    public View getView(int position, View convertView, ViewGroup parentViewGroup)
    {       
            FruitLoopsViewHolder viewHolder;
            final FruitLoopsNode FruitLoopsNode = provider.get(position);


                convertView = LayoutInflater.from(parentViewGroup.getContext()).inflate(R.layout.list_FruitLoops, null);

                viewHolder = new FruitLoopsViewHolder(
                                (TextView) convertView.findViewById(R.id.time),
                                (Button) convertView.findViewById(R.id.consume),
                                (ImageView) convertView.findViewById(R.id.thumbnail),
                                (TextView) convertView.findViewById(R.id.content_title),
                                (TextView) convertView.findViewById(R.id.content_brief),
                                (RatingBar) convertView.findViewById(R.id.ratingBar));
                convertView.setTag(viewHolder);


        /** [Omitted some code setting many of the fields above to] */

        final Button button = viewHolder.getButton();
        final View finalConvertView = convertView;
        button.setOnClickListener(new OnClickListener()
        {
                public void onClick(View view)
                {
                        button.setEnabled(false);
                        button.setVisibility(View.INVISIBLE);
                        finalConvertView.setAlpha(0.5f);
                        finalConvertView.setBackgroundColor(Color.GRAY);
                }
        });

        /**
         * As using a final viewHolder to set the value inside the onClick() method of the 
         * button will cause the images not to update anymore, investigating the alpha value
         * of the convertView seemed the only easy way to get there. Also, as it is a float, 
         * I want to leave a bit of extra padding in the test.
         */
        if(finalConvertView.getAlpha() < 0.9)
        {
                viewHolder.setConsumed(true);
        }

return convertView;
}
于 2013-06-21T08:11:03.113 回答