Android ViewPager 获取当前View [英] Android ViewPager get the current View

查看:54
本文介绍了Android ViewPager 获取当前View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager,我想获取当前选定的可见视图,而不是位置.

I have a ViewPager, and I'd like to get the current selected and visible view, not a position.

  1. getChildAt(getCurrentItem) 返回错误的 View
  2. 这并非一直有效.有时返回null,有时只是返回错误的View.

  1. getChildAt(getCurrentItem) returns wrong View
  2. This works not all the time. Sometimes returns null, sometimes just returns wrong View.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser == true) { 
        mFocusedListView = ListView; 
    }
}

  • PageListener on ViewPagergetChildAt() 也不起作用,每次都没有给我正确的视图.

  • PageListener on ViewPager with getChildAt() also not working, not giving me the correct View every time.

    如何获取当前可见的视图?

    How can i get current visible View?

    View view = MyActivity.mViewPager.getChildAt(MyActivity.mViewPager.getCurrentItem()).getRootView();
    ListView listview = (ListView) view.findViewById(R.id.ListViewItems);
    

    推荐答案

    我已经想通了.我所做的是调用带有名称的 setTag() 到所有 Views/ListViews,然后调用 findViewWithTag(mytag), mytag 是标签.

    I've figured it out. What I did was to call setTag() with a name to all Views/ListViews, and just call findViewWithTag(mytag), mytag being the tag.

    不幸的是,没有其他方法可以解决这个问题.

    Unfortunately, there's no other way to solve this.

    这篇关于Android ViewPager 获取当前View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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