【问题标题】:Header view within GridView with Reyclerview causing lagGridView 中的标题视图与 Recyclerview 导致滞后
【发布时间】:2018-05-19 10:12:54
【问题描述】:

我正在尝试制作一个图库应用。显示图像时,我使用 GridLayoutManager 显示图像,并且在显示标题视图时还增加了跨度列(通过使用 setSpanSizeLookup)。但是当我滚动浏览大约 500 张图像的列表时,它的滚动似乎有点滞后。如果我删除 setSpanSizeookup 代码而不是它的工作顺利。请告诉我如何解决这个问题。

这是我的活动 onCreate 方法,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_image);

    getImagesPath(this);

    send = findViewById(R.id.send);
    dialog = new ProgressDialog(this);
    recyclerView = findViewById(R.id.recycler);
    GridLayoutManager manager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);

    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (adapter.getItemViewType(position)) {
                case GalleryAdapter.VIEW_TYPE_HEADER:
                    return 4;
                case GalleryAdapter.VIEW_TYPE_ITEM:
                    return 1;
                default:
                    return 1;
            }
        }
    });

    recyclerView.setLayoutManager(manager);
    recyclerView.addItemDecoration(new GridItemDecor(3));
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(25);
    recyclerView.setDrawingCacheEnabled(true);
    recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    adapter = new GalleryAdapter(this, imageList);
    recyclerView.setAdapter(adapter);

    dialog.setCanceledOnTouchOutside(false);
    send.setOnClickListener(this);

    deviceName = getDeviceName();
}

这是我的适配器类,

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.GalleryViewHolder> {

public static final int VIEW_TYPE_HEADER = 2;
public static final int VIEW_TYPE_ITEM = 1;
private Context context;
private ArrayList<String> imageList;
private ArrayList<String> uriList = new ArrayList<>();
private ArrayList<String> selectAllList = new ArrayList<>();
private ArrayList<String> videoList = new ArrayList<>();
private VideoThumbLoader thumbLoader = new VideoThumbLoader();

public GalleryAdapter(Context context, ArrayList<String> imageList) {
    this.context = context;
    this.imageList = imageList;
}

@NonNull
@Override
public GalleryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;
    if (viewType == 1)
        view = LayoutInflater.from(context).inflate(R.layout.ly_single_image, parent, false);
    else
        view = LayoutInflater.from(context).inflate(R.layout.ly_single_date, parent, false);

    return new GalleryViewHolder(view, viewType);
}

@Override
public void onBindViewHolder(@NonNull GalleryViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case VIEW_TYPE_ITEM: {
            holder.overlay.setVisibility(uriList.contains(imageList.get(holder.getAdapterPosition())) ? View.VISIBLE : View.GONE);
            holder.video.setVisibility(videoList.contains(imageList.get(holder.getAdapterPosition())) ? View.VISIBLE : View.GONE);

            Glide.with(holder.image.getContext())
                    .clear(holder.image);

            File file = new File(imageList.get(holder.getAdapterPosition()));
            if (getMimeType(file).contains("video")) {
                holder.video.setVisibility(View.VISIBLE);
                videoList.add(imageList.get(holder.getAdapterPosition()));

                holder.image.setTag(imageList.get(holder.getAdapterPosition()));
                thumbLoader.showThumbByAsynctack(imageList.get(holder.getAdapterPosition()), holder.image);
            } else {
                Uri uri = Uri.parse(imageList.get(holder.getAdapterPosition()));
                Glide.with(holder.image.getContext())
                        .load(new File(uri.getPath()))
                        .apply(RequestOptions
                                .centerCropTransform())
                        .thumbnail(1f)
                        .into(holder.image);
            }
        }
        break;

        case VIEW_TYPE_HEADER: {
            holder.date.setText(imageList.get(holder.getAdapterPosition()));

            if (selectAllList.contains(imageList.get(position))) {
                holder.selectAll.setTag(R.drawable.ic_selectall_placeholder);
                holder.selectAll.setBackground(context.getDrawable(R.drawable.ic_selectall_placeholder));
            } else {
                holder.selectAll.setTag(R.drawable.ic_deselectall_placeholder);
                holder.selectAll.setBackground(context.getDrawable(R.drawable.ic_deselectall_placeholder));
            }
        }
        break;
    }
}

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

@Override
public int getItemViewType(int position) {
    File file = new File(imageList.get(position));
    if (file.exists())
        return VIEW_TYPE_ITEM;
    else
        return VIEW_TYPE_HEADER;
}

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

这是我的活动布局代码,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pixelolio.pixelolio.ui.activity.SelectImageActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/layout" />

这是我的单张图片布局,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

这里是单个日期(标题)的布局代码,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_margin="10dp">
<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/extra_small_text_size"
    android:textColor="@android:color/black"
    android:text="21 Sept"
    android:textStyle="bold"/>

<View
    android:id="@+id/selectAll"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:background="@drawable/ic_deselectall_placeholder"
    android:padding="5dp"
    android:layout_marginStart="10dp"/>

【问题讨论】:

  • 你能发布你的 xml 吗?
  • 代码更新@ClintPaul

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

尝试以这种形式更改由 RecyclerView 组成的布局结构,

<ScrollView >
<LinearLayout >
   <View > <!-- upper content -->
        <RecyclerView > <!-- with custom layoutmanager -->
</LinearLayout >

当滚动有一点延迟时,我会使用这种结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2017-07-15
    • 2019-02-09
    相关资源
    最近更新 更多