RecyclerView 不会一直滚动到底部

时间:2021-04-26 12:18:38

标签: java android android-recyclerview

我在 PlayStore 上有一个应用程序,它有一个显示图像和/或视频的 recyclerView。我面临的问题是,当我滚动到底部,然后任何项目或其属性更改时,它会尝试向上滚动。我曾尝试更改 recyclerview 版本,但没有用。

这是视频:- Screen Record

以下是我的布局:-

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:background="@android:color/transparent"
    android:elevation="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/floating_image_parent_of_parent"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:background="?android:colorBackground"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:orientation="horizontal">
            <TextView
                android:text="@string/select_current_album"
                android:gravity="center_vertical|start"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                />
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:checked="false"
                android:layout_gravity="end"
                android:id="@+id/select_all_checkbox"
                />
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:id="@+id/gallery_recycler_view"/>

            <com.sugarsnooper.filetransfer.MyFastScroll
                android:id="@+id/fastscroll"
                app:fastscroll__handleColor="?colorControlActivated"
                app:fastscroll__bubbleColor="?colorControlActivated"
                app:fastscroll__bubbleTextAppearance="@style/StyledScrollerTextAppearance"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:gravity="end"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:id="@+id/floating_action_button_filter_gallery"
            android:backgroundTint="@color/button_color"
            app:icon="@drawable/ic_filter"
            app:iconTint="#ffffff"
            android:text="@string/filter"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_marginBottom="60dp"
            android:layout_marginEnd="24dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/file_info_loading_progress_bar"
        android:paddingStart="30dp"
        android:paddingEnd="30dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:gravity="center">
        <ProgressBar
            android:maxHeight="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/please_wait"
            android:paddingStart="10dp"
            android:paddingEnd="10dp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

上面的xml放在这个viewpager2中:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
<com.google.android.material.tabs.TabLayout

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:id="@+id/file_selection_tl"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="match_parent"
    android:layout_height="80dp" />
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/file_selection_vp"
        app:layout_constraintTop_toBottomOf="@id/file_selection_tl"
        android:layout_width="match_parent"
        android:layout_height="0dp"

        app:layout_constraintBottom_toBottomOf="parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:backgroundTint="@android:color/transparent"
        android:gravity="bottom|start">
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/send_fab_substitute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_send_24"
            android:layout_marginBottom="30dp"
            app:borderWidth="0dp"
            android:layout_marginStart="24dp"
            android:backgroundTint="@color/button_color"
            app:tint="@color/white"
            />
    </LinearLayout>



    <eightbitlab.com.blurview.BlurView
        android:id="@+id/blurView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/send_actions_buttons"
        android:background="#11000000"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="90dp"
        android:padding="20dp">

        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/selected"
            android:layout_marginEnd="20dp"
            android:gravity="center"
            android:textAlignment="gravity"
            android:layout_weight="1"
            android:textSize="8sp"
            android:clickable="false"
            android:id="@+id/selected_files_counter"
            android:backgroundTint="@color/button_color"
            android:textColor="@color/button_text_color"/>
        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAlignment="gravity"
            android:textSize="8sp"
            android:text="@string/send"
            android:clickable="true"
            android:id="@+id/send_files_fab"
            android:focusable="true"
            android:backgroundTint="@color/button_color"
            android:textColor="@color/button_text_color"/>

    </LinearLayout>
    </eightbitlab.com.blurview.BlurView>
</androidx.constraintlayout.widget.ConstraintLayout>

