Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在RecyclerView中创建空视图?_Android_Android Fragments_Android Recyclerview_Android Adapter - Fatal编程技术网

Android 如何在RecyclerView中创建空视图?

Android 如何在RecyclerView中创建空视图?,android,android-fragments,android-recyclerview,android-adapter,Android,Android Fragments,Android Recyclerview,Android Adapter,我正在使用一个片段来处理具有不同适配器的RecyclerView,但是当我尝试在RecyclerView中显示emptyView文本视图时,它会阻止RecyclerView正常工作 我在适配器中创建了以下筛选器: /** * Method that filters the data using the onQueryTextSubmit and onQueryTextChange. * * @return a Filter class that calls the

我正在使用一个片段来处理具有不同适配器的RecyclerView,但是当我尝试在RecyclerView中显示emptyView文本视图时,它会阻止RecyclerView正常工作

我在适配器中创建了以下筛选器:

/**
     * Method that filters the data using the onQueryTextSubmit and onQueryTextChange.
     *
     * @return a Filter class that calls the method performFiltering of the FilterResults class
     * and this method applies the filter and returns a list with the resulting data filtered.
     */
@Override
public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence querySample) {

            if (emptyView.getVisibility() == View.VISIBLE) {
                emptyView.setVisibility(View.GONE);
            }

            /*
             * Verifies the value of the sampleNameSearched and compares the data.
             */
            if (querySample == null) {
                // Updates the recyclerView with the sampleList.
                sampleListFiltered.submitList(sampleList);
                // holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
            } else {

                // Cleans the accentuation, letter case and other symbols of the querySample.
                sampleNameSearched = Normalizer.normalize(querySample.toString(), Normalizer.Form.NFD)
                        .replaceAll("[^\\p{ASCII}]", "").toLowerCase();

                // Creates the list that will save the filtered samples.
                List<Sample> filteredSampleList = new LinkedList<>();

                // Gets the data filtered in the for loop.
                for (Sample sample : sampleList) {

                    // Cleans the accentuation, letter case and other symbols.
                    String sampleName = Normalizer.normalize(sample.getName(), Normalizer.Form.NFD)
                            .replaceAll("[^\\p{ASCII}]", "").toLowerCase();

                    // Adds the sample that matches the filter in the filteredSampleList.
                    if (sampleName.contains(sampleNameSearched)) {
                        filteredSampleList.add(sample);
                    }
                }

                // Updates the RecyclerView.
                sampleListFiltered.submitList(filteredSampleList);

                // TODO: Find a solution for not showing the emptyView instantly (not entering the onBindViewHolder).
                /*
                 * if filteredSampleList is empty, shows the emptyView with the proper message.
                 */
                if (filteredSampleList.isEmpty()) {
                    // Updates the RecyclerView.
                    emptyView.setVisibility(View.VISIBLE);
                } else {
                    if (emptyView.getVisibility() == View.VISIBLE) {
                        emptyView.setVisibility(View.GONE);
                    }
                }

            }

            // Returns the filterResults.
            FilterResults filterResults = new FilterResults();
            filterResults.values = sampleListFiltered;
            return filterResults;
        }

        // Publish the results on the RecyclerView.
        @Override
        protected void publishResults(CharSequence constraint, FilterResults filterResults) {
            notifyDataSetChanged();
        }
    };
}
/**
*方法,该方法使用onQueryTextSubmit和onQueryTextChange筛选数据。
*
*@返回调用FilterResults类的PerformFilter方法的筛选器类
*此方法应用过滤器并返回一个列表,其中包含过滤后的结果数据。
*/
@凌驾
公共过滤器getFilter(){
返回新筛选器(){
@凌驾
受保护过滤器结果性能过滤(CharSequence querySample){
if(emptyView.getVisibility()==View.VISIBLE){
emptyView.setVisibility(View.GONE);
}
/*
*验证sampleNameSearched的值并比较数据。
*/
if(querySample==null){
//使用sampleList更新recyclerView。
sampleListFiltered.submitList(sampleList);
//holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(),R.color.BaseColor_1));
}否则{
//清除querySample的重音符号、字母大小写和其他符号。
sampleNameSearched=Normalizer.normalize(querySample.toString(),Normalizer.Form.NFD)
.replaceAll(“[^\\p{ASCII}]”,“”)。toLowerCase();
//创建将保存筛选样本的列表。
List filteredSampleList=新建LinkedList();
//获取在for循环中筛选的数据。
对于(样本:样本列表){
//清除重音符号、字母大小写和其他符号。
字符串sampleName=Normalizer.normalize(sample.getName(),Normalizer.Form.NFD)
.replaceAll(“[^\\p{ASCII}]”,“”)。toLowerCase();
//在filteredSampleList中添加与筛选器匹配的示例。
if(sampleName.contains(sampleNameSearched)){
filteredSampleList.add(样本);
}
}
//更新RecyclerView。
sampleListFiltered.submitList(filteredSampleList);
//TODO:为不立即显示空视图(不输入onBindViewHolder)找到解决方案。
/*
*如果filteredSampleList为空,则显示带有正确消息的emptyView。
*/
if(filteredSampleList.isEmpty()){
//更新RecyclerView。
emptyView.setVisibility(View.VISIBLE);
}否则{
if(emptyView.getVisibility()==View.VISIBLE){
emptyView.setVisibility(View.GONE);
}
}
}
//返回filterResults。
FilterResults FilterResults=新的FilterResults();
filterResults.values=sampleListFiltered;
返回过滤器结果;
}
//在RecyclerView上发布结果。
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults FilterResults){
notifyDataSetChanged();
}
};
}
我不明白为什么我的emptyView.setVisibility()有时能工作,而其他的却不能

