1

I am using the droidQuery library to add some gesture detection to my app. I was having trouble adding the SwipeInterceptorView in my xml file (see original question here, so I define it inside my onCreate method and give it the same parameters. Then I add it to my layout using layout.addView(swipeInterceptorView). I attempted to add it to my XML layout file, but I got an error.

Is defining the SwipeInterceptorView programatically good practice, or should I add it to my XML file? If I should, how can I fix this XML file? :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/table"
    android:orientation="vertical" >
    <self.philbrown.SwipeInterceptorView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </self.philbrown.SwipeInterceptorView>
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

When I use this layout, I get the Runtime error java.lang.ClassNotFoundException.

Also, i know how to add the onClickListener to an ImageView, but could anyone tell me how I should add the listener to a TextView also?

Thanks for any help!

Community
  • 1
  • 1
superuser
  • 731
  • 10
  • 28

1 Answers1

1

Is defining the SwipeInterceptorView programatically good practice, or should I add it to my XML file?

It is best practice to define the View in XML, since this provides a reusable file (so you can using it elsewhere in your app, or again in another app).

That said, it is not bad practice to instantiate it programmatically - it just reduces the reusability factor.

how can I fix this XML file?

I am currently unsure as to why this is not working. I will be looking into it in more detail ASAP. In the mean time, one sure thing would be to download the entire project, import it into your workspace, and add it as a library project, rather than a jar.

Also, i know how to add the onClickListener to an ImageView, but could anyone tell me how I should add the listener to a TextView also?

You can add the click listener exactly the same way:

mTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //your code
    }
});

If you want to make full use of the droidQuery library, you can optionally use one of these methods instead:

click

$.with(mTextView).click(new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        //your code
    }
});

on

$.with(mTextView).on("click", new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        //your code
    }
});

one (only handles click once)

$.with(mTextView).one("click", new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        //your code
    }
});

bind

Object data = new Object();//could be anything
$.with(mTextView).on("click", data, new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {//data will be passed as a param
        //your code
    }
});
Phil
  • 35,852
  • 23
  • 123
  • 164
  • thanks. Also, how could I let the text-view detect a swipe? I tried doing the same thing as you showed my for the image-view in your previous answer, but it didn't work – superuser Aug 20 '13 at 21:20
  • @user2583347 you can put your `TextView` inside a `ViewGroup` - such as a `RelativeLayout` or `FrameLayout`. This could be the same size as your `TextView` is now, and set your `TextView` to fill the parent bounds. Finally, add the swipe handling code to this parent `ViewGroup`. – Phil Aug 21 '13 at 03:18
  • thanks. do you know how to make the swiping less sensitive with the droidQuery library so you have to swipe longer? – superuser Aug 21 '13 at 14:56
  • @superuser you will need to change your implementation of the `swipe` function by declaring setting a class variable to the current time (`System.getCurrentTimeMilis()`) for the `START` event, then for each swipe event, check the time since the event started. If the interval has been long enough, you can react to it. – Phil Aug 21 '13 at 15:00
  • @superuser, again I think this would be best as a new question (where I can post some code). I will look for it soon. – Phil Aug 21 '13 at 15:12
  • I posted the question – superuser Aug 21 '13 at 15:31
  • also, after I added the swipeInterceptorView, this main function that you are showing me is never called. – superuser Aug 21 '13 at 17:21
  • @superuser did you call `view.setSwipeListener`? – Phil Aug 21 '13 at 17:23
  • yes. the swipeinterceptorview is registering the swipes. The other function is the one that is not working. – superuser Aug 21 '13 at 17:25
  • @superuser, which function? Can you say more? – Phil Aug 21 '13 at 17:28
  • the one that begins with $.with(v).swipe(new Function() { – superuser Aug 21 '13 at 17:29
  • it is defined in the onCreate method of my activity – superuser Aug 21 '13 at 17:37
  • @superuser, if you are using the `SwipeInterceptorView` in `XML`, you do not need to call this `droidQuery` method, which is simply a shortcut for adding the swipe interceptor and swipe detection for you. Instead, just manually call `setSwipeListener`. You will then also need to handle touch up and down events in the `onTouchListener` code. – Phil Aug 21 '13 at 17:40
  • I just asked another question about how to use the droidQuery library – superuser Aug 22 '13 at 13:05