以下是recyclerAdapter:-

    private static class GalleryRecyclerViewAdapter extends RecyclerView.Adapter<GalleryRecyclerViewAdapter.MyViewHolder> implements SectionTitleProvider {

        private final onClickListener onClickListener;

        private FragmentActivity requireActivity(){
            return activity;
        }


        @Override
        public String getSectionTitle(int position) {
            //this String will be shown in a bubble for specified position
            String text = itemsToShow.get(position).getModifiedString();
            try {
                if (text == null) {
                    text = itemsToShow.get(position + 1).getModifiedString();
                }
            }
            catch (IndexOutOfBoundsException ignored){

            }
            return text;
        }




        private interface onClickListener{
            public void onClick(View item, int position, boolean seperator);
            public void onLongClick(View item, int position, boolean seperator);
        }

        public GalleryRecyclerViewAdapter(onClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(requireActivity()).inflate(R.layout.grid_item, parent, false);
            return new MyViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//            Log.e("View Load", String.valueOf(itemsToShow.get(position).isHasToShow()));
            if (itemsToShow.get(position).isSeperator()){
                holder.label.setVisibility(View.VISIBLE);
                holder.ivparent.setVisibility(GONE);
                String date = dateFormat.format(new Date(itemsToShow.get(position + 1).getModified()));
                holder.date_label.setText(date);
                boolean isChecked = true;
                for (Media media : itemsToShow){
                    try{
                        if (dateFormat.format(new Date(media.getModified())).equals(date)){
                            if (!media.isChecked()){
                                isChecked = false;
                                break;
                            }
                        }
                    }
                    catch (Exception e){

                    }
                }
                holder.checkBox.setChecked(isChecked);
            }
            else {
                if (!itemsToShow.get(position).isChecked()){
//                    holder.ivparent.setBackgroundColor(Color.parseColor("#00777777"));
//                    holder.imageView.setPadding(0, 0, 0, 0);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        holder.imageView.setForeground(uncover_image);
                    }
                    holder.itemChecked.setChecked(false);
                }
                else {
//                    holder.ivparent.setBackgroundColor(requireActivity().getResources().getColor(R.color.media_selected_border));
//                    holder.imageView.setPadding(max_padding, max_padding, max_padding, max_padding);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        holder.imageView.setForeground(cover_image);
                    }
                    holder.itemChecked.setChecked(true);
                }
                if (itemsToShow.get(position).getFILETYPE() == Media.TYPE_VIDEO){
                    holder.durationText.setVisibility(View.VISIBLE);
                    Media media = itemsToShow.get(position);
                    if (media.getDuration() == null)
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                try {
                                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                                    retriever.setDataSource(requireActivity(), itemsToShow.get(position).getUri());
                                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                                    long timeInMillisec = Long.parseLong(time);
                                    retriever.release();
                                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                                        @Override
                                        public void run() {
                                            String time = formatTime(timeInMillisec);
                                            media.setDuration(time);
                                            holder.durationText.setText(time);
                                        }
                                    });
                                }
                                catch (NullPointerException ne){
                                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                                        @Override
                                        public void run() {
                                            holder.durationText.setText("?");
                                        }
                                    });
                                }
                                catch (Exception ie){
                                    ie.printStackTrace();
                                }
                            }
                        }).start();
                    else
                        holder.durationText.setText(media.getDuration());
                }
                else{
                    holder.durationText.setVisibility(GONE);
                }
                holder.label.setVisibility(GONE);
                holder.ivparent.setVisibility(View.VISIBLE);
                Glide.with(requireActivity()).load(itemsToShow.get(position).getUri()).into(holder.imageView);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(holder.itemView, position, itemsToShow.get(position).isSeperator());
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onClickListener.onLongClick(holder.itemView, position, itemsToShow.get(position).isSeperator());
                    if (!itemsToShow.get(position).isSeperator())
                        return true;
                    else
                        return false;
                }
            });
        }

        @Override
        public void onViewRecycled(@NonNull MyViewHolder holder) {
            super.onViewRecycled(holder);
            holder.recycle();
        }

        @Override
        public int getItemCount() {
            return itemsToShow.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            LinearLayout ivparent;
            LinearLayout label;
            ImageView imageView;
            TextView date_label;
            CheckBox checkBox;
            CheckBox itemChecked;
            TextView durationText;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(R.id.imageView);
                ivparent = itemView.findViewById(R.id.iv_parent);
                label = itemView.findViewById(R.id.multi_selection_and_date_change_bar);
                date_label = itemView.findViewById(R.id.date_text_view_recycler_view);
                checkBox = itemView.findViewById(R.id.select_all_checkbox_recycler_view);
                durationText = itemView.findViewById(R.id.media_duration_text);
                itemChecked = itemView.findViewById(R.id.checkbox_item_checked_round);
            }

            public void recycle() {
                Glide.with(requireActivity()).clear(imageView);
            }
        }
        private String formatTime(long milliseconds) {
            int seconds = (int) (milliseconds / 1000) % 60 ;
            int minutes = (int) ((milliseconds / (1000*60)) % 60);
            int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
            String hh = "";
            if (hours <= 9){
                hh = "0" + hours;
            }
            else{
                hh = String.valueOf(hours);
            }
            String mm = "";
            if (minutes <= 9){
                mm = "0" + minutes;
            }
            else{
                mm = String.valueOf(minutes);
            }
            String ss = "";
            if (seconds <= 9){
                ss = "0" + seconds;
            }
            else{
                ss = String.valueOf(seconds);
            }
            if (hours > 0)
                return hh + ":" + mm + ":" + ss;
            else
                return mm + ":" + ss;
        }

    }

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    cardview:cardCornerRadius="5dp"
    >

    <LinearLayout
        android:descendantFocusability="blocksDescendants"
        android:visibility="gone"
        android:id="@+id/multi_selection_and_date_change_bar"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">
        <TextView
            android:layout_weight="1"
            android:id="@+id/date_text_view_recycler_view"
            android:textAlignment="gravity"
            android:padding="5dp"
            android:gravity="center_vertical|start"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <CheckBox
            android:clickable="false"
            android:id="@+id/select_all_checkbox_recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical|end"/>
    </LinearLayout>



    <LinearLayout
        android:id="@+id/iv_parent"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="130dp">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:elevation="1dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="end|top">
    <CheckBox
        android:clickable="false"
        android:id="@+id/checkbox_item_checked_round"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleY="1.3"
        android:scaleX="1.3"
        android:button="@drawable/custom_checkbox"
        android:layout_margin="10dp" />
    </LinearLayout>
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            />
    <LinearLayout
        android:elevation="1dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="end|bottom">
    <TextView
        android:id="@+id/media_duration_text"
        android:visibility="gone"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:textColor="#FFFFFF"
        android:background="#BB000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

