Android - 从GridView删除项目后,激活状态仍然有效

时间:2018-03-01 17:36:10

标签: android android-gridview

我有一个包含一些卡片的GridView。当我点击卡片时它会突出显示(出现红色边框)。我有一个按钮,当点击它时,它会删除突出显示的卡片。但是,红色边框仍然在下一张卡片上。

What I want to happen

{{3}}

卡的LinearLayout使用的选择器。

border_image_selector.xml

cin.ignore();   
cin.get();  

CardsAdapter.java

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.peridis.setfinder">
    <item android:state_activated="true" app:state_lighted="true"
       android:drawable="@drawable/border_image_lighted_activated"></item>
    <item app:state_lighted="true" android:drawable="@drawable/border_image_lighted" ></item>
    <item android:state_activated="true" android:drawable="@drawable/border_image_activated" ></item>
    <item android:drawable="@drawable/border_image" ></item>
</selector>

我用于删除突出显示的卡片的主要活动部分。

MainActivity.java

public class CardsAdapter extends ArrayAdapter {

private Context context;
private ArrayList<Card> cards;
public static final int ONE_SHAPE = 0;
public static final int TWO_SHAPES = 1;
public static final int THREE_SHAPES = 2;

public CardsAdapter(@NonNull Context context,int resource, @NonNull ArrayList<Card> cardsArryaList) {
    super(context, resource, cardsArryaList);
    this.context=context;
    cards = cardsArryaList;
}

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

@Override
public int getItemViewType(int position) {
    return cards.get(position).getNumber() - 1;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    Card card = cards.get(position);
    int cardNumber = getItemViewType(position);
    LayoutInflater inflater = LayoutInflater.from(context);
    int cardImageId = cards.get(position).getImageId(context);

    if(convertView == null)
    {
        if(cardNumber == ONE_SHAPE)
        {
            convertView = inflater.inflate(R.layout.card_one_shape_layout, parent, false);
        }
        else if(cardNumber == TWO_SHAPES)
        {
            convertView = inflater.inflate(R.layout.card_two_shapes_layout, parent, false);
        }
        else//cardNumber == THREE_SHAPES
        {
            convertView = inflater.inflate(R.layout.card_three_shapes_layout, parent, false);
        }

        ImageView cardImage1 = (ImageView) convertView.findViewById(R.id.cardImage1);
        ImageView cardImage2 = (ImageView) convertView.findViewById(R.id.cardImage2);
        ImageView cardImage3 = (ImageView) convertView.findViewById(R.id.cardImage3);

        holder = new ViewHolder();
        holder.image1 = cardImage1;
        holder.image2 = cardImage2;
        holder.image3 = cardImage3;

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


    holder.image1.setImageResource(cardImageId);



    if(holder.image2 != null)
    {
        holder.image2.setImageResource(cardImageId);
    }
    if(holder.image3 != null)
    {
        holder.image3.setImageResource(cardImageId);
    }

    return convertView;
}

static class ViewHolder
{
    ImageView image1;
    ImageView image2;
    ImageView image3;
}
}

我尝试在删除后将ActivatedState设置为false但它没有工作。

1 个答案:

答案 0 :(得分:0)

Finally, I found out that I can use clearChoices on my GridView.

public void removeButtonClicked(View v)
{
    for(int i=cardsContainerGridView.getChildCount()-1; i>=0; i--)
    {
        if(cardsContainerGridView.getChildAt(i).isActivated())
        {
            cards.remove(i);
        }
    }
    cardsContainerGridView.clearChoices();

    cardsAdapter.notifyDataSetChanged();
}