我有一个RecyclerView,它在每个特定时期添加项目。添加对象时,如果列表项超过(假设)500,那么将删除第一个项目并添加新项目。
如果RecyclerView无法向下滚动!recyclerView.canScrollVertically(1);
,则在添加新项目后,RecyclerView将smoothScroolToPosition()
移至最后位置。
问题出在哪里? 好吧,如果ReyclerView在移除旧物品时位于中间(不是底部,而不是顶部),它会突然跳起一些位置。我希望RecyclerView不会跳转位置并保持在顶部删除项目时的位置
我尝试使用layoutManager.setStackFromEnd(true);
,但没有运气
有什么建议吗?
一些代码(不相关的代码被删除)。当RecyclerView从中间显示列表项并按VOLUME_UP:
时,可以重现我的问题public class ActivityMain extends ActionBarActivity {
public static final int MAX_LOG_ITEMS = 500;
private RecyclerView mRecyclerView;
private AdapterLog mRecyclerAdapter;
private boolean mAutoScroll = true;
private DataReceiver mDataReceiver;
private Handler mLogHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what){
case DataReceiver.CAT_LOGS:
List<Log> catLogs = (List<Log>) msg.obj;
updateLogs(catLogs);
break;
case DataReceiver .CLEAR_LOGS:
if (mRecyclerAdapter.getItemCount() > MAX_LOG_ITEMS)
mRecyclerAdapter.removeFirstItems(mRecyclerAdapter.getItemCount() - MAX_LOG_ITEMS);
break;
case Logcat.REMOVE_LOGS:
mRecyclerAdapter.clear();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
mRecyclerAdapter = new AdapterLog();
mRecyclerView = (RecyclerView) findViewById(R.id.activity_main_recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerView.setOnScrollListener(new UIUtils.ScrollManager(toolbarContainer != null ?
toolbarContainer : toolbar){
@Override public void onScrolled(RecyclerView r, int dx, int dy) {
super.onScrolled(r, dx, dy);
mAutoScroll = !r.canScrollVertically(1);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_VOLUME_UP:
// mAutoScroll = false;
// mRecyclerView.scrollToPosition(0);
// if (mRecyclerAdapter.getItemCount() > MAX_LOG_ITEMS)
mRecyclerAdapter.removeFirstItems(mRecyclerAdapter.getItemCount() - 50);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
mAutoScroll = true;
mRecyclerView.scrollToPosition(mRecyclerAdapter.getItemCount() -1);
return true;
}
return false;
}
private void updateLogs(final List<Log> logList) {
final boolean scroll = mAutoScroll;
mRecyclerAdapter.addAll(logList);
if (scroll) mRecyclerView.smoothScrollToPosition(mRecyclerAdapter.getItemCount() - 1);
}
}
RecyclerAdapter:
public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
private final List<Log> mLogList;
public AdapterLog() {
this.mLogList = new ArrayList<Log>();
}
@Override
public AdapterLog.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem_log, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(getItem(position).getMessage());
holder.mTextView.setTextColor(getItem(position).getLevel().getColor());
}
@Override
public int getItemCount() {
return mLogList.size();
}
public Log getItem(int position) {
return mLogList.get(position);
}
public void addAll(List<Log> logList) {
mLogList.addAll(logList);
notifyDataSetChanged();
}
public void removeFirstItems(int count) {
for (int i=0; i<count; i++) mLogList.remove(0);
notifyDataSetChanged();
}
public void clear() {
mLogList.clear();
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.listitem_log_textview);
}
}
}
答案 0 :(得分:20)
您告诉您的观点是您更改了整个适配器。对于添加,删除和重新排序,请考虑使用以下方法:
notifyItemRangeChanged
notifyItemRangeInserted
notifyItemRangeRemoved
notifyItemMoved
notifyItemInserted
notifyItemChanged
notifyItemRemoved
因此,在您的情况下,您要移除顶级count
项:
notifyItemRangeRemoved(0,count)
应该做的工作。
答案 1 :(得分:0)
public class viewHolderFav extends RecyclerView.ViewHolder {
private ImageView image;
private TextView name;
public viewHolderFav( View itemView ) {
super( itemView );
image = ( ImageView ) itemView.findViewById( R.id.txt_image_fav );
name = ( TextView ) itemView.findViewById( R.id.txt_name_fav );
edite.setOnClickListener( new View.OnClickListener( ) {
@Override
public void onClick( View v ) {
remove( posts.get( getLayoutPosition() ) );
}
} );
}
public void remove(DataBoj item) {
int position = posts.indexOf(item);
posts.remove(position);
notifyItemRemoved(position);
}