我正在尝试实现一个scrollListener来获取HorizontallScrollview滚动的事件,这是android没有提供的,所以我发现了另一种实现这个的方法 使用此链接
listening to scroll events horizontalscrollview android
- >创建一个扩展horizontall滚动视图的类
- >滚动更改方法
时覆盖
但是当我在我的活动类中创建我的类的对象时,它会给出错误。
附加了Logcat输出。
PLS。救命。我是第一次这样做。
代码: XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.gridTest.myHorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarThumbHorizontal="@drawable/scrollbar_horizontal_thumb"
android:scrollbarTrackHorizontal="@drawable/scrollbar_horizontal_track"
android:visibility="visible"
android:fadeScrollbars="false"
android:id="@+id/scrollView">
<android.support.v7.widget.GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="18"
android:orientation="horizontal"
android:rowCount="1"
android:paddingBottom="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp"
android:id="@+id/text1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="Hi"
android:textSize="16sp" />
</android.support.v7.widget.GridLayout>
</com.example.gridTest.myHorizontalScrollView>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp" />
</RelativeLayout>
Java代码: MainActivity
package com.example.gridtest;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.widget.GridLayout;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridLayout grid = null;
ViewGroup vg = null;
myHorizontalScrollView hsvw = null;
SeekBar seekBar = null;
Display myDisplay = null;
int width = 0;
TextView text1 = null;
int max = 954;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridLayout) (findViewById(R.id.grid));
hsvw = (myHorizontalScrollView)findViewById(R.id.scrollView);
//error occur here..
seekBar = (SeekBar)findViewById(R.id.seekBar1);
myDisplay = getWindowManager().getDefaultDisplay();
width = myDisplay.getWidth();
text1=(TextView)(findViewById(R.id.text1));
gestureScanner = new GestureDetector(this);
text1.getWidth();
text1.getHeight();
}
myHorizontalScrollView.java
package com.example.gridtest;
import android.content.Context;
import android.util.Log;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
public class myHorizontalScrollView extends HorizontalScrollView{
public myHorizontalScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
Log.i("Scrolling", "X from ["+oldl+"] to ["+l+"]");
super.onScrollChanged(l, t, oldl, oldt);
}
}
Log Cat OutPut:
06-03 11:37:43.688: E/AndroidRuntime(14781): FATAL EXCEPTION: main
06-03 11:37:43.688: E/AndroidRuntime(14781): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridtest/com.example.gridtest.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.gridTest.myHorizontalScrollView
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread.access$600(ActivityThread.java:156)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.os.Looper.loop(Looper.java:153)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread.main(ActivityThread.java:5297)
06-03 11:37:43.688: E/AndroidRuntime(14781): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 11:37:43.688: E/AndroidRuntime(14781): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 11:37:43.688: E/AndroidRuntime(14781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-03 11:37:43.688: E/AndroidRuntime(14781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-03 11:37:43.688: E/AndroidRuntime(14781): at dalvik.system.NativeStart.main(Native Method)
06-03 11:37:43.688: E/AndroidRuntime(14781): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.gridTest.myHorizontalScrollView
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-03 11:37:43.688: E/AndroidRuntime(14781): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.Activity.setContentView(Activity.java:1881)
06-03 11:37:43.688: E/AndroidRuntime(14781): at com.example.gridtest.MainActivity.onCreate(MainActivity.java:38)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.Activity.performCreate(Activity.java:5122)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-03 11:37:43.688: E/AndroidRuntime(14781): ... 11 more
06-03 11:37:43.688: E/AndroidRuntime(14781): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gridTest.myHorizontalScrollView" on path: DexPathList[dexElements=[zip file "/mnt/asec/com.example.gridtest-2/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/com.example.gridtest-2/lib, /vendor/lib, /system/lib]]
06-03 11:37:43.688: E/AndroidRuntime(14781): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
06-03 11:37:43.688: E/AndroidRuntime(14781): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-03 11:37:43.688: E/AndroidRuntime(14781): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-03 11:37:43.688: E/AndroidRuntime(14781): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
06-03 11:37:43.688: E/AndroidRuntime(14781): ... 21 more