gpt4 book ai didi

java - RecyclerView 滚动时扩展项目之间的空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:05 25 4
gpt4 key购买 nike

我刚刚完成我的 RecyclerView对于我的 Android Application ,但是在测试时,我发现了一个我不明白的奇怪错误。当 Activity 启动时,Recycler 中的项目 View 位于 RecyclerView 中在正确的距离。但是,一旦我开始滚动,它们就会彼此分开,直到一次只能看到一个项目,您必须滚动 distance almost equivalent在看到下一个项目之前到屏幕的那个。

这是错误的样子:

滚动 Activity 午餐前

enter image description here

滚动回收 View 后

enter image description here

如果您知道可能导致此问题的原因,我们将不胜感激。

这是 Activity 中的 RecyclerView 代码:

private ArrayList<String> imagesUrlListThumb, imagesUrlListFull = new ArrayList<String>();
private RecyclerAdapter recyclerAdapter;
private String urlRecyclerThumb = "";
private RecyclerView recyclerView;
private ImageView imgCurRecyclerView;

imagesUrlListThumb = produit.getImgUrlThumbMul();
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), imagesUrlListThumb);
recyclerView = (RecyclerView) findViewById(R.id.content_product_detail_recycer_view);
RecyclerView.LayoutManager recyclerLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(recyclerLayoutManager);
recyclerView.setAdapter(recyclerAdapter);
urlRecyclerThumb = imagesUrlListThumb.get(0);
RecyclerItemClickSupport.addTo(recyclerView).setOnItemClickListener(new RecyclerItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView rv, int pos, View view) {
urlRecyclerThumb = imagesUrlListThumb.get(pos);
Picasso.with(getApplicationContext()).load(urlRecyclerThumb).fit().into(imgCurRecyclerView);
}
});

回收器适配器:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{

private List<String> urlThumbImg;
private Context context;

public RecyclerAdapter(Context ctx, List<String> urls){
this.urlThumbImg = urls;
this.context = ctx;
}

@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_slideshow, parent, false);
return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position){
String current = urlThumbImg.get(position);
Picasso.with(context).load(current).fit().into(holder.myImgView);
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView myImgView;

public MyViewHolder(View view){
super(view);
myImgView = (ImageView) view.findViewById(R.id.imageView_slide);
}
}
}

这是我关于回收站 View 的 xml 布局:

<!-- RECYCLER VIEW -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:orientation="vertical">

<ImageView
android:id="@+id/content_product_detail_recycer_view_cur_image"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:background="@android:color/black" />

<android.support.v7.widget.RecyclerView
android:id="@+id/content_product_detail_recycer_view"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@android:color/darker_gray">

</android.support.v7.widget.RecyclerView>

</LinearLayout>

recyclerview 中项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView_slide"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="@android:color/darker_gray" />

最佳答案

提供您的 Layout 和 RecyclerView 代码以帮助调试而不是猜测。

好的,问题来了:

With the release 2.3.0 there is an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

由于这一变化,请务必仔细检查项目 View 的布局参数:以前忽略的布局参数(例如滚动方向上的 MATCH_PARENT)现在将得到充分考虑。

基本上你需要删除 LinearLayout

<ImageView
android:id="@+id/imageView_slide"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="@android:color/darker_gray" />

LinearLayout 宽度中的 match_parent 导致每个项目以一张图像和剩余空间适合整个屏幕。

如果你坚持使用LinearLayout,把width和height都设置成wrap_content

关于java - RecyclerView 滚动时扩展项目之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38224039/

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