简体   繁体   English

ListView防止滚动时刷新

[英]ListView prevent refresh while scrolling

In firebase chat example application, when data updated(change, remove, move, add) listview scrolling to down. 在Firebase聊天示例应用程序中,当数据更新(更改,删除,移动,添加)列表视图向下滚动时。 Source: https://github.com/firebase/AndroidChat 资料来源: https : //github.com/firebase/AndroidChat

How to prevent scroll while 30 seconds after last scrollStateChanged? 如何防止在上一次scrollStateChanged之后30秒内滚动?

        msgListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            scroll_idle_time = System.currentTimeMillis();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }
    });

    public static boolean canScroll() {
    return scroll_idle_time + 30000 < System.currentTimeMillis();
}

将此代码放在getView Function的第一行中:

if (convertView != null) return convertView;

I have solved with following code 我已经用以下代码解决了

 public class CustomAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<SectionsDetailsPOJO> alSectionDetails;

    public CustomAdapter(Activity a, ArrayList<SectionsDetailsPOJO> d) {
        activity = a;
        alSectionDetails = d;
    }


    @Override
    public int getViewTypeCount() {
        if(alSectionDetails.size()>0) {
            return alSectionDetails.size();
        }
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getCount() {
        if (alSectionDetails.size() <= 0)
            return 1;
        return alSectionDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        final LayoutInflater inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_view, null);

            holder = new ViewHolder();
            holder.SectionNo = (TextView) convertView.findViewById(R.id.textView1);
            holder.SectionHeader=(TextView)convertView.findViewById(R.id.textView2);

            holder.SectionNo.setText(alSectionDetails.get(position).getSection_no());
            holder.SectionHeader.setText(alSectionDetails.get(position).getSection_header());

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
            convertView.setTag(holder);
        }
        return convertView;
    }

    public  class ViewHolder {
        public TextView SectionNo;
        public TextView SectionHeader;
    }
}

Solution found at: Retaining position in ListView after calling notifyDataSetChanged 在以下位置找到解决方案: 调用notifyDataSetChanged后在ListView中保留位置

//Save where you last were in the list.
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// Call to notify, or updated(change, remove, move, add)
notifyDatasetChanged(); 

//Prevents the scroll to the new item at the new position
mList.setSelectionFromTop(index, top);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM