我有一个RecyclerView,每个特定时期都添加项目.当添加对象时,如果列表项超过(允许说)500,则第一个项目将被删除,并且新项目将被添加.
如果RecyclerView不能再向下滚动!recyclelerView.canScrollVertically(1);那么在添加新项目之后,RecyclerView会将ScroolToPosition()平滑到最后一个位置.
哪里有问题?
好的,如果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); } } }