同步在Android的两个水平滚动浏览 [英] Synchronizing two Horizontal Scroll Views in Android

查看:292
本文介绍了同步在Android的两个水平滚动浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面这则讯息调校滚动型滚动位置的建议的平台 - 安卓但运行了一个问题。

背景 - 我想与横向和纵向滚动表布局,但我想的第一行和第一列总是present - 像冻结窗格与Excel。不幸的是,似乎几乎是不可能与Android java编程......害我这么头疼。我把它设置,这样如果我可以同步两个horizo​​ntalscrollviews那么我将有表的工作就是我想要的。

我也跟着从上面的贴链接的意见和我的问题是这样的。 该应用程序的力量,当我实现了关闭

  

scrollView1.setScrollViewListener(本);

我想问题可能是我声明了ObservableScrollViews的方式。我不使用XML - 被编程创建的所有对象。我试图用

  

私人ObservableScrollView oScrollViewOne =新ObservableScrollView(本);

但这造成了强制关闭为好。 (我可以只创建一个普通滚动视图,为其分配一个ID,然后用

  

scrollView1 =(ObservableScrollView)findViewById(ID);    其中,ID是整数我给滚动视图。

我不知道该怎么做下面的意见,以及如何使用他们,如果我不使用的XML布局。

我们应该在布局中指定,而不是现有的滚动型标签新ObservableScrollView类。

  

com.test.ObservableScrollView   机器人:ID =@ + ID / scrollview1   ......

     

任何其他建议实行同步两个horizo​​ntalscroll观点,与明确的code - 不只是一个建议使用OnTouchMotionEvent或其他的想法不给code,正如我以前见过。

下面是错误code

  10-23 23:33:08.631:ERROR / AndroidRuntime(18187):致命异常:主要
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.glen.apps.TeacherAidePro / com.glen.apps.TeacherAidePro.TeacherAidePro}:java.lang中。空指针异常
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread.access $ 2300(ActivityThread.java:135)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.os.Handler.dispatchMessage(Handler.java:99)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.os.Looper.loop(Looper.java:144)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread.main(ActivityThread.java:4937)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在java.lang.reflect.Method.invokeNative(本机方法)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在java.lang.reflect.Method.invoke(Method.java:521)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在dalvik.system.NativeStart.main(本机方法)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):由:显示java.lang.NullPointerException
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.view.View< INIT>(View.java:1810)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.view.View< INIT>(View.java:1856)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.view.ViewGroup< INIT>(ViewGroup.java:299)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.widget.FrameLayout< INIT>(FrameLayout.java:83)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.widget.ScrollView< INIT>(ScrollView.java:137)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.widget.ScrollView< INIT>(ScrollView.java:133)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.widget.ScrollView< INIT>(ScrollView.java:129)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在com.glen.apps.TeacherAidePro.ObservableScrollView< INIT>(ObservableScrollView.java:12)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在com.glen.apps.TeacherAidePro.TeacherAidePro< INIT>(TeacherAidePro.java:119)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在java.lang.Class.newInstanceImpl(本机方法)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在java.lang.Class.newInstance(Class.java:1429)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.Instrumentation.newActivity(Instrumentation.java:1036)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
10-23 23:33:08.631:ERROR / AndroidRuntime(18187):11 ...更多
 

