why onInterceptTouchEvent overrides the all touch event - android

I have a viewpager and several fragments.I extend a ViewPager and implement a my own class.Also i implemented onInterceptTouchEvent method.Now when i swipe it doesn't work.I think onInterceptTouchEvent intercept them. How to handle this?
package com.android.ho;
import com.sph.custom.ViewPagerWithTapGestreDetection;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PDFActivity extends FragmentActivity implements ImageOnTapCallback,OnPageChangeListener{
static final int ITEMS = 20;
MyAdapter mAdapter;
//ViewPager mPager;
ViewPagerWithTapGestreDetection mPager;
//ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
try {
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPagerWithTapGestreDetection) findViewById(R.id.pager);
mPager.setId(5000);
mPager.setImageOnTapCallback(this);
mPager.setAdapter(mAdapter);
//mPager.setOffscreenPageLimit(1);
mPager.setOnPageChangeListener(this);
/*
Button button = (Button) findViewById(R.id.first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(ITEMS - 1);
}
});
*/
mPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return MyFragment.newInstance("1.pdf");
case 1: // Fragment # 1 - This will show image
return MyFragment.newInstance("2.pdf");
case 2: // Fragment # 1 - This will show image
return MyFragment.newInstance("3.pdf");
case 3: // Fragment # 1 - This will show image
return MyFragment.newInstance("4.pdf");
case 4: // Fragment # 1 - This will show image
return MyFragment.newInstance("5.pdf");
default:// Fragment # 2-9 - Will show list
return MyFragment.newInstance("1.pdf");
}
}
}
#Override
public void tapDetected() {
// TODO Auto-generated method stub
Log.e("tapDetected", "tapDetected");
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageScrollStateChanged", "onPageScrollStateChanged");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
Log.e("onPageScrolled", "onPageScrolled");
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageSelected", "onPageSelected");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("onDestroy", "onDestroy onDestroy onDestroy");
}
}
This is my fragment class
package com.android.ho;
import java.io.File;
import java.io.InputStream;
import com.epapyrus.plugpdf.core.viewer.ReaderView;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static int count = 1;
static String file;
TextView txt;
ReaderView readerView;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
file = message;
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
try {
File fl = new File(Environment.getExternalStorageDirectory(), "/pdf/2.pdf");
readerView = (ReaderView) v.findViewById(R.id.pdfviews);
//readerView.setOnClickListener(this);
Log.e("readerView", fl.toString());
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return true;
}
});
// readerView.openFile("/mnt/sdcard/pdf/1.pdf","");
if (count > 5)
count = 1;
readerView.openFile("/mnt/sdcard/pdf/" + count + ".pdf", "");
Log.e("Inside onCreateView count is ", "" + count);
count++;
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "Cilcked..",Toast.LENGTH_SHORT).show();
}
});
/*v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});*/
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// Log.d("PDFVIEW", "onDestroyView");
try {
if (readerView != null) {
readerView.clear();
// readerView.clearAllField(5);
// readerView.destroyDrawingCache();
// readerView.
// readerView=null;
// mPDFView.purgeMemory();
// mPDFView.destroy();//added to force release memory
// mPDFView = null;
}
Log.e("Fragment onDestroyView ", " onDestroyView readerView is "+readerView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my custom viewpager class
package com.android.ho
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import com.sph.bh.ImageOnTapCallback;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;
public class ViewPagerWithTapGestreDetection extends ViewPager {
private ImageOnTapCallback imageOnTapCallback;
private GestureDetector tapGestureDetector;
private OnGestureListener mGestureListener;
public ViewPagerWithTapGestreDetection(Context context) {
super(context);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
public ViewPagerWithTapGestreDetection(Context context, AttributeSet attrs) {
super(context, attrs);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Don't let any touches be passed down to the children automatically
Log.d("onInterceptTouchEvent ", "onInterceptTouchEvent");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("onTouchEvent", "onTouchEvent");
if (imageOnTapCallback != null) {
tapGestureDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
public ImageOnTapCallback getImageOnTapCallback() {
return imageOnTapCallback;
}
public void setImageOnTapCallback(ImageOnTapCallback imageOnTapCallback) {
this.imageOnTapCallback = imageOnTapCallback;
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("onSingleTapConfirmed", "onSingleTapConfirmed");
if (null != imageOnTapCallback) {
imageOnTapCallback.tapDetected();
}
return super.onSingleTapConfirmed(e);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("onDoubleTap", "onDoubleTap");
return super.onDoubleTap(e);
}
#Override
public void onLongPress(MotionEvent e) {
Log.d("onLongPress", "onLongPress");
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll", "onScroll");
return super.onScroll(e1, e2, distanceX, distanceY);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("onFling ", "onFling");
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}

I think onInterceptTouchEvent intercept them.
Well, you are returning true from onInterceptTouchEvent(), which means, that as a ViewGroup you do not want any touch event to be passed to children, instead pass that touch event to onTouchEvent().
You should listen for event in onInterceptTouchEvent() return false until you see that this is such a gesture, that shouldn't be handled by children. And on that point only you should return true.

Related

Gesture detection not working on layout

So I have tried this one tutorial which tells how the gestures work and I made a simple activity which detects the gesture and generates a toast. Now I am working on my project and made my homepage which has a simple Relative layout and an image view. The gestures work find there as well. Now I made a new Help page and tried everything I could but the gesture is not working for me. Is it because of the layout? as this one contains multiple relative and linear layouts.
Given below is my Help activity. I wanna open speech recognition on LongPress (I am trying a toast here but its not working)
Any help would be appreciated. I may be missing out something stupid
package com.example.dell.optasia;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import static android.view.GestureDetector.*;
import org.w3c.dom.Text;
import java.util.List;
public class Help extends AppCompatActivity implements
OnGestureListener, OnDoubleTapListener {
private GestureDetectorCompat GestureDetect;
public MediaPlayer mp1;
TextView textView;
TextView answer5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
mp1 = MediaPlayer.create(Help.this, R.raw.help);
mp1.start();
ActionBar ab = getSupportActionBar(); //Actionbar
ab.setLogo(R.mipmap.iconlaunch);
ab.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // set your desired color
ab.setDisplayUseLogoEnabled(true); //shows logo
ab.setDisplayShowHomeEnabled(true); //shows home page
TextView textView= (TextView) findViewById(R.id.textView);
GestureDetect = new GestureDetectorCompat(this, this);
GestureDetect.setOnDoubleTapListener(this);
TextView txtview = (TextView) findViewById(R.id.question1);
final TextView answer1 = (TextView) findViewById(R.id.answer1);
txtview.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (answer1.getVisibility() == View.VISIBLE)
answer1.setVisibility(View.GONE);
else
answer1.setVisibility(View.VISIBLE);
}
});
TextView txtview1 = (TextView) findViewById(R.id.question2);
final TextView answer2 = (TextView) findViewById(R.id.answer2);
txtview1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if (answer2.getVisibility() == View.VISIBLE)
answer2.setVisibility(View.GONE);
else
answer2.setVisibility(View.VISIBLE);
}
});
TextView txtview2 = (TextView) findViewById(R.id.question3);
final TextView answer3 = (TextView) findViewById(R.id.answer3);
txtview2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (answer3.getVisibility() == View.VISIBLE)
answer3.setVisibility(View.GONE);
else
answer3.setVisibility(View.VISIBLE);
}
});
TextView txtview3 = (TextView) findViewById(R.id.question4);
final TextView answer4 = (TextView) findViewById(R.id.answer4);
txtview3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (answer4.getVisibility() == View.VISIBLE)
answer4.setVisibility(View.GONE);
else
answer4.setVisibility(View.VISIBLE);
}
});
TextView txtview4 = (TextView) findViewById(R.id.question5);
final TextView answer5 = (TextView) findViewById(R.id.answer5);
txtview4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (answer5.getVisibility() == View.VISIBLE)
answer5.setVisibility(View.GONE);
else
answer5.setVisibility(View.VISIBLE);
}
});
}
//for the overflow actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//for the actionbar menu items
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Play_ID:
mp1 = MediaPlayer.create(Help.this, R.raw.manual);
mp1.start();
break;
case R.id.Help_ID:
startActivity(new Intent(Help.this, Help.class));
break;
case R.id.About_ID: {
startActivity(new Intent(Help.this, LocationMain.class));
return true;
}
case R.id.Contact_Us:{
startActivity(new Intent(Help.this, ContactUs.class));
return true;
}
case android.R.id.home:{
startActivity(new Intent(Help.this, HomeScreen.class));
return true;
}
default:
return super.onOptionsItemSelected(item);
}
return false;
}
//calls event for the gestures
#Override
public boolean onTouchEvent(MotionEvent event) {
GestureDetect.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
if(mp1!=null)
{
mp1.stop();
} return false;
}
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
Toast.makeText(Help.this, "longpress!", Toast.LENGTH_LONG).show();
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
}
You are creating a GestureDetector but you are never "hooking it up" to your View. Try changing your onCreate like this:
super.onCreate(savedInstanceState);
View v = new RelativeLayout(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(v);
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent me){
return mDetector.onTouchEvent(me);
}
});
or you can try this
public boolean onTouchEvent(MotionEvent event) {
return gestureDetectorCompat.onTouchEvent(event)
}

