почему CardView вызывает замедление процесса прокрутки? - PullRequest
0 голосов
/ 25 апреля 2019

Я использовал listView без пользовательского макета, который имеет Линейный макет как родительский макет. Прокрутка была в порядке, тогда мне нужно реализовать CardView для повышения и закругления угла, после реализации Прокрутка списка становится медленной. Более того, она также замедляет Процесс RecyclerView. вот мой 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>

вот мой код адаптера

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;

}

}

Ответы [ 2 ]

0 голосов
/ 25 апреля 2019

Cardview, вероятно, является просто «переломным моментом» для того, сколько графического контента может быть использовано неоптимизированным способом без отставания. Как уже упоминалось в некоторых комментариях, вы должны использовать 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;
}

Вы можете узнать больше об этом здесь

Кроме того, для оптимизации производительности следует использовать RecyclerView, а не ListView.

0 голосов
/ 25 апреля 2019

Постарайтесь свести к минимуму иерархию представления и исключить сложные операции из-за привязки.

...