Android:使用ViewSwitcher过滤GridView

时间:2014-10-05 01:03:06

标签: android gridview viewswitcher

我遇到了一个奇怪的问题。我的GridView项目底栏可通过ViewSwitcher切换。 GridViewAdapter实施FilterableFilterViewSwitcher都可以正常使用。但是,当我在使用GridView后尝试过滤掉ViewSwitcher个项目时,似乎View卡住了执行切换的项目。

为了更好地强调这里的a link带有视频。正如您在切换底栏后看到的那样,ShopB的View显示不正确。奇怪的是,填充ArrayList的{​​{1}}根据调试器具有正确的数据。此外,按钮点击等操作也是正确的。只有Adapter是错误的。我真的对此感到不满,并会感谢任何建议。以下是代码的重要部分。

getView(...)

View

过滤

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ShopGridHolder holder;
    ShopGridItem temp = filteredShops.get(position);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
                parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.shop_grid_item, parent, false);

        holder = new ShopGridHolder(convertView);
        holder.setOnClickListener(clickListener);
        holder.setOnSeekBarChangeListener(skListener);

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

    holder.setImgLogo(temp.getImage());
    holder.setTextName(temp.getName());
    holder.setChkbxFavorite(temp.isFavored());
    holder.getSeekbarPrice().setMax(temp.getMaxPrice() - temp.getMinPrice());
    holder.getSeekbarPrice().setProgress(temp.getPrice() - temp.getMinPrice());
    holder.setTextPrice(String.valueOf(temp.getPrice()) + " " +
            parent.getResources().getString(R.string.currency));

    holder.getSeekbarPrice().setTag(R.id.seekbar_price, position);
    holder.getSeekbarPrice().setTag(R.id.text_price, holder.getTextPrice());
    holder.getChkbxFavorite().setTag(position);
    holder.getBtnAddToCart().setTag(position);
    holder.getBtnRedirect().setTag(position);
    holder.getBtnShowOnMap().setTag(position);

    Animation animation = AnimationUtils.loadAnimation(parent.getContext(), R.anim.instant);
    holder.getViewSwitcher().setInAnimation(animation);
    holder.getViewSwitcher().setOutAnimation(animation);
    holder.getViewSwitcher().setDisplayedChild(temp.isOnMainView() ? 0 : 1);

    return convertView;
}

onTextChanged(...) - 应用过滤器

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint == null) {
                    results.values = shops;
                    results.count = shops.size();
            } else {
                ArrayList<ShopGridItem> filteredTemp = new ArrayList<ShopGridItem>();
                String name, tags;
                currentConstraint = constraint.toString().toLowerCase();
                String[] constraintWords = currentConstraint.toString().split("\\s+");
                for (int i = 0; i < constraintWords.length; ++i) {
                    constraintWords[i] = constraintWords[i].trim();
                }

                for (ShopGridItem shop : shops) {
                    name = shop.getName().toLowerCase();
                    tags = shop.getTags();
                    boolean isVisible = true;

                    for (String word : constraintWords) {
                        if (name.contains(word) || tags.contains(word)) {
                            isVisible = true;
                        } else {
                            isVisible = false;
                            break;
                        }
                    }

                    if (isVisible) {
                        if (showFavorites) {
                            if (shop.isFavored()) {
                                filteredTemp.add(shop);
                            }
                        } else {
                            filteredTemp.add(shop);
                        }
                    }
                }

                results.values = filteredTemp;
                results.count = filteredTemp.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredShops = (ArrayList<ShopGridItem>) results.values;
            notifyDataSetChanged();
        }
    };
}

onItemLongClik(...) - 负责ViewSwitcher行动

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (shopGridAdapter != null) {
        s = s.toString().trim();
        shopGridAdapter.getFilter().filter(s);
    }
}

shop_grid_item.xml

@SuppressLint("NewApi")
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

    ShopGridHolder holder = (ShopGridHolder) view.getTag();
    ShopGridItem shop = (ShopGridItem) shopGridAdapter.getItem(position);
    Animation animIn, animOut;

    if (shop.isOnMainView()) {
        animIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.in_right);
        animOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.out_left);
    } else {
        animIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.in_left);
        animOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.out_right);
    }

    ViewSwitcher viewSwitcher = holder.getViewSwitcher();
    viewSwitcher.setInAnimation(animIn);
    viewSwitcher.setOutAnimation(animOut);

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        viewSwitcher.setHasTransientState(true);
    }

    viewSwitcher.showNext();
    shop.switchView();

    return false;
}

1 个答案:

答案 0 :(得分:0)

好的,我发现代码有什么问题。动画完成后,我没有在setHasTransientState(false)上调用ViewSwitcher。设置它解决了问题。