我正在尝试制作图库应用。在显示图像时,我使用了GridLayoutManager来显示图像,并且在显示Header视图时我还增加了span列(通过使用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"/>
答案 0 :(得分:0)
尝试更改包含此表单中的RecyclerView的布局结构
<ScrollView >
<LinearLayout >
<View > <!-- upper content -->
<RecyclerView > <!-- with custom layoutmanager -->
</LinearLayout >
当滚动时出现一点延迟时,我会使用这种结构。