下面是一行12

  1.包com.glen.apps.TeacherAidePro;
 2。
 3.进口android.content.Context;
 4.进口android.util.AttributeSet;
 5.进口android.widget.ScrollView;
 6。
 7.公共类ObservableScrollView扩展了滚动型{
 8。
 9.私人IScrollListener监听= NULL;
10。
11.公共ObservableScrollView(上下文的背景下){
12.超(上下文);
  }
 

解决方案

该错误指向你的ObservableScrollView构造一个空指针。能否请您发表的构造,以及12号线在自己的?

一个可能的问题,我看到的方式您的XML和自定义滚动视图交互是您的自定义滚动视图是一个内部类。请参见此页如何在声明一个内部类的自定义组件XML或将其移动到一个外部类。我通常把它放在一个外部类,但如果你想保持它作为一个内部类会是这样

 <查看
    类=com.glen.apps.TeacherAidePro $ ObservableScrollView
... />
 

我不认为这虽然说明了你的空指针异常,所以请发表您的ObservableScrollView类。

编辑:

如果你坚持做的一切在Java中,这里是一个工作的例子:

 私人ObservableScrollView scrollView1 = NULL;
私人ObservableScrollView scrollView2 = NULL;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的LinearLayout父=新的LinearLayout(本);
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    parent.setWeightSum(2.0f);

    scrollView1 =新ObservableScrollView(本);
    scrollView1.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT,1.0F));
    scrollView2 =新ObservableScrollView(本);
    scrollView2.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT,1.0F));

    scrollView1.setScrollViewListener(新ScrollViewListener(){
        公共无效onScrollChanged(ObservableScrollView滚动视图,INT X,
                INT Y,INT oldx,诠释oldy){
            scrollView2.scrollTo(X,Y);
        }
    });
    scrollView2.setScrollViewListener(新ScrollViewListener(){
        公共无效onScrollChanged(ObservableScrollView滚动视图,INT X,
                INT Y,INT oldx,诠释oldy){
            scrollView1.scrollTo(X,Y);
        }
    });

    TextView的TV1 =新的TextView(本);
    tv1.setText("TEXT1TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
    tv1.setTextSize(36.0f);
    scrollView1.addView(TV1);

    TextView的TV2 =新的TextView(本);
    tv2.setText("TEXT2TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
    tv2.setTextSize(36.0f);
    scrollView2.addView(TV2);

    parent.addView(scrollView1);
    parent.addView(scrollView2);
    parent.invalidate();

    的setContentView(父);
}
 

基本上,这将创建两个滚动的观点并列,用1.0各权重,并把它们与2.0的布局总重量的LinearLayout,所以他们每次拿到宽度的一半。

不过,我强烈建议习惯XML,因为它是非常非常容易(在我看来)来创建布局。这也更容易发现错误,XML的嵌套形式更容易阅读。不管怎么说,希望这将清除的事情了。

I tried following the advice from this post Synchronise ScrollView scroll positions - android but running into a problem.

Background - I want a table layout with both horizontal and vertical scrolling, but I want the first row and first column always present - like freeze pane with excel. Unfortunately it seems that is close to impossible with Android java programming...causing me such a headache. I have it set up so that if I can just synchronize these two horizontalscrollviews then I will have the table working the way I want.

I followed the advice from the posted link above and the problem I have is this. The app force closes when I implement the

scrollView1.setScrollViewListener(this);

I think the problem may be the way I am declaring the ObservableScrollViews. I am not using XML - all objects are created programatically. I tried using

private ObservableScrollView oScrollViewOne = new ObservableScrollView (this);

but this is causing a force close as well. (Can I just create a regular scrollview, assign it an ID and then use

scrollView1 = (ObservableScrollView) findViewById(ID); where ID is the integer number I gave the scrollview.

I'm wondering what to make of the comments below and how to use them if I am not using the XML layout.

And we should specify this new ObservableScrollView class in the layout, instead of the existing ScrollView tags.

com.test.ObservableScrollView android:id="@+id/scrollview1" ...

Any other suggestions to implement synchronizing two horizontalscroll views, with the explicit code - not just a suggestion to use OnTouchMotionEvent or other idea without giving the code, as I've seen before.

Here's the error code

10-23 23:33:08.631: ERROR/AndroidRuntime(18187): FATAL EXCEPTION: main
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.glen.apps.TeacherAidePro/com.glen.apps.TeacherAidePro.TeacherAidePro}: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.os.Looper.loop(Looper.java:144)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread.main(ActivityThread.java:4937)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at java.lang.reflect.Method.invoke(Method.java:521)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at dalvik.system.NativeStart.main(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187): Caused by: java.lang.NullPointerException
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.view.View.<init>(View.java:1810)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.view.View.<init>(View.java:1856)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.view.ViewGroup.<init>(ViewGroup.java:299)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.widget.FrameLayout.<init>(FrameLayout.java:83)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.widget.ScrollView.<init>(ScrollView.java:137)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.widget.ScrollView.<init>(ScrollView.java:133)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.widget.ScrollView.<init>(ScrollView.java:129)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at com.glen.apps.TeacherAidePro.ObservableScrollView.<init>(ObservableScrollView.java:12)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at com.glen.apps.TeacherAidePro.TeacherAidePro.<init>(TeacherAidePro.java:119)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at java.lang.Class.newInstanceImpl(Native Method)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at java.lang.Class.newInstance(Class.java:1429)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
10-23 23:33:08.631: ERROR/AndroidRuntime(18187):     ... 11 more

Here's line 12

 1. package com.glen.apps.TeacherAidePro;
 2.
 3. import android.content.Context;
 4. import android.util.AttributeSet;
 5. import android.widget.ScrollView;
 6.
 7. public class ObservableScrollView extends ScrollView {
 8.
 9.   private IScrollListener listener = null;    
10.
11.   public ObservableScrollView(Context context) {
12.       super(context);
  }

解决方案

The error points to a null pointer in a constructor in your ObservableScrollView. Could you please post the constructor, as well as line 12 on its own?

One possible issue I am seeing with the way your XML and custom scroll view interact is that your custom scroll view is an inner class. See this page for how to declare an inner class custom component in XML, or move it to an external class. I usually put it in an external class, but if you wanted to keep it as an inner class it would be something like

<view
    class="com.glen.apps.TeacherAidePro$ObservableScrollView"
.../>

I don't think this explains your null pointer exception though, so please post your ObservableScrollView class.

EDIT:

If you insist on doing everything in Java, here is a working example:

private ObservableScrollView scrollView1 = null;
private ObservableScrollView scrollView2 = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout parent = new LinearLayout(this);
    parent.setOrientation(LinearLayout.HORIZONTAL);
    parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    parent.setWeightSum(2.0f);

    scrollView1 = new ObservableScrollView(this);
    scrollView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT, 1.0f));
    scrollView2 = new ObservableScrollView(this);
    scrollView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT, 1.0f));

    scrollView1.setScrollViewListener(new ScrollViewListener() {
        public void onScrollChanged(ObservableScrollView scrollView, int x,
                int y, int oldx, int oldy) {
            scrollView2.scrollTo(x, y);
        }
    });
    scrollView2.setScrollViewListener(new ScrollViewListener() {
        public void onScrollChanged(ObservableScrollView scrollView, int x,
                int y, int oldx, int oldy) {
            scrollView1.scrollTo(x, y);
        }
    });

    TextView tv1 = new TextView(this);
    tv1.setText("TEXT1TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
    tv1.setTextSize(36.0f);
    scrollView1.addView(tv1);

    TextView tv2 = new TextView(this);
    tv2.setText("TEXT2TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
    tv2.setTextSize(36.0f);
    scrollView2.addView(tv2);

    parent.addView(scrollView1);
    parent.addView(scrollView2);
    parent.invalidate();

    setContentView(parent);
}

Basically, this creates two scroll views side by side, with weights of 1.0 each, and puts them in a LinearLayout with total weight on the layout of 2.0, so they each get half the width.

However, I highly recommend getting used to XML as it is much, much easier (in my opinion) to create layouts. It is also easier to spot mistakes, and the nested form of XML makes it easier to read. Anyway, hope this clears things up.

这篇关于同步在Android的两个水平滚动浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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