我的Activity中有一个片段,结构如下:
<android.support.v7.widget.RecyclerView android:id="@+id/cafeRecyclerView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
它只有一个RecyclerView。它有一个带有以下代码的适配器:
public class InfinityFragmentRecyclerAdapter extends RecyclerView.Adapter implements HasDatabase
{
protected JsonDatabase jsonDatabase = new JsonDatabase();
protected Context mContext;
protected int arrangeMode;
public InfinityFragmentRecyclerAdapter(Context context, int viewMode)
{
mContext = context;
arrangeMode = viewMode;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View view;
if (arrangeMode == SupportsGridAndListArrangement.GRID_VIEW)
{
view = LayoutInflater.from(viewGroup.getContext()).inflate
(R.layout.grid_advertisement_layout, null);
}
else
{
view = LayoutInflater.from(viewGroup.getContext()).inflate
(R.layout.list_advertisement_layout, null);
}
return new ItemHolder(view, mContext, arrangeMode);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i)
{
AdItemPassiveDataSet adItemPassiveDataSet = (AdItemPassiveDataSet) jsonDatabase.getDataSetAt(i);
Picasso.with(mContext).load(adItemPassiveDataSet.getItemThumbnailAddress()).into
(((ItemHolder) viewHolder).imageView);
((ItemHolder) viewHolder).adID = adItemPassiveDataSet.getItemID();
((ItemHolder) viewHolder).userID = adItemPassiveDataSet.getUserID();
((ItemHolder) viewHolder).userRole = adItemPassiveDataSet.getUserRole();
((ItemHolder) viewHolder).price.setText(adItemPassiveDataSet.getItemPrice(), false);
((ItemHolder) viewHolder).title.setText(adItemPassiveDataSet.getItemTitle(), false);
((ItemHolder) viewHolder).state.setText(adItemPassiveDataSet.getItemState(), false);
((ItemHolder) viewHolder).date.setText(adItemPassiveDataSet.getItemTime(), false);
if (arrangeMode == SupportsGridAndListArrangement.LIST_VIEW)
{
((ItemHolder) viewHolder).description.setText(adItemPassiveDataSet.getItemDescription());
StaggeredGridLayoutManager.LayoutParams layoutParams =
new StaggeredGridLayoutManager.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
viewHolder.itemView.setLayoutParams(layoutParams);
}
}
@Override
public int getItemCount()
{
if (jsonDatabase != null)
{
return jsonDatabase.getCount();
}
else
{
return 0;
}
}
@Override
public JsonDatabase getDatabase()
{
return jsonDatabase;
}
public void append(Object newJsonDatabase)
{
int currentCount = 0;
if (jsonDatabase != null)
{
currentCount = jsonDatabase.getCount();
}
int newCount = ((JsonDatabase) newJsonDatabase).getCount();
jsonDatabase.appendAtEnd((JsonDatabase) newJsonDatabase);
for (int i = currentCount; i < (currentCount + newCount); i++)
{
notifyItemInserted(i);
}
}
}
我也以这种方式设置我的片段:
recyclerView = (RecyclerView) findViewById(R.id.cafeRecyclerView);
recyclerViewAdapter = new InfinityFragmentRecyclerAdapter(this, SupportsGridAndListArrangement.GRID_VIEW);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
每次从服务器收到新数据时,我都会以CardView
的方式将它们添加到RecyclerView中:
recyclerViewAdapter.append(newJsonDatabase);
一切都很好!我可以下载数据,将它们添加到回收站视图等等,但问题是回收站视图不能流畅地滚动。当我在回收商视图中的卡片视图的顶部或底部到达回收者视图的顶部时,它会以某种方式粘贴并跳跃一些像素!我真的很困惑。有什么想法吗?任何帮助将不胜感激。
答案 0 :(得分:0)
尝试使用 notifyItemRangeInserted()而不是notifyItemInserted()
如果我理解正确,您的代码应该是这样的
notifyItemInserted(currentCount, currentCount + newCount);