0

我遇到了一个奇怪的问题。我的GridView项目有一个底栏,可通过ViewSwitcher. GridViewAdapter实现Filterable。两者都Filter可以ViewSwitcher自行正常工作。但是,当我在使用后尝试过滤掉GridView项目ViewSwitcher时,似乎View卡在了执行切换的项目上。

为了更好地理解这里是一个视频链接。切换底部栏后可以看到,ShopB 的View显示不正确。奇怪的是,根据调试器ArrayList填充的具有正确的数据。Adapter按钮点击等动作也是正确的。只有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;
}

筛选

@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();
        }
    };
}

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

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

onItemLongClik(...) - 负责 ViewSwitcher 操作

@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;
}

shop_grid_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fragment_background"
    android:descendantFocusability="blocksDescendants" >

        <RelativeLayout
            android:id="@+id/layout_shop"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:contentDescription="@string/desc_shop_logo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/image_logo" />

            <RelativeLayout
                android:id="@+id/relative_layout_top"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="#66696969">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000000"
                    android:textSize="16sp"
                    android:id="@+id/text_shop_name"
                    android:layout_marginLeft="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"/>

                <CheckBox
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginEnd="8dp"
                    android:button="@drawable/favorite_selector"
                    android:id="@+id/checkbox_favorite"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>

            </RelativeLayout>

            <ViewSwitcher
                android:id="@+id/view_switcher"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true" >

                <RelativeLayout
                    android:id="@+id/relative_layout_bottom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#66696969">

                    <TextView
                        android:paddingLeft="4dp"
                        android:paddingStart="4dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#000000"
                        android:textSize="16sp"
                        android:id="@+id/text_price"
                        android:layout_centerVertical="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"/>

                    <SeekBar
                        android:id="@+id/seekbar_price"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:thumbOffset="8dp"
                        android:progress="0"
                        android:secondaryProgress="0"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/button_add_to_cart"
                        android:layout_toStartOf="@+id/button_add_to_cart"
                        android:layout_toRightOf="@+id/text_price"
                        android:layout_toEndOf="@+id/text_price"
                        android:progressDrawable="@drawable/seekbar_progress"
                        android:thumb="@drawable/seekbar_thumb"/>

                    <ImageButton
                        android:id="@+id/button_add_to_cart"
                        android:contentDescription="@string/desc_add_to_cart"
                        android:background="@color/transparent"
                        android:src="@drawable/ic_add_to_cart"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:paddingRight="4dp"
                        android:paddingEnd="4dp"
                        android:focusable="false"
                        android:focusableInTouchMode="false"/>

                </RelativeLayout>

                <LinearLayout
                    android:id="@+id/layout_shop_buttons"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:background="#66696969" >

                    <ImageButton
                        android:id="@+id/button_show_on_map"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="@color/transparent"
                        android:layout_weight="1"
                        android:src="@drawable/ic_show_on_map"
                        android:contentDescription="@string/desc_map"
                        android:focusable="false"
                        android:focusableInTouchMode="false"/>

                    <ImageButton
                        android:id="@+id/button_redirect"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="@color/transparent"
                        android:layout_weight="1"
                        android:src="@drawable/ic_go_to_page"
                        android:contentDescription="@string/desc_redirect"
                        android:focusable="false"
                        android:focusableInTouchMode="false"/>

                </LinearLayout>

        </ViewSwitcher>

    </RelativeLayout>

</RelativeLayout>
4

1 回答 1

0

好的,我发现代码有什么问题。动画完成后我没有打电话setHasTransientState(false)ViewSwitcher设置它解决了这个问题。

于 2014-10-05T11:31:40.107 回答