0

Sherlock アクション バーと同じスワイプ (左右) 機能を提供したいのですが、TabActivity でどのように実現できますか。onfling() を実装しましたが、達成できませんでした。コードは次のとおりです。

    package com.restaurantapp.pkg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewFlipper;

public class ListViewImagesActivity extends Activity {

    private Cursor food_items;
    public String item_name;
    private String item_descrp;
    private String item_price;
    public Object o;
    private ArrayList<ItemDetails> image_details;
    private String categoryid;
    public int pos;
    private String item_id;
    private SQLiteDatabase db;
    private String category_name_id;
    public Dialog dialog;
    public ViewFlipper viewFlipper;
    public Animation slideLeftIn;
    public Animation slideLeftOut;
    public Animation slideRightIn;
    public Animation slideRightOut;

    public static final int SWIPE_MIN_DISTANCE = 120;

    public static final int SWIPE_MAX_OFF_PATH = 250;

    public static final int SWIPE_THRESHOLD_VELOCITY = 200;

    public GestureDetector gestureDetector;

    View.OnTouchListener gestureListener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        setContentView(R.layout.foodlist_menu);

        Bundle extra = getIntent().getExtras();
        categoryid = extra.getString("categoryId");
        category_name_id = extra.getString("main_categoryId");
        Log.d("category name intent", category_name_id);

        // ----------Animation Files
        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        getFoodItems();
        image_details = GetSearchResults();
        // View on which gesture should function This is image view at bottom of
        // listView on which flip gesture is performed.

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setOnTouchListener(gestureListener);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));


    public class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                    Toast.makeText(ListViewImagesActivity.this, "Left Swipe",
                            Toast.LENGTH_SHORT).show();

                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                    Toast.makeText(ListViewImagesActivity.this, "Right Swipe",
                            Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}
4

1 に答える 1

0

フラグメントを使用すると、スワイプが期待どおりになります

于 2013-02-27T11:41:00.483 に答える