1

I was using listView without with the custom layout which is having Linear layout as parent layout. Scrolling was fine, Then I need to implement CardView for elevation and round corner, After implementation list Scrolling become slow.Moreover it is also slowing down the RecyclerView Process. here is my xml.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/WhiteSmoke"
    android:layout_margin="5dp"
   android:orientation="vertical">



   <android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="12dp"
    card_view:cardElevation="6dp">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/listviewback"
        android:orientation="vertical"
        android:padding="5dip">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/custom_listviewIV"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:src="@drawable/avatar2"

                />

            <TextView
                android:id="@+id/custom_listviewMainTv"
                android:layout_width="180dip"
                android:layout_height="45dip"
                android:layout_marginLeft="2dip"
                android:layout_toLeftOf="@+id/custom_listviewLinearLay"
                android:layout_toRightOf="@+id/custom_listviewIV"

                android:maxLines="2"
                android:paddingTop="2dip"
                android:text="Goverment  "
                android:textColor="@color/GreyDark"
                android:textSize="15sp"
                android:textStyle="bold"/>

            <LinearLayout
                android:id="@+id/custom_listviewLinearLay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="2dip"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/custom_listviewRating"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_gravity="right"
                    android:layout_marginBottom="2dp"
                    android:background="@drawable/tv_round"
                    android:backgroundTint="@color/primary_dark"

                    android:gravity="center"
                    android:text="4.2/5"
                    android:textColor="@color/white"
                    android:textSize="13sp" />


            </LinearLayout>

        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="5dip"
            android:background="@color/GreyLight" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dip"
            android:orientation="vertical"
            android:paddingLeft="10dip"
            android:paddingTop="1dip"
            android:paddingRight="10dip"

            >

            <TextView
                android:id="@+id/custom_listviewFirstTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dip"

                android:text="Fsc Pre Engineering1"
                android:textColor="@color/greyligher"
                android:textSize="13sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/custom_listviewSecondTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Fsc Pre Engineering2"
                android:textColor="@color/greyligher"
                android:textSize="13sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/custom_listviewThirdTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Fsc Pre Engineering3"
                android:textColor="@color/greyligher"
                android:textSize="13sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/custom_listviewFourthTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="more+"
                android:textColor="@color/black"
                android:textSize="13sp"
                android:visibility="gone" />

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

here is my adapter code

public class CustomAdaptor extends BaseAdapter {

private ArrayList<Data> myDataList;
private Context context;
LinearLayout linearLayout;

public CustomAdaptor(ArrayList<Data> mylist, Context context) {
    this.myDataList = mylist;
    this.context = context;
}

public int getCount() {
    return myDataList.size();
}

public Object getItem(int position) {
    return myDataList.get(position);
}

public long getItemId(int position) {
    return position;
}


public View getView(final int position, View convertView, ViewGroup parent) {

    View view = View.inflate(context, R.layout.custom_listview, null);
    linearLayout= view.findViewById(R.id.layout);
    TextView collegeName = view.findViewById(R.id.custom_listviewMainTv);
    TextView rating =  view.findViewById(R.id.custom_listviewRating);
    ImageView country =  view.findViewById(R.id.custom_listviewIV);
    LinearLayout ll= view.findViewById(R.id.custom_listviewLinearLay);
    TextView firstTv=  view.findViewById(R.id.custom_listviewFirstTv);
    TextView secondTv=  view.findViewById(R.id.custom_listviewSecondTv);
    TextView thirdTv=  view.findViewById(R.id.custom_listviewThirdTv);
    TextView fourthTv=  view.findViewById(R.id.custom_listviewFourthTv);

    collegeName.setText(myDataList.get(position).getName());
    rating.setText("" + myDataList.get(position).getRating());
    Double n=0.0;
    if (n==myDataList.get(position).getRating()) {
        ll.setVisibility(View.GONE);
    }

    if(!myDataList.get(position).getCourseList().isEmpty()) {
        if (!myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName().equals("")) {
            firstTv.setVisibility(View.VISIBLE);
            firstTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName());

        }
        if (myDataList.get(position).getCourseList().get(0).lst.size()>1) {
            if (!myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName().equals("")) {
                secondTv.setVisibility(View.VISIBLE);
                secondTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName());

            }
        }
        if (myDataList.get(position).getCourseList().get(0).lst.size()>2) {
            if (!myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName().equals("")) {
                thirdTv.setVisibility(View.VISIBLE);
                thirdTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName());

            }
        }
        if (myDataList.get(position).getCourseList().size()>1) {
            fourthTv.setVisibility(View.VISIBLE);
            fourthTv.setText("+More");

        }else if(myDataList.get(position).getCourseList().get(0).lst.size()>3) {
            fourthTv.setVisibility(View.VISIBLE);
            fourthTv.setText("+More");
        }
        String img = "" + myDataList.get(position).getImage();
        img = img.replaceAll(" ", "%20");
        Glide.with(context).load(img).into(country);

        view.setTag(myDataList.get(position).getId());
    }
    else {
        linearLayout.setVisibility(View.GONE);
    }


    return view;

}

}

Irfan Yaqub
  • 140
  • 10
  • Add your Adapter code as well – Ashish Kudale Apr 25 '19 at 10:39
  • Try not make any complex operations on binding data to the view. – Dmytro Batyuk Apr 25 '19 at 11:07
  • @DmytroBatyuk bro there is not much complex operation or any loop .. everything was good before applying CardView. – Irfan Yaqub Apr 25 '19 at 12:18
  • @AshishKudale i update the code with adapter code. – Irfan Yaqub Apr 25 '19 at 12:22
  • `findViewById` and `inflate` are expensive operations, every time you swipe your listview it gets called that's why you need to make your own ViewHolder for view caching. A good alternative is to dump `listview` and start using `RecylerView` instead. – Enzokie Apr 25 '19 at 12:23
  • @Enzokie bro i told you before i used it in RecyclerView too. But result is same. ListView and RecyclerView Both lags when use the CardView. – Irfan Yaqub Apr 25 '19 at 12:43
  • @MR.Robot Since I don't see your `RecyclerView` code I can only assume you are using findviewbyid inside in the wrong place. Make sure the inflated views are assigned to a member variable within ViewHolder. Never call `findViewById` within `onCreateViewHolder` in the case of `RecyclerView`. Also you have too many `get` calls in your code, try to make your datasource less deep as much as possible. – Enzokie Apr 25 '19 at 12:48

2 Answers2

0

Try to minimize your view hierarchy and reduce complex operations from binding.

Saad
  • 28
  • 4
0

Cardview is probably just the "breaking point" for how much graphic content that cna be used in an unoptimized way without lagging. As mentioned in some comments you should be using a ViewHolder, here is a simple example of using a viewholder:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_entry, null);
        holder = new ViewHolder();
        holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
        holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
        holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    Person person = getItem(position);

    holder.nameTextView.setText(person.getName());
    holder.surnameTextView.setText(person.getSurname());
    //holder.personImageView.setImageBitmap(person.getImage());

    return convertView;
}

you can read more about it here

Additionally you should be using RecyclerView and not ListView, to optimize performance.

Joachim Haglund
  • 775
  • 5
  • 15
  • bro in other layout i am using recyclerView, in which 6 types of layouts are inflating . but same performance is there. – Irfan Yaqub Apr 25 '19 at 14:23
  • The viewholder is the main point not recyclerview. using recyclerview instead of listview is just additional optimization – Joachim Haglund May 07 '19 at 13:25