Scroll view move vertical on swipe the row horizontal

I have many customview in the scrollview. I have implemented swipe feature on the the customview . When I swipe customview to left , scrollview also move vertical. I don't want scroll view to be moved vertical while swiping. I thing Action_Cancel event of touch is going to scrollview which I have to stop. I tried to stop it but it is not working .
Below is the
package com.AA.KK.customui;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GestureDetectorCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.AA.ax4mobile.AAPreferences;
import com.AA.ax4mobile.Constants;
import com.AA.ax4mobile.Session;
import com.AA.ax4mobile.Utils;
import com.AA.ax4mobile.network.Network;
import com.AA.ax4mobile.network.NetworkResponseListener;
import com.AA.KK.R;
import com.AA.KK.activities.KKBaseActivity;
import com.AA.KK.activities.PDFViewerActivity;
public class MailLayout extends RelativeLayout implements OnClickListener, OnTouchListener,
GestureDetector.OnGestureListener {
private CheckBox checkBox;
private TextView title;
private TextView fileSize;
private ImageView fileIcon;
private ImageView tray;
private String url;
private String fileId;
private ViewGroup swipeView;
private boolean isPreview;
private boolean isSelected = true;
private GestureDetectorCompat mDetector;
private static final int SWIPE_OFFSET = 20;
private static final int ANIMATION_OFFSET= 30;
public MailLayout(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.activity_mail_layout, this);
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
setLayoutParams(linLayoutParam);
swipeView = (RelativeLayout) findViewById(R.id.swipe);
swipeView.setOnTouchListener(this);
title = (TextView) findViewById(R.id.file_name);
fileIcon = (ImageView) findViewById(R.id.file_image);
fileSize = (TextView) findViewById(R.id.file_size);
tray = (ImageView) findViewById(R.id.clickme);
tray.setOnClickListener(this);
checkBox = (CheckBox) findViewById(R.id.chk_checkBox);
mDetector = new GestureDetectorCompat(Utils.getInstance().getContext(), this);
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public boolean isPreview() {
return isPreview;
}
public void setPreview(boolean isPreview) {
this.isPreview = isPreview;
}
public ViewGroup getSwipeView() {
return swipeView;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public void setTitleText(String text) {
title.setText(text);
}
public String getTitleText() {
return (String) title.getText();
}
public void setFileSize(String text) {
fileSize.setText(text);
}
public void setChecked(boolean checked) {
checkBox.setChecked(checked);
}
public boolean getChecked() {
return checkBox.isChecked();
}
public void setFileIcon(String fileName) {
if (fileName.contains(".pdf") || fileName.contains(".PDF")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_pdf_selected);
} else if (fileName.contains(".docx") || fileName.contains(".DOCX")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_docx_selected);
} else if (fileName.contains(".doc") || fileName.contains(".DOC")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_doc_selected);
} else if (fileName.contains(".ppt") || fileName.contains(".PPT")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_ppt_selected);
} else if (fileName.contains(".pptx") || fileName.contains(".PPTX")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_pptx_selected);
} else if (fileName.contains(".png") || fileName.contains(".PNG")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_png_selected);
} else if (fileName.contains(".gif") || fileName.contains(".GIF")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_gif_selected);
} else if (fileName.contains(".csv") || fileName.contains(".CSV")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_csv_selected);
} else if (fileName.contains(".bmp") || fileName.contains(".BMP")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_bmp_selected);
} else if (fileName.contains(".txt") || fileName.contains(".TXT")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_txt_selected);
} else if (fileName.contains(".jpg") || fileName.contains(".jpeg") || fileName.contains(".JPG")
|| fileName.contains(".JPEG")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_jpg_selected);
} else if (fileName.contains(".xls") || fileName.contains(".XLS")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_xls_selected);
} else if (fileName.contains(".xlsx") || fileName.contains(".XLXS")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_xlsx_selected);
} else {
fileIcon.setBackgroundResource(R.drawable.ic_file_selected);
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
checkBox.setOnCheckedChangeListener(listener);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickme:
doAddToTray(getFileId());
break;
}
}
public void setFileId(String fileId) {
this.fileId = fileId;
// TODO Auto-generated method stub
}
public String getFileId() {
return fileId;
}
public static void dismissProgress() {
Message msg = new Message();
msg.what = Constants.UIMSG_HIDE_PROGRESSDIALOG;
Bundle data = new Bundle();
data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
msg.setData(data);
Session.getInstance().getMsgHandler().sendMessage(msg);
}
public static void showProgress() {
String message = Utils.getInstance().getContext().getString(R.string.loading);
Message msg = new Message();
msg.what = Constants.UIMSG_SHOW_PROGRESSDIALOG;
Bundle data = new Bundle();
data.putString("message", message);
data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
msg.setData(data);
Session.getInstance().getMsgHandler().sendMessage(msg);
}
public static void doAddToTray(final String ids) {
AAPreferences settings = AAPreferences.getPreferences(Utils.getInstance().getContext());
String accToken = settings.getString(Constants.REQUEST_KEY_ACCESS_TOKEN, null);
String servername = settings.getString(Constants.REQUEST_KEY_SERVERNAME, null);
JSONObject req = new JSONObject();
Network net = Network.getInstance();
try {
req.accumulate(Constants.REQUEST_KEY_ACCESS_TOKEN, accToken);
req.accumulate(Constants.REQUEST_KEY_SERVERNAME, servername);
req.accumulate(Constants.REQUEST_KEY_ID, ids);
req.accumulate(Constants.REQUEST_KEY_PARENT_ID, "");
showProgress();
NetworkResponseListener listener = new NetworkResponseListener() {
#Override
public void handleSuccessResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
dismissProgress();
Toast.makeText(Utils.getInstance().getContext(), R.string.file_added_to_move_tray,
Toast.LENGTH_LONG).show();
String count = null;
try {
JSONObject json = response.getJSONObject("payload");
count = json.getString("total_count");
if (!TextUtils.isEmpty(count))
Utils.getInstance().saveTrayCount(count);
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);
}
#Override
public void handleErrorResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
dismissProgress();
String count = null;
try {
JSONObject json = response.getJSONObject("payload");
count = json.getString("total_count");
Utils.getInstance().saveTrayCount(count);
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);
Message msg = Network.defaultErrorManager(request, response);
if (msg != null) {
KKBaseActivity.defaultUIHandler.sendMessage(msg);
}
}
};
net.linkTray(req, listener);
} catch (JSONException e) {
e.printStackTrace();
}
};
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.e("onFling", "event"+e1.getAction());
if (swipeView.getX() > -(tray.getWidth() + SWIPE_OFFSET)) {
swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
} else {
if (isPreview) {
swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
} else {
swipeView.animate().translationX(-(tray.getWidth() + SWIPE_OFFSET)).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
}
}
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
if (isSelected) {
if (swipeView.getX() <= 0)
swipeView.setX(e2.getX() - e1.getX() + swipeView.getX());
}
return true;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
String s = getUrl();
if (getUrl() == "" || getUrl() == null || getUrl().equalsIgnoreCase("null")) {
Toast.makeText(Utils.getInstance().getContext(), "File is not viewable", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Utils.getInstance().getContext(), PDFViewerActivity.class);
intent.putExtra("filename", getTitleText());
intent.putExtra("view_url", getUrl());
intent.putExtra("preview_from_notification", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Utils.getInstance().getContext().startActivity(intent);
}
return true;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.e("onTouch", "event"+event.getAction());
boolean flag;
if (isPreview) {
tray.setVisibility(View.INVISIBLE);
} else {
tray.setVisibility(View.VISIBLE);
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
flag = onFling(event, event, 0f, 0f);
} else {
flag = mDetector.onTouchEvent(event);
}
return flag;
}
/* #Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.e("onTouchEvent", "event"+event.getAction());
return super.onTouchEvent(event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev){
Log.e("dispatchTouchEvent", "event"+ev.getAction());
if(ev.getAction()== 3)
return false;
else
return super.dispatchTouchEvent(ev);
}*/
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.e("onInterceptTouchEvent", "event"+ev.getAction());
return true;
}
}
in your onScroll()
put this -
ViewParent parent = getParent();
if(parent!= null){
parent.requestDisallowOnInterceptTouchEvent(true);
}

