listview 平滑移动到最后一条

 listview 平滑移动到最后一条


  mChatList.smoothScrollToPosition(mChatList.getCount() - 1);
  mChatList.smoothScrollToPositionFromTop(mChatListgetCount() - 1,0);
  次两种方法 可以平滑移动 情况是item高度基本一致的情况
  如果一个item很高 效果是没有的


  mChatList.setSelection(mChatList.getCount() - 1);
  次方法可以马上定位到最后一条 但是没有平滑的动作 很生硬


  平滑移动方法一


 @SuppressLint("NewApi")
    public  void DeterminedScrollTo(final ListView listView, final int index, int attempts ) {
        if (listView.getFirstVisiblePosition() != index && attempts < 10) {
            attempts++;
            listView.smoothScrollToPositionFromTop (index, 1, 100);
            final int att = attempts;
            listView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    DeterminedScrollTo (listView, index, att);
                    
                }
            }, 100);
        }
    }
    


    
  平滑移动方法二
    @SuppressLint("NewApi") 
    public static void smoothScrollToPosition(final AbsListView view, final int position, final int offset) {


        final View child = getChildAtPosition(view, position);


        if ((child != null) && ((child.getTop() == offset) ||
                ((child.getTop() > offset) && !view.canScrollVertically(1)) 
                || ((child.getTop() < offset) && !view.canScrollVertically(-1)))) {


            /
            //Optional row animation to highlight the row when scrolling finishes
            // Animation animation = new ScaleAnimation(1, 1, 0, 1, 0, child.getHeight()/2);
            // animation.setDuration(400);
            // child.startAnimation(animation);
            /


            return;


        }




        view.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(final AbsListView view, final int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {


                    view.setOnScrollListener(null);


                    smoothScrollToPosition(view, position, offset);




                }
            }


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


        });




        // Perform scrolling to position
        new Handler().post(new Runnable() {
            @Override
            public void run() {


            view.smoothScrollToPositionFromTop(position, offset);


            }
        });




    }






    public static View getChildAtPosition(final AdapterView view, final int position) {
        final int index = position - view.getFirstVisiblePosition();
        if ((index >= 0) && (index < view.getChildCount())) {
            return view.getChildAt(index);
        } else {
            return null;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值