0

I cannot access the method marked****(setScrollViewListener) from class DetectHere .even at outside the class (ie from the method onCreate) DetectHere the method setScrollViewListener is valid..here i have created a class SSScrollView to know when the scroll bar reaches the end of scroll..kindly help.

package com.example.mee.layoutcreate;    
import ...    

public class MainActivity extends ActionBarActivity {


    public interface ScrollViewListener {
       void onScrollChanged(SSScrollView scrollView, int x, int y, int oldx, int oldy);
                                        }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       final SSScrollView scroll=new SSScrollView(this);       


      public  class DetectHere  implements ScrollViewListener {

                scroll.setScrollViewListener(this);   ********************* cannot access this method 

            @Override
            public void onScrollChanged(SSScrollView scrollView, int x,
                                        int y, int oldx, int oldy) {

                                            return;
                                                                    }
                                                                }
                                                        }
                                                    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
                                        }
        return super.onOptionsItemSelected(item);
                                                        }
                                                            }


Here is the class SSScrollView                                      





 package com.example.mee.layoutcreate;
    import...

    public class SSScrollView extends ScrollView {

        private MainActivity.LDObservableScrollViewListener scrollViewListener = null;

        public SSScrollView(Context context) {
            super(context);
        }

        public SSScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public SSScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public void setScrollViewListener(MainActivity.LDObservableScrollViewListener scrollViewListener) {
            this.scrollViewListener = scrollViewListener;
        }

        @Override
        protected void onScrollChanged(int x, int y, int oldx, int oldy) {
            super.onScrollChanged(x, y, oldx, oldy);
            if(scrollViewListener != null) {
                scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
            }
        }

    }

1 Answers1

0

The problem I guess is that listener requires a Context as parameter. Since DetectHere is not extended from Activity, you cannot set it inside. You should create constructor as below:

private Context ctx;

public DetectHere (Context ctx){
   this.ctx = ctx;
{

and then set listener:

scroll.setScrollViewListener(ctx);

But actually in your case DetectHere is a listener itself. So in your Activity you need to set it as:

scroll.setScrollViewListener(new DetectHere());

and implement this class methods, the IDE will hint you to do this.

I hope this is what you're asking and it will help you.

Yurets
  • 3,999
  • 17
  • 54
  • 74