2014-10-05 54 views
0

我遇到了一个奇怪的问题。我的GridView产品有一个可通过ViewSwitcher切换的底部条。 GridViewAdapter实现FilterableFilterViewSwitcher都能正常工作。但是,当我尝试在使用ViewSwitcher后过滤出GridView项目时,似乎View卡住了执行切换的项目。Android:使用ViewSwitcher过滤GridView

为了更好地了解这里的视频a link。正如您在切换底部栏后可以看到的,ShopB的View显示不正确。奇怪的是,根据调试器,正在填充AdapterArrayList具有正确的数据。还有按钮点击等操作是正确的。只有View是错的。我真的受到了这个困扰,并会感谢任何建议。以下是代码的重要部分。

getView(...)

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

回答

0

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