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!
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
}
});