首页 > 解决方案 > 带有 CardView 的 RecyclerView 滞后且滚动不顺畅

问题描述

尝试在回收站视图中加载大约 500 行数据,但滚动时出现滞后。

数据不包括任何类型的图像,它只是纯文本。因此找不到任何滞后的原因。

使用的xml和java代码如下:-

xml

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/markeListCard"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardPreventCornerOverlap="true"
    android:layout_alignParentTop="true"
    card_view:cardUseCompatPadding="true"
    app:cardCornerRadius="@dimen/_5sdp"
    app:cardElevation="@dimen/_5sdp"
    android:layout_marginBottom="14dp"
    android:paddingBottom="@dimen/_150sdp">


    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="@dimen/_125sdp"
        android:background="@drawable/compltedtlist"
        android:layout_gravity="center">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_5sdp"
                android:text="School Name :"
                android:singleLine="true"
                android:inputType="textPostalAddress"
                android:imeOptions="actionNext"
                android:textStyle="bold"
                android:textSize="@dimen/_10ssp"
                android:textColor="#0E9CE0"
                android:paddingLeft="@dimen/_10sdp"
                android:id="@+id/scndprsnname" >
            </TextView>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_10sdp"
                android:maxLines="1"
                android:text="Divisional Public School"
                android:ems="10"
                android:singleLine="true"
                android:textColor="#000"
                android:imeOptions="actionNext"
                android:textSize="@dimen/_10ssp"
                android:id="@+id/scholnametxt" >
            </TextView>

        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_30sdp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_5sdp"
                android:text="District :"
                android:singleLine="true"
                android:inputType="textPostalAddress"
                android:imeOptions="actionNext"
                android:textStyle="bold"
                android:textSize="@dimen/_10ssp"
                android:textColor="#0E9CE0"
                android:paddingLeft="@dimen/_10sdp"
                android:id="@+id/detaildadresedit" >
            </TextView>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_38sdp"
                android:maxLines="1"
                android:text="Aleem Ahmed"
                android:ems="10"
                android:singleLine="true"
                android:textColor="#000"
                android:imeOptions="actionNext"
                android:textSize="@dimen/_10ssp"
                android:id="@+id/tehsilname" >
            </TextView>

        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_48sdp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_5sdp"
                android:text="Tehsil :"
                android:singleLine="true"
                android:inputType="textPostalAddress"
                android:imeOptions="actionNext"
                android:textStyle="bold"
                android:textSize="@dimen/_10ssp"
                android:textColor="#0E9CE0"
                android:paddingLeft="@dimen/_10sdp"
                android:id="@+id/pcname" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_45sdp"
                android:maxLines="1"
                android:text="Lahore"
                android:ems="10"
                android:singleLine="true"
                android:textColor="#000"
                android:imeOptions="actionNext"
                android:textSize="@dimen/_10ssp"
                android:id="@+id/citytxt" >
            </TextView>

        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_65sdp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/_5sdp"
                android:text="Address :"
                android:textStyle="bold"
                android:ems="10"
                android:singleLine="true"
                android:textColor="#0E9CE0"
                android:imeOptions="actionNext"
                android:textSize="@dimen/_10ssp"
                android:paddingLeft="@dimen/_10sdp"
                android:id="@+id/provnceedit" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginRight="@dimen/_10sdp"
                android:layout_marginLeft="-40dp"
                android:maxLines="1"
                android:text="h#10 stret no 17 sabzar lahore"
                android:ems="30"
                android:singleLine="true"
                android:textColor="#000"
                android:imeOptions="actionNext"
                android:textSize="@dimen/_10ssp"
                android:id="@+id/addrestxt" >
            </TextView>

        </LinearLayout>










        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_50sdp"
            android:layout_marginTop="@dimen/_70sdp"
            android:layout_gravity="center"
            android:paddingTop="@dimen/_10sdp"
            android:gravity="center"
            android:textAlignment="center"
            android:layout_marginLeft="@dimen/_170sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/uploadbtn"
                android:layout_width="@dimen/_140sdp"
                android:layout_height="@dimen/_30sdp"
                android:background="@drawable/yelowbtn"
                android:layout_gravity="center"
                android:textStyle="bold"
                android:textSize="@dimen/_10sdp"
                android:textAlignment="center"
                android:layout_marginTop="@dimen/_20sdp"
                android:layout_marginBottom="@dimen/_15sdp"
                android:gravity="center"
                android:text="Stage 2"
                android:textColor="#000" />

        </LinearLayout>


    </RelativeLayout>



</android.support.v7.widget.CardView>
    <!--
    </LinearLayout>-->

适配器

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.offlinemap.R;
import com.offlinemap.form.FormStage2;
import com.offlinemap.form.FormToUpdateData;

import java.util.ArrayList;
import java.util.List;

public class Stage2RecyclerViewAdapterViewCompleteList extends RecyclerView.Adapter<Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder> {