我的布局如下:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@id/search_sample_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="25dp"
        app:layout_constraintTop_toBottomOf="@+id/sample_folder_selector">

        <SearchView
            android:id="@+id/sample_search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:background="@drawable/search_box_border"
            android:clickable="true"
            android:iconifiedByDefault="false"
            android:layoutDirection="rtl"
            android:queryHint="@string/sample_search" />

    </RelativeLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ListNavigatorRecyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="25dp"
        app:layout_constraintBottom_toTopOf="@+id/ButtonPanel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_sample_field"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/emptyListNavigatorView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="100dp"
        android:gravity="center"
        android:text=""
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@+id/ButtonPanel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_sample_field"
        app:layout_constraintVertical_bias="1.0" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/ButtonPanel"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/BaseColor_4"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

        <ImageButton
            android:id="@+id/remove_sample_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="25dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:src="@drawable/btn_delete"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"

            style="@style/IconButton" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout> 

步骤2-编写不存在的示例:

步骤3-删除错误的字符:

现在正在发生什么(不应该发生)

步骤1-过滤器(工作):

步骤2-编写不存在的示例(不显示emptyView):

步骤3-删除错误字符(不显示示例或粗体字符):

如果我按下键盘上的搜索按钮,它会工作,但它本来打算在不按下搜索按钮的情况下工作

有人能帮我解决这个问题吗

我使用了toast,toast很有效,但是当涉及到在PerformFilter或布局中使用任何需要set方法的set方法时,Android似乎不知何故失去了它的工作流程

我尝试了以下实现:


如何解决这个问题?

我创建了一个EmptyRecyclView来扩展RecyclView行为,并添加了一个updateEmptyView()方法来验证我的RecyclView中有多少项,并相应地更新视图。我的解决方案就是基于此。另一个帮助我解决这个问题的来源是,它创建了一条我所需要的EmptyView消息


感谢maff91和具有相同行为的Google应用程序。

如何解决此问题?

我创建了一个EmptyRecyclView来扩展RecyclView行为,并添加了一个updateEmptyView()方法来验证我的RecyclView中有多少项,并相应地更新视图。我的解决方案就是基于此。另一个帮助我解决这个问题的来源是,它创建了一条我所需要的EmptyView消息

我要感谢maff91和具有相同行为的Google应用程序