I came across a strange problem. My GridView
item has a bottom bar switchable via ViewSwitcher
. GridViewAdapter
implemets Filterable
. Both Filter
and ViewSwitcher
work correctly on their own. However, when I try to filter out GridView
items after using ViewSwitcher
it seems that the View
is stuck with the item on which switching was performed.
For better understading here's a link with a video. As you can see after switching the bottom bar, ShopB's View
is displayed incorrectly. The strange thing is that the ArrayList
, which is populating the Adapter
has correct data according to the debugger. Also actions like buttons' clicks are correct. Only the View
is wrong. I'm really buffeled by this and will be thankful for any advice. Below are the important parts of code.
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;
}
Filter
@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(...) - applying the filter
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (shopGridAdapter != null) {
s = s.toString().trim();
shopGridAdapter.getFilter().filter(s);
}
}
onItemLongClik(...) - responsible for ViewSwitcher action
@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>