垂直滚动列表与水平可滚动行的循环器视图

时间:2015-06-30 04:41:21

标签: android android-layout android-recyclerview

我想要一个RecyclerView,其中我们有垂直可滚动的项目列表。 从这个可滚动的项目列表中,一些应该具有在水平方向上滚动的能力。 如下图所示 enter image description here

有人可以指导我怎么做吗?

谢谢。

2 个答案:

答案 0 :(得分:5)

  

自定义LayoutManagers

     
      
  • StaticGridLayoutManager - 基于数据集的可变列数的2D滚动网格。可见(非再循环)视图的窗口是
      静态地确定。
  •   
  • DynamicGridLayoutManager - 2D滚动网格,其中可视视图窗口是动态确定的。结果导致视图较少   内存,但滚动性能是值得怀疑的。
  •   

我遇到了同样的问题,我找到了这个库。也许它会帮助你。 https://github.com/devunwired/recyclerview-playground

有关RecyclerView LayoutManager的更多详细信息:http://wiresareobsolete.com/2014/09/building-a-recyclerview-layoutmanager-part-1/

p / s:对于您的案例http://lucasr.org/2014/07/31/the-new-twowayview/

答案 1 :(得分:-1)

由于这似乎是一个常见问题,我想我会分享我对此的简单实现。使用RecyclerView很容易实现这一点。我在尝试使用设备相机拍摄照片时创建可滚动图像的水平列表时这样做了。我已粘贴适配器的相关部分。

我使用了一个使用LinearLayoutManager的RecyclerView,其方向设置为水平。

适配器本身非常简单(请注意,相关部分仅在此处):

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ebay.lockers.R;
import com.ebay.lockers.utils.AsyncDrawable;
import com.ebay.lockers.utils.BitmapUtils;
import com.ebay.lockers.utils.BitmapWorkerTask;

import java.io.File;
import java.util.List;

/**
 * Created by Sunil on 6/17/2016.
 */
public class ImagesHorizontalListAdapter extends RecyclerView.Adapter<ImagesHorizontalListAdapter.ImagesViewHolder> {

    private Context context;
    private List<File> imageFiles;

    public ImagesHorizontalListAdapter(Context context, List<File> imageFiles) {
        this.context = context;
        this.imageFiles = imageFiles;
    }

    @Override
    public ImagesHorizontalListAdapter.ImagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(context).inflate(R.layout.simple_image_view, parent, false);
        ImagesViewHolder viewHolder = new ImagesViewHolder(layout);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ImagesHorizontalListAdapter.ImagesViewHolder holder, final int position) {
        int availableWidth = context.getResources().getDisplayMetrics().widthPixels;
        int imageWidth = availableWidth/4; // Number of images to be shown by default
        int imageHeight = imageWidth*4/3;
        final int minDimenForScaling = Math.min(imageWidth, imageHeight);

        holder.image.post(new Runnable() {
            @Override
            public void run() {
                loadBitmap(imageFiles.get(position), holder.image, minDimenForScaling, minDimenForScaling);
            }
        });
    }

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

    public void loadBitmap(File file, ImageView imageView, int reqWidth, int reqHeight) {
        if(BitmapUtils.cancelPotentialWork(file, imageView)) {
            final BitmapWorkerTask task = new BitmapWorkerTask(imageView, reqWidth, reqHeight);
            // The second Bitmap parameter is a placeholder image
            // Should consider animation; TO DO --
            final AsyncDrawable asyncDrawable = new AsyncDrawable(context.getResources(), null, task);
            imageView.setImageDrawable(asyncDrawable);
            task.execute(file);
        }
    }

    public static class ImagesViewHolder extends RecyclerView.ViewHolder {
        // each data item is an image
        ImageView image;

        public ImagesViewHolder(View layout) {
            super(layout);
            this.image = (ImageView) layout.findViewById(R.id.image);
        }
    }
}