Android:使用ViewSwitcher过滤GridView
我遇到了一个奇怪的问题。我的Android:使用ViewSwitcher过滤GridView,android,gridview,viewswitcher,Android,Gridview,Viewswitcher,我遇到了一个奇怪的问题。我的GridView项有一个底部栏,可通过ViewSwitcher进行切换GridViewAdapter实现可过滤。Filter和ViewSwitcher都能独立正常工作。但是,当我在使用ViewSwitcher后尝试过滤出GridView项目时,View似乎与执行切换的项目卡住了 为了更好的理解,这里有一段视频。切换底部栏后可以看到,ShopB的视图显示不正确。奇怪的是,根据调试器,填充适配器的ArrayList具有正确的数据。此外,按钮点击等操作也是正确的。只有视图是
GridView
项有一个底部栏,可通过ViewSwitcher
进行切换<代码>GridViewAdapter实现可过滤。Filter
和ViewSwitcher
都能独立正常工作。但是,当我在使用ViewSwitcher
后尝试过滤出GridView
项目时,View
似乎与执行切换的项目卡住了
为了更好的理解,这里有一段视频。切换底部栏后可以看到,ShopB的视图显示不正确。奇怪的是,根据调试器,填充适配器的ArrayList
具有正确的数据。此外,按钮点击等操作也是正确的。只有视图
是错误的。我真的被这件事吓坏了,如果有任何建议,我将不胜感激。下面是代码的重要部分
getView(…)
过滤器
@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(…)-负责视图切换操作
@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;
}
@SuppressLint(“NewApi”)
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
ShopGridHolder=(ShopGridHolder)view.getTag();
ShopGridItem商店=(ShopGridItem)shopGridAdapter.getItem(位置);
动画动画输入,动画输出;
if(shop.isOnMainView()){
animIn=AnimationUtils.loadAnimation(view.getContext(),R.anim.in_right);
animOut=AnimationUtils.loadAnimation(view.getContext(),R.anim.out_左);
}否则{
animIn=AnimationUtils.loadAnimation(view.getContext(),R.anim.in_左);
animOut=AnimationUtils.loadAnimation(view.getContext(),R.anim.out\u right);
}
ViewSwitcher ViewSwitcher=holder.getViewSwitcher();
viewSwitcher.setInAnimation(动画);
viewSwitcher.setOutAnimation(动画输出);
int currentapiVersion=android.os.Build.VERSION.SDK\u int;
if(currentapiVersion>=android.os.Build.VERSION\u code.JELLY\u BEAN){
viewSwitcher.setHasTransientState(真);
}
viewSwitcher.showNext();
shop.switchView();
返回false;
}
shop\u grid\u 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>
好的,我发现了代码的错误。动画完成后,我没有在ViewSwitcher
上调用setHasTransientState(false)
。设置它解决了问题
@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;
}
<?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>