当前的gradle文件:-

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.myapp.filetransfer"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 36
        versionName "0.172954"
    }
    buildTypes {
        release {
            minifyEnabled false
            useProguard false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            minifyEnabled true
            useProguard true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'com.thanosfisherman.wifiutils:wifiutils:1.6.4'
    implementation 'com.thanosfisherman.elvis:elvis:3.0'


    implementation 'com.futuremind.recyclerfastscroll:fastscroll:0.2.5'


    implementation 'com.simplecityapps:recyclerview-fastscroll:2.0.1'
    implementation 'me.zhanghai.android.fastscroll:library:1.1.4'


    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.google.android.gms:play-services-cast-framework:18.1.0'
    implementation 'com.google.android.exoplayer:exoplayer:2.11.7'

    implementation 'com.adityak:browsemyfiles:1.9'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation('com.journeyapps:zxing-android-embedded:4.1.0') { transitive = false }
    implementation 'com.google.zxing:core:3.3.0'
    implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3'
    implementation 'com.github.iammert:MaterialIntroView:1.6.0'

    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.fragment:fragment:1.2.5"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.github.ybq:Android-SpinKit:1.4.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0'
    testImplementation 'junit:junit:4.12'

    implementation 'com.github.MFlisar:DragSelectRecyclerView:0.3'
    implementation 'com.eightbitlab:blurview:1.6.5'
    implementation "androidx.core:core-ktx:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"



    implementation 'com.github.chrisbanes:PhotoView:2.2.0'

}
android {
    useLibrary 'org.apache.http.legacy'
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
repositories {
    mavenCentral()
}

编辑:- 回收器和适配器初始化:- http://www.sugarsnooper.com/code.txt

0 个答案:

没有答案