获取列表视图项位置之后,显示软键盘 [英] Get listview item position after soft keyboard shown

查看:252
本文介绍了获取列表视图项位置之后,显示软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,它是在操作栏和一个EditText以上。
见图片:

I have a ListView, it is under action bar and above an EditText. See picture:

该列表视图项目根据其内容具有随机高度。

The list view items have random height according to its content.

当点击列表视图中的项目,我需要关注的EditText和开放的软键盘,同时我想一个被点击的是在列表视图的顶部,例如,如果我点击项目2,我想看看:

When click list view item, I need to focus on EditText and open soft keyboard, meanwhile I want the one being clicked to be at top of the list view, for example if I click item2, I want to see:

有关将点击的项目列表视图顶部,单击某个项目时,我将通过它的位置获得点击查看项目,并调用 view.getLocationOnScreen(处)以获得其y位置,并使用此ÿ减去ActionBar的高度,然后我得到滚动的距离,最后调用 listview.smoothScrollBy(距离100);

For putting the clicked item to the top of listview, when clicking an item, I will get the clicked item View by its position, and call view.getLocationOnScreen(locations) to get its y location, and use this y minus actionbar's height, then I get the distance to scroll, at last call listview.smoothScrollBy(distance, 100);.

这方法软键盘后效果很好被显示,由于列表视图的高度将被固定即可;但它不能很好地工作,我第一次单击列表视图项,因为在这一点上,ListView的高度将被改变作为软键盘占用了一些空间,当我使用视图导致。 getLocationOnScreen(处),它返回视图的老位置,的意思所示的软键盘前的位置,我也试图用视图.postDelayed(新的Runnable(){//获取视图位置在这里!},1000); ,但得到了同样的的位置

This approach works well after the soft keyboard being shown, as the list view's height will be fixed then; but it doesn't work well the first time I click a list view item, because at this point, the listview's height will be changed as the soft keyboard takes up some spaces, which causes when I use view.getLocationOnScreen(locations), it returns the old position of the view, old means the position before the soft keyboard shown, I also tried to use view.postDelayed(new Runnable() {//Gets view position here!}, 1000);, but got the same old position.

所以问题应该如何获取最新位置显示的键盘后?

So the question should be how to get the latest position after the keyboard shown?

我用的android:transcriptMode =正常的ListView和 adjustResize windowSoftInputMode

I use android:transcriptMode="normal" for ListView and adjustResize for windowSoftInputMode.

编辑:

也试过 INT firstVisiblePos = commentsListView.getFirstVisiblePosition();
        INT偏移= firstVisiblePos - clickedPosition;
        commentsListView.smoothScrollByOffset(偏移);
,但仍然没有帮助

Also tried int firstVisiblePos = commentsListView.getFirstVisiblePosition(); int offset = firstVisiblePos - clickedPosition; commentsListView.smoothScrollByOffset(offset);, but still no helps.

推荐答案

commentsListView.setSelection(clickedPosition + 1); ,得到了它,只是这个工作对我来说,虽然它没有动画(不光滑),它是我不够好,为使平滑滚动到的位置,使用这个答案[链接] <一个href=\"http://stackoverflow.com/questions/14479078/smoothscrolltopositionfromtop-is-not-always-working-like-it-should/20997828#20997828\">smoothScrollToPositionFromTop()并不总是工作像它应该

commentsListView.setSelection(clickedPosition + 1);, got it, simply this works for me, although it has no animation (not smooth), it is good enough for me, for making the smooth scroll to position, use this answer [link]smoothScrollToPositionFromTop() is not always working like it should

这篇关于获取列表视图项位置之后,显示软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