【问题标题】:ListView in android scroll very slow when they take a short placeandroid中的ListView在占位短时滚动非常慢
【发布时间】:2019-12-17 22:56:37
【问题描述】:

请,只有 3 个文本视图(没有图像)的列表视图在全屏显示高度和宽度时工作得非常好。 在我将其更改为屏幕高度的一半后,它的滚动变得非常缓慢。

listView 中行的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="16dp"
        android:layout_toStartOf="@id/timer_row">
            <TextView
                android:id="@+id/name_row"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="@dimen/main_title_size"
                android:textColor="@color/white"
                 />
            <TextView
                android:id="@+id/author_row"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/main_timer_size"
                android:textColor="@color/white"
                android:layout_below="@id/name_row"
                android:layout_marginTop="16dp"
                android:layout_alignParentStart="true"/>
    </RelativeLayout>
    <TextView
        android:id="@+id/timer_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/main_timer_size"
        android:layout_margin="16dp"
        android:padding="8dp"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

以及带有视图的适配器

public class MusicAdapter extends BaseAdapter {

    private ArrayList<Music> list;
    private LayoutInflater layoutInflater;

    MusicAdapter(Context context, ArrayList<Music> list) {
        this.list = list;
        layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.music_row, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            Music music = (Music) getItem(position);
            viewHolder.title.setText(music.getName());
            viewHolder.author.setText(music.getAuthor());
            viewHolder.timer.setText(music.getTimer());

            return convertView;
        }


        static class ViewHolder{
        TextView title,author,timer;
        ViewHolder(View v)
        {
            this.title = v.findViewById(R.id.name_row);
            this.author = v.findViewById(R.id.author_row);
            this.timer = v.findViewById(R.id.timer_row);
        }
        }
    }

【问题讨论】:

    标签: android performance listview scroll


    【解决方案1】:

    您应该尝试使用 RecyclerView 而不是 ListView:https://developer.android.com/guide/topics/ui/layout/recyclerview

    【讨论】:

    • 我之前确实使用过 RecyclerView,但它有些问题
    • 在不实际运行代码的情况下很难调试此类问题。您可以通过切换到RecyclerView 来提高性能,使用ConstraintLayout 而不是RelativeLayout 作为列表项,确保您的onBindViewHolder 尽可能轻量级(例如:不要使用MediaMetadataRetriever 来获取持续时间因为它很慢,...)。您可以使用分析器调试您的应用性能:developer.android.com/studio/profile/android-profiler
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多