如何在RecyclerView

时间:2016-11-14 08:56:52

标签: android android-recyclerview

我使用onFling一次滚动一个项目,并希望只有一个项目在页面上完全可见,而下一个项目可以部分显示。

enter image description here

我想要这个,我怎样才能在RecyclerView中实现这个目标。这是适配器代码,只是代替cardView(图中显示),它的imageView:

public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

private final ArrayList<Movie> mValues;

public SimpleItemRecyclerViewAdapter(ArrayList<Movie> items) {
    mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_content, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.mItem = mValues.get(position);
    Context context = holder.imageView.getContext();
    String logo = mValues.get(position).getPosterPath();
    Picasso.with(context).load(logo).into(holder.imageView);

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public final View mView;
    public ImageView imageView;
    public Movie mItem;
    MainActivity mainActivity = new MainActivity();

    public ViewHolder(View view) {
        super(view);
        mView = view;
        imageView = (ImageView) view.findViewById(R.id.image_movie_poster);

        android.view.ViewGroup.LayoutParams params = imageView.getLayoutParams();
        params.height = (int) (mainActivity.height * 0.7);
        imageView.setLayoutParams(params);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public String toString() {
        return "Blah";
    }
}
}

2 个答案:

答案 0 :(得分:1)

这适用于我的水平回收视图。您可以更改为垂直布局管理器。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    final SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);

    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.setAdapter(new TestAdapter());
        }
    });
}

private class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        final View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.test_card, parent, false);
        int width = recyclerView.getWidth();
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = (int)(width * 0.8);
        view.setLayoutParams(params);
        return new TestViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        TestViewHolder holder = (TestViewHolder) viewHolder;
        holder.numText.setText(((Integer) i).toString());
    }

    @Override
    public int getItemCount() {
        return 20;
    }
}

答案 1 :(得分:1)

您可以使用自定义布局管理器来控制每个项目的显示方式

class RatioLayoutManager constructor(
context: Context?,
@RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL,
reverseLayout: Boolean = false) : LinearLayoutManager(context, orientation, reverseLayout) {

var ratio = 0.7f

private val horizontalSpace get() = width - paddingStart - paddingEnd

private val verticalSpace get() = width - paddingTop - paddingBottom

override fun generateDefaultLayoutParams() =
    scaledLayoutParams(super.generateDefaultLayoutParams())

override fun generateLayoutParams(lp: ViewGroup.LayoutParams?) =
    scaledLayoutParams(super.generateLayoutParams(lp))

override fun generateLayoutParams(c: Context?, attrs: AttributeSet?) =
    scaledLayoutParams(super.generateLayoutParams(c, attrs))

private fun scaledLayoutParams(layoutParams: RecyclerView.LayoutParams) =
    layoutParams.apply {
        when (orientation) {
            HORIZONTAL -> width = (horizontalSpace * ratio + 0.5).toInt()
            VERTICAL -> height = (verticalSpace * ratio + 0.5).toInt()
        }
    }

}