首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HorizontalListView(安卓)中滚动的问题

HorizontalListView(安卓)中滚动的问题
EN

Stack Overflow用户
提问于 2014-12-27 23:38:58
回答 1查看 215关注 0票数 0

我使用一个HorizontalListView滚动我使用下面的代码(setOnItemClickListener方法)

代码语言:javascript
复制
 listview.post(new Runnable() {
                public void run() {
                    int centerX = listview.getChildAt(position).getLeft();

                    listview.scrollTo(centerX);
                }
            });

但是当位置到达3或更多时会出现一个问题,然后我会得到一个错误:

代码语言:javascript
复制
12-28 01:33:37.814  20841-20841/com.example.SmsService E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.SmsService, PID: 20841
java.lang.NullPointerException
        at com.example.SmsService.VideoView$1$1.run(VideoView.java:68)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
        at dalvik.system.NativeStart.main(Native Method)

我想这可能是因为我使用了循环的listView

代码语言:javascript
复制
  @Override
    public int getCount() {
        return Integer.MAX_VALUE;//
    }

我怎么能解决这个问题?,也许还有其他方法滚动listView?(滚动平滑不起作用) 我的班。和.xml文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" android:id="@+id/ll">
    <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView"
               android:layout_gravity="center_horizontal"
               android:layout_alignParentTop="true"
               android:layout_alignParentRight="true"
               android:layout_alignParentBottom="true"
            />
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="150dp" android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" android:layout_marginBottom="20dp">
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <com.devsmart.android.ui.HorizontalListView
                    android:id="@+id/listview2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true" android:layout_weight="1"/>
            <com.devsmart.android.ui.HorizontalListView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:smoothScrollbar="true"
                    android:clickable="true" android:layout_alignParentRight="true"            android:layout_weight="1"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>
</LinearLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-28 00:21:37

更改这一行

代码语言:javascript
复制
public void run() {
    int centerX = listview.getChildAt(position).getLeft();
    listview.scrollTo(centerX)
}

代码语言:javascript
复制
public void run() {
    View v = listView.getChildAt(position);
    if(v != null)
    {
        int centerX = v.getLeft();
        listview.scrollTo(centerX);
    }
}

你可能得另找一种方法来移动你的物品。此代码将只修复空指针异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27672474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档