gpt4 book ai didi

android - 如何在 RecyclerView 中实现部分可见的下一个项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:25 25 4
gpt4 key购买 nike

我使用 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";
}
}
}

最佳答案

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

@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;
}
}

关于android - 如何在 RecyclerView 中实现部分可见的下一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com