    private Context mCtx;
    private List<ModeClassCompleteListStage2> searcList;
    View thumb1View;
    private String jobid;
    private Animator mCurrentAnimator;
    private int mShortAnimationDuration;
    private modelclascompletelist model;
    String job;
    private Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    //ImageView image;
    private ArrayList<String> list = new ArrayList<String>();
    String pos;
    // We can be in one of these 3 states
    static final int NONE = 0;
    static final int DRAG = 1;
    /*static final int ZOOM = 2;*/
    private static final int ZOOM = 2;
    int mode = NONE;

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    String savedItemClicked,scholid,tehsilo ,distrcto ,province ,sgtsID ;
    SharedPreferences preferences;



    public Stage2RecyclerViewAdapterViewCompleteList(Context mCtx, List<ModeClassCompleteListStage2> searcList)
    {
        this.mCtx = mCtx;
        this.searcList = searcList;
    }


    @Override
    public Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.carview_introcomplete, null);
        return new Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder holder, final int position)

    {
        final ModeClassCompleteListStage2 model = searcList.get(position);



        holder.adres.setText(model.getDetailsadres());
        holder.scholname.setText(model.getScholname());
        holder.district.setText(model.getDistrict());
        holder.city.setText(model.getTehsil());

       /* Glide.with(mCtx)
                .load(model.getImage1())
                .into(holder.image);*/

        if(model.getStg_id().equals("0")){
            holder.edit.setText("Add");

        }
        else {

            holder.edit.setText("Update");
        }






        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                /* String mtoid = searcList.get(position).get();*/
                 scholid = searcList.get(position).getScholid();
                 tehsilo = searcList.get(position).getTehsil();
                 distrcto = searcList.get(position).getDistrict();
                 province = searcList.get(position).getProvince();
                 sgtsID = searcList.get(position).getStg_id();
                 preferences = PreferenceManager.getDefaultSharedPreferences(mCtx);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("scholid", scholid);
                editor.putString("thsillo", tehsilo);
                editor.putString("distrctto", distrcto);
                editor.putString("prov", province);
                editor.putString("sgts",sgtsID);
                editor.apply();

                if(holder.edit.getText().toString().equals("Add")) {
                    Intent intent = new Intent(mCtx.getApplicationContext(), FormStage2.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                    mCtx.getApplicationContext().startActivity(intent);
                }
                else {

                    Intent intent = new Intent(mCtx.getApplicationContext(), FormToUpdateData.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                    mCtx.getApplicationContext().startActivity(intent);
                }


            }


        });




    }


    @Override
    public int getItemCount() {
        /*if (searcList.size() == 0) {
            .setVisibility(View.INVISIBLE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
        }*/
        return searcList.size();
    }

    class SearchViewHolder extends RecyclerView.ViewHolder {

        /*TextView movingitem, pickup, dropoff,currentbid,date,edit,delete,acceptbid,message,invitetransport,viewquote,similarjob,mesagecount,simlarcount;
        ImageView image,largeimage,editimage;
        CardView cardView;
        ImageView delicon,similaricon,inviteicon,mainimageicon,mesageicon;*/
        TextView adres, city, scholname, principalname, secondpersonname, entryno,district;
        Button edit;


        public SearchViewHolder(View itemView)

        {
            super(itemView);


            adres = itemView.findViewById(R.id.addrestxt);
            scholname = itemView.findViewById(R.id.scholnametxt);
            edit= itemView.findViewById(R.id.uploadbtn);
            district = itemView.findViewById(R.id.tehsilname);
            city = itemView.findViewById(R.id.citytxt);
          /*  movingitem = itemView.findViewById(R.id.MovingItemServlivep);
            pickup = itemView.findViewById(R.id.pickupservlivep);
            dropoff = itemView.findViewById(R.id.DropOffServlivep);
            image = itemView.findViewById(R.id.imagecardlivep);
            currentbid = itemView.findViewById(R.id.BidServlivep);
            date = itemView.findViewById(R.id.dateservlivep);
            edit = itemView.findViewById(R.id.editjob);
            delete = itemView.findViewById(R.id.deletejob);
            //   similarjob = itemView.findViewById(R.id.similarjobtext);
            acceptbid  = itemView.findViewById(R.id.biddetailslivep);
            viewquote  = itemView.findViewById(R.id.offerpricelivep);
            //  invitetransport = itemView.findViewById(R.id.invitetransportertext);
            mesagecount  = itemView.findViewById(R.id.countermsgs);
            // simlarcount = itemView.findViewById(R.id.countersimlar);
            editimage= itemView.findViewById(R.id.imageView98);

            mesageicon= itemView.findViewById(R.id.imagy98);

            delicon= itemView.findViewById(R.id.imagy971);

            cardView = itemView.findViewById(R.id.markeListCard);

            //  similaricon= itemView.findViewById(R.id.imagy981);

            // inviteicon= itemView.findViewById(R.id.ima98);

            mainimageicon= itemView.findViewById(R.id.imagecardlivep);*/
        }


    /*    public void setFilter(List<UserInfo> newList){
            data=new ArrayList<>();
            data.addAll(newList);
            notifyDataSetChanged();
        }*/

    }




}

标签: androidandroid-layoutandroid-fragmentsandroid-recyclerview

解决方案


推荐阅读