我正在使用RecyclerView
和StaggeredGridLayoutManager
来展示外观略有不同的商品。我在JakeWharton(link)使用AspectRatioImageView
的项目中,在Picasso
实际下载并显示图像之前维护imageView大小。 RV(RecyclerView)由无限滚动模式的服务器数据填充。
问题是(如附图所示),在scolling时,RV的顶部和底部会出现一些间隙(图片中有时会有项目在列之间移动!)(截屏很难: d)
编辑2:我忘了一件非常重要的事情,就是我对所有项目使用相同的布局,但是对于适配器项目布局的某些部分会将其可见性更改为{ {1}}或反之亦然。
我的问题:为什么RV表现得像这样?难道我做错了什么?这在RV中是正常的吗?
代码很大,我不知道哪个部分对此有用。如果你可以缩小原因,我会在这里放一些代码。
编辑:与问题相关的代码部分
的xml:
Gone
初始化视图:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_texture"
android:padding="@dimen/padding_lvlhalf"
android:scrollbarStyle="outsideOverlay"/>
我的自定义适配器:
@Bind(R.id.recycler_view) RecyclerView mRecyclerView;
.
.
.
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(columnCount, direction));
mRecyclerView.setAdapter(
new RecyclerViewAdapter<>(ctx, new ViewFiller<Post, DashboardPostViewHolder>() {
@Override
public void fill(Post post, DashboardPostViewHolder holder) {
holder.fill(post);
}
@Override
public DashboardPostViewHolder getViewHolder(View view) {
return new DashboardPostViewHolder(view, receiverName);
}
@Override
public int getLayoutID() {
return R.layout.post_dashboard;
}
});)
public RecyclerViewAdapter(Context context, ViewFiller filler) {
super();
this.filler = filler;
mLayoutInflater = LayoutInflater.from(context);
items = new ArrayList<>();
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = mLayoutInflater.inflate(filler.getLayoutID(), parent, false);
return filler.getViewHolder(v);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
filler.fill(getItem(position), holder);
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
public T getItem(int position){
return items.get(position);
}
public void addItem(int position, T item){
items.add(position, item);
notifyItemInserted(position);
}
public void addItem(T item){
items.add(item);
notifyItemInserted(items.size() - 1);
}
public void addItems(List<T> data){
int oldSize = items.size();
items.addAll(data);
notifyItemRangeInserted(oldSize, data.size());
}
public void addItemsToListHead(List<T> data){
items.addAll(0, data);
notifyItemRangeInserted(0, data.size());
}
public void resetData(List<T> data) {
this.items = data;
notifyDataSetChanged();
}
public void removeItem(int position){
items.remove(position);
notifyItemRemoved(position);
}
public void clear(){
this.items.clear();
notifyDataSetChanged();
}
public List<T> getItems(){
return items;
}
界面
ViewFiller
编辑2(续):我的项目xml以及我如何填充它们。
public interface ViewFiller<D, VH extends BaseViewHolder>{
void fill(D data, VH holder);
VH getViewHolder(View view);
@LayoutRes int getLayoutID();
}
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_lvlhalf"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/post"
android:background="@color/poinila_background">
<TextView
android:layout_gravity="right"
android:text="@string/test_title"
android:layout_margin="@dimen/margin_lvl1"
style="@style/match_wrap.large_text.centered_right"
android:id="@+id/post_title"
android:visibility="gone"/>
<!--Post Image-->
<com.shaya.poinila.android.presentation.view.AspectRatioImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:contentDescription="@string/post_content_description"
android:id="@+id/post_image"
android:visibility="gone"
app:aspectRatioEnabled="true"
app:dominantMeasurement="width"
/>
<!--Content-->
<TextView
android:id="@+id/post_content"
style="@style/match_wrap.medium_text.three_line.end_ellipsize"
android:layout_margin="@dimen/margin_lvl1"/>
<!--android:text="@string/test_long"-->
<include layout="@layout/horizontal_line"
/>
<!--Post Author-->
<include android:id="@+id/post_author"
layout="@layout/circle_image_title_subtitle_reverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_lvl1"/>
<include layout="@layout/horizontal_line"
/>
<!--Post Stats (favs, comments, etc) -->
<include android:id="@+id/post_stats"
layout="@layout/stats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_lvlhalf"
android:layout_marginBottom="@dimen/margin_lvlhalf"/>
<include layout="@layout/horizontal_line"/>
<!--Post Collection Info-->
<include layout="@layout/rounded_image_title_subtitle_reverse"
android:id="@+id/post_collection"/>
</LinearLayout>
</android.support.v7.widget.CardView>
上课
PostViewHolder
很抱歉很长的帖子!
答案 0 :(得分:0)
这样做,网格项之间的间距保持不变
private boolean flagDecoration = false;
if(!flagDecoration)
{
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(),(R.dimen.spacing));
recyclerView.addItemDecoration(itemDecoration);
flagDecoration = true;
}