我正在尝试向我的应用程序添加滑动手势。我按照教程进行操作,但似乎无法正常工作。我究竟做错了什么?
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class Home extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
GridView gridMenu;
private GestureDetector gestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
gridMenu = (GridView)findViewById(R.id.gridMenu);
gridMenu.setAdapter(new ImageAdapter(this));
gridMenu.setHorizontalScrollBarEnabled(false);
gestureDetector = new GestureDetector(this, new OnSwipeGestureListener());
gridMenu.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?>parent, View v, int position, long id){
Intent url;
Bundle urlBundle = new Bundle();
switch(position){
case 0:
// Public Affairs
url = new Intent(Home.this, PublicAffairs.class);
urlBundle.putString("url", "http://kyfbnewsroom.com/category/public-affairs/feed");
url.putExtras(urlBundle);
startActivity(url);
break;
case 1:
// Action Alerts
url = new Intent(Home.this, ActionAlerts.class);
urlBundle.putString("url", "http://kyfbnewsroom.com/category/public-affairs/notifications/feed");
url.putExtras(urlBundle);
startActivity(url);
break;
case 2:
// Market Updates
url = new Intent(Home.this, MarketUpdates.class);
urlBundle.putString("url", "http://kyfbnewsroom.com/category/market-updates/feed");
url.putExtras(urlBundle);
startActivity(url);
break;
case 3:
// Ag Stories
url = new Intent(Home.this, AgNews.class);
urlBundle.putString("url", "http://kyfbnewsroom.com/category/ag-news/feed");
url.putExtras(urlBundle);
startActivity(url);
break;
case 4:
// KFB News
url = new Intent(Home.this, KFBMagazine.class);
urlBundle.putString("url", "http://kyfbnewsroom.com/category/kfb-news/feed/");
url.putExtras(urlBundle);
startActivity(url);
break;
case 5:
// Member Benefits
Intent member = new Intent(Home.this, MemberBenefits.class);
startActivity(member);
//overridePendingTransition(R.anim.fadeon, R.anim.fadeoff);
break;
case 6:
//Monthly Video
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/user/kentuckyfarmbureau")));
break;
case 7:
// Photos
// Intent flickr = new Intent(Home.this, Flickr.class);
// startActivity(flickr);
//overridePendingTransition(R.anim.fadeon, R.anim.fadeoff);
// Farm Markets
Intent markets = new Intent(Home.this, RSFM.class);
startActivity(markets);
break;
case 8:
// Social Media
Intent social = new Intent(Home.this, SocialMedia.class);
startActivity(social);
//overridePendingTransition(R.anim.fadeon, R.anim.fadeoff);
break;
}
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (gestureDetector.onTouchEvent(event))
{
return true;
}
else
{
return false;
}
}
private class OnSwipeGestureListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float deltaX = e2.getX() - e1.getX();
if ((Math.abs(deltaX) < SWIPE_MIN_DISTANCE) || (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY))
{
return false;
}
else
{
if (deltaX < 0)
{
handleSwipeLeftToRight();
}
else
{
handleSwipeRightToLeft();
}
}
return true;
}
}
private void handleSwipeLeftToRight()
{
System.out.println("Swipe Left to Right");
Intent intent = new Intent(Home.this, RSFM.class);
startActivity(intent);
}
private void handleSwipeRightToLeft()
{
System.out.println("Swipe Right to Left");
Intent intent = new Intent(Home.this, RSFM.class);
startActivity(intent);
}
/*
* This method is used to check whether there is internet connection or not.
*/
public boolean isConnect() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public class ImageAdapter extends BaseAdapter {
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount(){
return MenuIcon.length;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
View v;
if(convertView == null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_layout, null);
}else{
v = convertView;
}
ImageView imgIcon = (ImageView) v.findViewById(R.id.imgIcon);
imgIcon.setImageResource(MenuIcon[position]);
//TextView txtIcon = (TextView) v.findViewById (R.id.txtIcon);
//txtIcon.setText(TextIcon[position]);
return v;
}
private Integer[] MenuIcon = {
R.drawable.publicaffairs, R.drawable.actionalerts, R.drawable.marketupdates,
R.drawable.agnews, R.drawable.kfbmagazine, R.drawable.memberbenefits, R.drawable.monthlyvideo,
R.drawable.roadsidemarkets, R.drawable.socialmedia
};
}
}