onClick is not called in my viewpager

I have a viewpager with several fragments in the viewpager.I need to handle click event.I have already implement the onTouch event,but its not trigger for click event.When i swiping onTouch is triggering.I don't know why it's not trigger for click event.
This is my viewpager class
package com.android.ho;
import com.sph.custom.ViewPagerWithTapGestreDetection;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PDFActivity extends FragmentActivity implements ImageOnTapCallback,OnPageChangeListener{
static final int ITEMS = 20;
MyAdapter mAdapter;
//ViewPager mPager;
ViewPagerWithTapGestreDetection mPager;
//ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
try {
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPagerWithTapGestreDetection) findViewById(R.id.pager);
mPager.setId(5000);
mPager.setImageOnTapCallback(this);
mPager.setAdapter(mAdapter);
//mPager.setOffscreenPageLimit(1);
mPager.setOnPageChangeListener(this);
/*
Button button = (Button) findViewById(R.id.first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(ITEMS - 1);
}
});
*/
mPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return MyFragment.newInstance("1.pdf");
case 1: // Fragment # 1 - This will show image
return MyFragment.newInstance("2.pdf");
case 2: // Fragment # 1 - This will show image
return MyFragment.newInstance("3.pdf");
case 3: // Fragment # 1 - This will show image
return MyFragment.newInstance("4.pdf");
case 4: // Fragment # 1 - This will show image
return MyFragment.newInstance("5.pdf");
default:// Fragment # 2-9 - Will show list
return MyFragment.newInstance("1.pdf");
}
}
}
#Override
public void tapDetected() {
// TODO Auto-generated method stub
Log.e("tapDetected", "tapDetected");
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageScrollStateChanged", "onPageScrollStateChanged");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
Log.e("onPageScrolled", "onPageScrolled");
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.e("onPageSelected", "onPageSelected");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("onDestroy", "onDestroy onDestroy onDestroy");
}
}
This is my fragment class
package com.android.ho;
import java.io.File;
import java.io.InputStream;
import com.epapyrus.plugpdf.core.viewer.ReaderView;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static int count = 1;
static String file;
TextView txt;
ReaderView readerView;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
file = message;
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
try {
File fl = new File(Environment.getExternalStorageDirectory(), "/pdf/2.pdf");
readerView = (ReaderView) v.findViewById(R.id.pdfviews);
//readerView.setOnClickListener(this);
Log.e("readerView", fl.toString());
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return true;
}
});
// readerView.openFile("/mnt/sdcard/pdf/1.pdf","");
if (count > 5)
count = 1;
readerView.openFile("/mnt/sdcard/pdf/" + count + ".pdf", "");
Log.e("Inside onCreateView count is ", "" + count);
count++;
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "Cilcked..",Toast.LENGTH_SHORT).show();
}
});
/*v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
Log.e("Inside onTouch onTouch onTouch onTouch onTouch onTouch ", "onTouch");
return false;
}
});*/
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// Log.d("PDFVIEW", "onDestroyView");
try {
if (readerView != null) {
readerView.clear();
// readerView.clearAllField(5);
// readerView.destroyDrawingCache();
// readerView.
// readerView=null;
// mPDFView.purgeMemory();
// mPDFView.destroy();//added to force release memory
// mPDFView = null;
}
Log.e("Fragment onDestroyView ", " onDestroyView readerView is "+readerView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my custom viewpager class
package com.android.ho
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import com.sph.bh.ImageOnTapCallback;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;
public class ViewPagerWithTapGestreDetection extends ViewPager {
private ImageOnTapCallback imageOnTapCallback;
private GestureDetector tapGestureDetector;
private OnGestureListener mGestureListener;
public ViewPagerWithTapGestreDetection(Context context) {
super(context);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
public ViewPagerWithTapGestreDetection(Context context, AttributeSet attrs) {
super(context, attrs);
try {
imageOnTapCallback = null;// set if needed
mGestureListener = new GestureListener();
tapGestureDetector = new GestureDetector(getContext(), mGestureListener, null, true);
} catch (Exception e) {
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Don't let any touches be passed down to the children automatically
Log.d("onInterceptTouchEvent ", "onInterceptTouchEvent");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("onTouchEvent", "onTouchEvent");
if (imageOnTapCallback != null) {
tapGestureDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
public ImageOnTapCallback getImageOnTapCallback() {
return imageOnTapCallback;
}
public void setImageOnTapCallback(ImageOnTapCallback imageOnTapCallback) {
this.imageOnTapCallback = imageOnTapCallback;
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("onSingleTapConfirmed", "onSingleTapConfirmed");
if (null != imageOnTapCallback) {
imageOnTapCallback.tapDetected();
}
return super.onSingleTapConfirmed(e);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("onDoubleTap", "onDoubleTap");
return super.onDoubleTap(e);
}
#Override
public void onLongPress(MotionEvent e) {
Log.d("onLongPress", "onLongPress");
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll", "onScroll");
return super.onScroll(e1, e2, distanceX, distanceY);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("onFling ", "onFling");
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}
Now when i click it identified by the onInterceptTouchEvent in the ViewPagerWithTapGestreDetection class.So i need to view a Horizontal scrollbar when i click.Is it the correct way to call it inside onInterceptTouchEvent ?

Double tap event in android

I need to toast something on double tap the screen. I tried the following code. But it's not working. No toast is coming on double tapping. What is wrong with these code?
package a.b.c;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;
public class SampleActivity extends Activity implements OnDoubleTapListener,OnGestureListener
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onDoubleTap(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onDoubleTap", Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(e.getAction()==1)
{
Toast.makeText(getBaseContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
}
return true;
}
I also implemented the OnGestureListener methodes. But have no effect. Please help.
Where do you set the doubletap listener? Try adding this to your oncreate and see if it fixes the issue
GestureDetector detector = new GestureDetector(this, this);

How to reposition an ImageView

I have some ImageViews in my xml file. In my code section I want to move an image according to my wish. For that, I've done event.getx() and event.getY() and then use imageView.layout(). This process is not working. How can i move that image?
I think this might help:
package com.example.moveimageview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
ImageView im;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the wrapper layout from the xml
rl = (RelativeLayout) findViewById(R.id.relative1);
//getting the ImageView from xml
im = (ImageView) findViewById(R.id.myImageView);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
{
//sending the new coordinates to the method
//that will change the view's location
setImageViewLocation(e.getX(), e.getY());
return true;
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setImageViewLocation(float x, float y)
{
//setting the coordinates
im.setX(x);
im.setY(y);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
im.invalidate();//invoking the view onDraw
}
});
}
}
(Edit) If you want to your ImageView around, here's the onTouchListner code for this (Same code as before just replace the OnTouchListner with this):
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
{
//sending the new coordinates to the method
//that will change the view's location
setImageViewLocation(e.getRawX(), e.getRawY());
return true;
}
if(e.getAction()==android.view.MotionEvent.ACTION_MOVE)
{
setImageViewLocation(e.getRawX(), e.getRawY());
return true;
}
return false;
}
});

Categories

Resources