android - 安卓如何让View往屏幕外隐藏?
PHPz
PHPz 2017-04-18 09:15:37
0
1
591

如图,让和让最新那条插入的数据 显示在 顶部开始的地方,而旧的数据隐藏在顶部屏幕之外呢?

当插入 问题14的时候,,前13条被隐藏在顶部之外的地方了。怎么做到这样呢?
模仿语言助手那种界面?如何做到

PHPz
PHPz

学习是最好的投资!

Antworte allen(1)
黄舟

以下两种方案都亲测可行:
方案一:(无动画)

listView.setSelection(问题14的position);

方案二:(带动画)

ListView lvBaseList添加问题14后调用

                final int targetPosition = 2;//假设你要置顶的ItemView的position = 2
                final long targetId = adapter.getItemId(targetPosition);
                View child = getTargetView(lvBaseList, targetId);
                final boolean isVisible = child != null;
                
                if (isVisible) {//问题14已显示
                    Log.d(TAG, "lvBaseList.onItemClick  isVisible = true >> ");
                    Log.d(TAG, "lvBaseList.onItemClick  lvBaseList.getY() = " + (int) lvBaseList.getY());
                    Log.d(TAG, "lvBaseList.onItemClick  child.getY() = " + (int) child.getY());
                    lvBaseList.smoothScrollBy((int) (child.getY() - lvBaseList.getY()), 200);
                    return;
                }
                
                //问题14未显示
                Log.d(TAG, "lvBaseList.onItemClick  isVisible = false >> ");
                lvBaseList.smoothScrollToPosition(targetPosition + lvBaseList.getHeaderViewsCount());
                lvBaseList.setOnScrollListener(new OnScrollListener() {
                    
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
                        if (scrollState == SCROLL_STATE_IDLE) {
                            lvBaseList.setOnScrollListener(null);//避免影响正常滚动
                            
                            View child = getTargetView(lvBaseList, targetId);
                            if (child == null) {
                                Log.e(TAG, "lvBaseList.onItemClick  child == null >>  return;");
                                return;
                            }
                            
                            Log.d(TAG, "lvBaseList.onItemClick  lvBaseList.getY() = " + (int) lvBaseList.getY());
                            Log.d(TAG, "lvBaseList.onItemClick  child.getY() = " + (int) child.getY());
//                            lvBaseList.scrollListBy((int) (child.getY() - lvBaseList.getY()));//可行,无动画
                            lvBaseList.smoothScrollBy((int) (child.getY() - lvBaseList.getY()), 200);
                        }
                    }
                    
                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        
                    }
                });

需要用到的函数:

     //根据targetId获取childView
     private View getTargetView(ListView lvBaseList, long targetId) {
        View child = null;
        MomentView itemView;//MomentView相当于ViewHolder,里面存放item数据或者itemId
        MomentItem item;
        for (int i = 0; i < lvBaseList.getChildCount(); i++) {
            child = lvBaseList.getChildAt(i);
            itemView = child == null ? null : (MomentView) child.getTag();
            item = itemView == null ? null : itemView.getData();
            Log.d(TAG, "lvBaseList.onItemClick  item.getId() = " + (item == null ? 0 : item.getId()) + "; targetId = " + targetId);
            if (item != null && item.getId() == targetId) {
                Log.d(TAG, "lvBaseList.onItemClick  item != null && item.getId() == targetId >>  break;");
                break;
            }
            child = null;
        }
        return child;
    }
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!