首页 > 解决方案 > 滚动时回收器查看初始滞后

问题描述

所以我有一个非常基本的回收站视图,其中项目位于 cardViews 中。没有图片加载,它只是cardViews上表示的数据。当从服务器加载数据时,虽然最初尝试滚动但它开始滞后。一段时间后,滞后消失了。有什么问题?我的适配器代码如下。

public class TeacherJobBoardAdapter extends
    RecyclerView.Adapter<TeacherJobBoardAdapter.ViewHolder> {


public class ViewHolder extends RecyclerView.ViewHolder {
   
    TextView salary, location,numberOfStudents,curriculum,teacherGender,class1,class2,gender1,gender2,daysInWeek, std1Subject, std2Subject,time;
    LinearLayout linearLayoutStd2;
    ImageView doneTick;
    CardView v;
    SlideToActView slider;
    AnimatedVectorDrawable animated;
    Context context;
    
    public ViewHolder(View itemView, Context context) {
        
        super(itemView);
        this.context = context;
        v = itemView.findViewById(R.id.card);
        slider = itemView.findViewById(R.id.slider_teacherJobBoard);

        doneTick = itemView.findViewById(R.id.doneTick_teacherJobBoard);
        animated = (AnimatedVectorDrawable) doneTick.getDrawable();

        numberOfStudents = itemView.findViewById(R.id.spn_teacherJobBoard_numberOfStudents);
        linearLayoutStd2 = itemView.findViewById(R.id.linlayout_teacherJobBoard_std2);
        location = itemView.findViewById(R.id.txt_teacherJobBoard_location);
        salary = itemView.findViewById(R.id.txt_teacherJobBoard_salary);
        curriculum = itemView.findViewById(R.id.txt_teacherJobBoard_curriculum);
        class1 = itemView.findViewById(R.id.txt_teacherJobBoard_std1Class);
        class2 = itemView.findViewById(R.id.txt_teacherJobBoard_std2Class);
        gender1 = itemView.findViewById(R.id.txt_teacherJobBoard_std1Gender);
        gender2 = itemView.findViewById(R.id.txt_teacherJobBoard_std2Gender);
        teacherGender = itemView.findViewById(R.id.txt_teacherJobBoard_tutorGender);
        daysInWeek = itemView.findViewById(R.id.txt_teacherJobBoard_daysInWeek);
        std1Subject = itemView.findViewById(R.id.txt_teacherJobBoard_std1Subject);
        std2Subject = itemView.findViewById(R.id.txt_teacherJobBoard_std2Subject);
        time = itemView.findViewById(R.id.txt_teacherJobBoard_time);
    }


}


private List<ParseObject> mObjects;

public TeacherJobBoardAdapter(List<ParseObject> object) {
    mObjects = object;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View contactView = inflater.inflate(R.layout.item_teacher_job_board, parent, false);

    // Return a new holder instance
    return new ViewHolder(contactView,context);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int pos) {
    int position = pos;
    holder.salary.setText(""+mObjects.get(pos).getInt("salary"));
    holder.location.setText(mObjects.get(pos).getString("location"));
    holder.numberOfStudents.setText(mObjects.get(pos).getInt("numberOfStudents")+"");
    holder.curriculum.setText(mObjects.get(pos).getString("curriculum"));
    holder.teacherGender.setText(mObjects.get(pos).getString("teacherGender"));

    if (mObjects.get(pos).getInt("numberOfStudents")==1){
        holder.linearLayoutStd2.setVisibility(View.GONE);
    }else{
        holder.gender2.setText(mObjects.get(pos).getString("gender2"));
        holder.class2.setText(mObjects.get(pos).getString("class2"));
        holder.std2Subject.setText(mObjects.get(pos).getString("subject2")+"");
    }
    holder.gender1.setText(mObjects.get(pos).getString("gender1"));
    holder.class1.setText(mObjects.get(pos).getString("class1"));
    holder.std1Subject.setText(mObjects.get(pos).getString("subject1")+"");


    holder.daysInWeek.setText(mObjects.get(pos).getInt("daysInWeek")+"");
    holder.time.setText(mObjects.get(pos).getString("time"));

    holder.slider.setOnSlideCompleteListener(new SlideToActView.OnSlideCompleteListener() {
        @Override
        public void onSlideComplete(SlideToActView slideToActView) {
            TeacherJobBoard myActivity = (TeacherJobBoard) holder.context;
            myActivity.apply(slideToActView,holder.doneTick,holder.animated);
        }
    });
}

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

这是项目布局的 xml

<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/_4sdp"
        android:layout_marginRight="@dimen/_4sdp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="@dimen/_5sdp"
        app:cardElevation="@dimen/_3sdp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:weightSum="10">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="3">


                <TextView
                    android:id="@+id/txt_teacherJobBoard_location"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="@font/kiona_regular"
                    android:paddingLeft="@dimen/_8sdp"
                    android:paddingTop="@dimen/_8sdp"
                    android:paddingBottom="@dimen/_8sdp"
                    android:text="ADABOR"
                    android:textColor="@color/tutor_bear_dark_blue"
                    android:textSize="@dimen/_20sdp" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:layout_weight="1"
                        android:fontFamily="@font/kiona_regular"
                        android:paddingTop="@dimen/_8sdp"
                        android:paddingEnd="@dimen/_8sdp"
                        android:text="SALARY"
                        android:textAlignment="textEnd"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_20sdp" />


                    <TextView
                        android:id="@+id/txt_teacherJobBoard_salary"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:layout_weight="1"
                        android:drawableEnd="@drawable/ic_taka"
                        android:fontFamily="@font/kiona_regular"
                        android:paddingBottom="@dimen/_8sdp"
                        android:text="9000"
                        android:textAlignment="textEnd"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_20sdp" />


                </LinearLayout>


            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/_1sdp"
                android:layout_marginLeft="@dimen/_10sdp"
                android:layout_marginRight="@dimen/_10sdp"
                android:background="@color/tutor_bear_dark_blue" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/_2sdp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="@font/roboto_light"
                        android:paddingStart="@dimen/_7sdp"
                        android:text="@string/studentViewMyPost_numberOfStudents"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <TextView
                        android:id="@+id/spn_teacherJobBoard_numberOfStudents"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="@font/roboto_light"
                        android:paddingEnd="@dimen/_7sdp"
                        android:text="2"
                        android:textAlignment="textEnd"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_6sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_light"
                        android:paddingLeft="@dimen/_7sdp"
                        android:text="@string/teacherJobBoard_curriculum"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />


                    <TextView
                        android:id="@+id/txt_teacherJobBoard_curriculum"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_light"
                        android:paddingEnd="@dimen/_7sdp"
                        android:text="English Medium"
                        android:textAlignment="textEnd"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="@font/roboto_light"
                        android:paddingStart="@dimen/_7sdp"
                        android:text="Student 1"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical">


                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std1Subject"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Physics, Chemistry, Maths, Psycology"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />

                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std1Gender"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:layout_marginTop="@dimen/_3sdp"
                            android:drawableStart="@drawable/ic_male"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Male"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />

                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std1Class"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:layout_marginTop="@dimen/_3sdp"
                            android:layout_marginBottom="@dimen/_3sdp"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Class 8"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />


                    </LinearLayout>


                </LinearLayout>


                <LinearLayout
                    android:id="@+id/linlayout_teacherJobBoard_std2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_7sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="@font/roboto_light"
                        android:paddingStart="@dimen/_7sdp"
                        android:text="Student 2"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical">


                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std2Subject"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Physics, Chemistry, Maths, Psycology"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />

                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std2Gender"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:layout_marginTop="@dimen/_3sdp"
                            android:drawableStart="@drawable/ic_male"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Male"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />

                        <TextView
                            android:id="@+id/txt_teacherJobBoard_std2Class"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:layout_marginTop="@dimen/_3sdp"
                            android:layout_marginBottom="@dimen/_3sdp"
                            android:fontFamily="@font/roboto_light"
                            android:paddingEnd="@dimen/_7sdp"
                            android:text="Class 8"
                            android:textAlignment="textEnd"
                            android:textColor="@color/tutor_bear_dark_blue"
                            android:textSize="@dimen/_13sdp" />


                    </LinearLayout>


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_6sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_light"
                        android:paddingLeft="@dimen/_7sdp"
                        android:text="@string/studentViewMyPost_preferredTutorGender"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />


                    <TextView
                        android:id="@+id/txt_teacherJobBoard_tutorGender"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableStart="@drawable/ic_female"
                        android:fontFamily="@font/roboto_light"
                        android:paddingEnd="@dimen/_7sdp"
                        android:text="Female"
                        android:textAlignment="textEnd"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_6sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:fontFamily="@font/roboto_light"
                        android:paddingLeft="@dimen/_7sdp"
                        android:text="@string/studentViewMyPost_daysInWeek"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />


                    <TextView
                        android:id="@+id/txt_teacherJobBoard_daysInWeek"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableStart="@drawable/ic_calender"
                        android:fontFamily="@font/roboto_light"
                        android:paddingEnd="@dimen/_7sdp"
                        android:text="5"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_6sdp"
                    android:layout_marginBottom="@dimen/_8sdp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:fontFamily="@font/roboto_light"
                        android:paddingLeft="@dimen/_7sdp"
                        android:text="@string/studentViewMyPost_preferredTime"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />


                    <TextView
                        android:id="@+id/txt_teacherJobBoard_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableStart="@drawable/ic_time_new"
                        android:fontFamily="@font/roboto_light"
                        android:paddingEnd="@dimen/_7sdp"
                        android:text="9:00 PM - 6:00 AM"
                        android:textColor="@color/tutor_bear_dark_blue"
                        android:textSize="@dimen/_13sdp" />
                </LinearLayout>

                <com.ncorti.slidetoact.SlideToActView
                    android:id="@+id/slider_teacherJobBoard"
                    android:layout_width="@dimen/_220sdp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:layout_marginBottom="@dimen/_10sdp"
                    android:clickable="false"
                    android:hapticFeedbackEnabled="true"
                    app:animation_duration="250"
                    app:area_margin="@dimen/_5sdp"
                    app:bump_vibration="60"
                    app:complete_icon="@drawable/anim_loading_spinner"
                    app:icon_margin="@dimen/_5sdp"
                    app:inner_color="@color/color_white"
                    app:outer_color="@color/tutor_bear_dark_blue"
                    app:slider_height="@dimen/_40sdp"
                    app:slider_icon="@drawable/ic_next_arrow"
                    app:text="Slide to Apply" />

                <ImageView
                    android:id="@+id/doneTick_teacherJobBoard"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_40sdp"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:layout_marginBottom="@dimen/_10sdp"
                    android:src="@drawable/done_tick_new_anim"
                    android:visibility="gone" />
            </LinearLayout>
    </LinearLayout>
    </androidx.cardview.widget.CardView>
    </LinearLayout>

标签: androidandroid-studioandroid-recyclerview

解决方案


尝试创建应用程序的发布版本。调试版本通常会显示少量的动画延迟。


推荐阅读