ViewPager缓存使SearchView结果看起来很奇怪

毗湿奴

小背景

我有SearchView一个一起ViewPageractivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/homeSearchView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toTopOf="@id/viewPager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/homeMenuIcon"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/homeSearchView" />


    <ImageView
        android:id="@+id/homeMenuIcon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_margin="8dp"
        android:contentDescription="Overflow Menu Icon"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/viewPager"
        tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>

我已经把它贴OnQueryTextListenerSearchViewMainActivity.java


ViewPager viewPager = findViewById(R.id.viewPager);
List<Quote> allQuotesList = getQuotes();//From `Volley` Request
QuoteViewPagerAdapter adapter = new QuoteViewPagerAdapter(getSupportFragmentManager(), allQuotesList);
viewPager.setAdapter(adapter);


SearchView searchView = findViewById(R.id.homeSearchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                getFilter().filter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                getFilter().filter(newText);
                return false;
            }
        });

 private Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<Quote> filteredResults = new ArrayList<>();

                if (constraint.toString().isEmpty())
                    filteredResults.addAll(allQuotesList);
                else
                    for (Quote quote : allQuotesList) {
                        if (quote.getQuote().toLowerCase().contains(constraint.toString().toLowerCase())
                                || quote.getAuthor().toLowerCase().contains(constraint.toString().toLowerCase()))
                            filteredResults.add(quote);
                    }

                FilterResults filterResult = new FilterResults();
                filterResult.values = filteredResults;

                return filterResult;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                adapter.setQuoteList((List<Quote>) results.values);
                adapter.notifyDataSetChanged();
            }
        };
    }

这是 QuoteViewPagerAdapter.java

public class QuoteViewPagerAdapter extends FragmentPagerAdapter {
    private List<Quote> quoteList;

    public QuoteViewPagerAdapter(FragmentManager fm, List<Quote> fragmentList) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        quoteList = fragmentList;
    }

    @androidx.annotation.NonNull
    @Override
    public Fragment getItem(int position) {
        return QuoteFragment.newInstance(quoteList.get(position));
    }

    @Override
    public int getCount() {
        return quoteList.size();
    }

    @Override
    public int getItemPosition(@androidx.annotation.NonNull Object object) {
        return POSITION_NONE;
    }

    public void setQuoteList(List<Quote> quoteList) {
        this.quoteList = quoteList;
    }
}

这是 Quote.java

public class Quote {

    private String quote;
    private String author;

    public Quote() {
    }

    public Quote(String quote, String author) {
        this.quote = quote;
        this.author = author;
    }

    public String getQuote() {
        return quote;
    }

    public void setQuote(String quote) {
        this.quote = quote;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

问题

当搜索到某些内容时,quoteList (List<Quote>)inQuoteViewPagerAdapter将更新为setQuoteList()因此,getCount()返回新的大小。

但是,旧片段(其中2个)保留在中ViewPager这使搜索结果在滚动ViewPager2次后出现同样,这导致最后2个结果被忽略。

另外,如果搜索结果的大小只有1个,则ViewPager冻结将使滚动无法进行(因为getCount()返回1,因此ViewPager仅显示了旧的第一个片段)

我尝试过的

//inside publishResults()
adapter = null;
viewPager.setAdapter(null);
viewPager.removeAllViews();
viewPager.removeAllViewsInLayout();
viewPager.destroyDrawingCache();

adapter = new QuoteViewPagerAdapter(getSupportFragmentManager(), (List<Quote>) results.values);
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();

//inside onCreate()

viewPager.setSaveFromParentEnabled(false);
viewPager.setSaveEnabled(false);

预期行为

ViewPager从位置0开始显示搜索结果中的新列表

期待从StackOverflow社区获得帮助。提前致谢。

毗湿奴

通过更改FragmentPagerAdapterFragmentStatePagerAdapterin可以按预期工作QuoteViewPagerAdapter.java

但是有点晚了,为未来的读者写这个答案...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Sklearn LogisticRegression predict_proba 结果看起来很奇怪

来自分类Dev

为什么按钮看起来很奇怪?

来自分类Dev

字体在Chrome中看起来很奇怪

来自分类Dev

字体在Firefox中看起来很奇怪

来自分类Dev

看起来很奇怪的Google地球

来自分类Dev

鹦鹉螺(?)看起来很奇怪

来自分类Dev

为什么按钮看起来很奇怪?

来自分类Dev

PySpark:OneHotEncoder 的输出看起来很奇怪

来自分类Dev

React Native 圆角看起来很奇怪

来自分类Dev

尝试将 PCM 转换为频率图,但结果在 0 附近看起来很奇怪

来自分类Dev

二维Perlin噪声看起来很奇怪

来自分类Dev

SSRS +报表管理器看起来很奇怪

来自分类Dev

Ubuntu 13.04图标看起来很奇怪,请帮忙

来自分类Dev

最新的Chrome 37.xx字体看起来很奇怪

来自分类Dev

AngularStrap日期选择器看起来很奇怪

来自分类Dev

Xcode 7.1 iOS模拟器看起来很奇怪

来自分类Dev

如果宽度小于116,则搜索栏看起来很奇怪

来自分类Dev

文字过长时列表看起来很奇怪

来自分类Dev

两组的R生存曲线看起来很奇怪

来自分类Dev

Facebook请求FBWebDialog UI看起来很奇怪

来自分类Dev

复杂模型的Three.js阴影看起来很奇怪

来自分类Dev

基于bootstrap的站点在firefox中看起来很奇怪

来自分类Dev

Ubuntu 13.04图标看起来很奇怪,请帮忙

来自分类Dev

为什么Chrome上的字体看起来很奇怪?

来自分类Dev

Chrome的字体看起来有些奇怪

来自分类Dev

Facebook字体在12.04上的Firefox上看起来很奇怪

来自分类Dev

Google字体在iPhone上看起来很奇怪

来自分类Dev

iOS Tableviewcell分隔符看起来很奇怪

来自分类Dev

最新的chrome 37.xx字体看起来很奇怪

Related 相关文章

热门标签

归档