本文介绍了实现对ViewFlipper刷卡动作多GridView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有以下的code听刷卡动作在我ViewFlipper GridView的之间进行导航。然而,当它检测到刷卡不一致。我加入了手势监听到每个GridView和还对在GridView每一个项目。还有什么我失踪?

I have the following code to listen to Swipe action to navigate between GridViews in my ViewFlipper. However, it detects the Swipe inconsistently. I added the Gesture Listener to every GridView and also on every item in the GridView. Is there anything else I'm missing?

全部code是如下:

public class ListNewsActivity extends Activity implements OnGestureListener {

String TAG = "ListNewsActivity";

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

private static final int DIALOG_PLEASE_WAIT = 100;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;

private static int MAX_STREAMS = AppSettings.getStreamList().size();

Context context;

ViewFlipper flipper;

StreamHeader header;

int currentStreamIndex = 0;

ArrayList<StreamNav> navList = new ArrayList<StreamNav>();
ArrayList<StreamGridView> gridList = new ArrayList<StreamGridView>();


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

    context = getApplicationContext();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stream);

    if (AppSettings.userToken == null) {
        AppSettings.getUserToken(context);
    }

    header = (StreamHeader) findViewById(R.id.stream_header);
    setHeaderTitle();

    flipper = (ViewFlipper)findViewById(R.id.stream_flipper);

    boolean loadData = false;
    Log.v(TAG, "No. of streams: " + String.valueOf(AppSettings.getStreamList().size()));
    for (Stream stream : AppSettings.getStreamList()) {
        Log.v(TAG, "Checking Stream  " + stream.getName());
        if (stream.getPosts() == null || stream.getPosts().size() == 0) {
            loadData = true;
            Log.v(TAG, "Empty Stream for " + stream.getBlogId() + ". Data will be loaded.");
            break;
        }
        else {
            Log.v(TAG, "Stream " + stream.getName() + " is populated. Posts: " + String.valueOf(stream.getPosts().size()));
        }
    }

    setupNavAndGrids();

    if (loadData) {
        new LoadStreamTask().execute();
    }
    else {
        populateGrids();
    }

    slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.stream_flipper_slidein_from_right);
    slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.stream_flipper_slideout_to_left);
    slideRightIn = AnimationUtils.loadAnimation(this, R.anim.stream_flipper_slidein_from_left);
    slideRightOut = AnimationUtils.loadAnimation(this, R.anim.stream_flipper_slideout_to_right);

    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

}

public void setupNavAndGrids() {

    LinearLayout llNav = (LinearLayout) findViewById(R.id.stream_nav_layout);

    View.OnClickListener navOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            StreamNav nav = (StreamNav)v;
            navigateToStream(nav.getStreamIndex());

        }
    };

    for (int i = 0; i < MAX_STREAMS; i++) {
        StreamNav nav = new StreamNav(context, null);
        nav.setStreamIndex(i);
        nav.setOnClickListener(navOnClickListener);
        navList.add(nav);
        llNav.addView(nav);

        StreamGridView gv = new StreamGridView(context, null);
        gv.setOnTouchListener(gestureListener);
        gridList.add(gv);

        flipper.addView(gv);

    }

    navList.get(0).setSelected(true);

}

void navigateToStream(int newStreamIndex) {

    for (StreamNav nav : navList) {
        if (nav.getStreamIndex() == newStreamIndex) {
            nav.setSelected(true);
        }
        else {
            nav.setSelected(false);
        }
    }
    Log.v(TAG, "Navigate To Stream: currentStreamIndex=" + String.valueOf(currentStreamIndex) + " newStreamIndex " + String.valueOf(newStreamIndex));
    if (currentStreamIndex < newStreamIndex) {
        int ctr = newStreamIndex - currentStreamIndex;
        for (int i = 0; i < ctr; i++) {
            showNext();
        }
    }
    else {
        int ctr =  currentStreamIndex - newStreamIndex;
        for (int i = 0; i < ctr; i++) {
            showPrevious();
        }
    }
}

public void showNext() {
    Toast.makeText(context, "Show Next Page for Flipper", Toast.LENGTH_LONG);
    flipper.setInAnimation(this, R.anim.stream_flipper_slidein_from_right);
    flipper.setOutAnimation(this, R.anim.stream_flipper_slideout_to_left);
    flipper.showNext();
    setStreamIndex(1);
    setHeaderTitle();
    }

public void showPrevious() {
    Toast.makeText(context, "Show Previous Page for Flipper", Toast.LENGTH_LONG);
    flipper.setInAnimation(this, R.anim.stream_flipper_slidein_from_left);
    flipper.setOutAnimation(this, R.anim.stream_flipper_slideout_to_right);
    flipper.showPrevious();
    setStreamIndex(-1);
    setHeaderTitle();
}

void setStreamIndex(int step) {
    if (step == 1) {
        if (currentStreamIndex == MAX_STREAMS) {
            currentStreamIndex = 0;
        }
        else {
            currentStreamIndex++;
        }
    }
    else {
        // -1 which means previous
        if (currentStreamIndex == 0) {
            currentStreamIndex = MAX_STREAMS - 1;
        }
        else {
            currentStreamIndex--;
        }
    }
}

void setHeaderTitle() {
    header.setTitle(AppSettings.getStreamList().get(currentStreamIndex).getName());
}

void populateGrids() {
    int pos = -1;
    for (StreamGridView sgv : gridList) {
        pos++;
        Log.v(TAG, "Loading Stream for " + AppSettings.getStreamList().get(pos).getName() + "; Posts: " + String.valueOf(AppSettings.getStreamList().get(pos).getPosts().size()));
        ArrayList<Post> posts = AppSettings.getStreamList().get(pos).getPosts();
        sgv.adapter = new LazyPostItemAdapter(ListNewsActivity.this, posts, gestureListener);
        sgv.gvStream.setAdapter(sgv.adapter);

    }
}
class LoadStreamTask extends AsyncTask<Void, String, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_PLEASE_WAIT);

    }

    @Override
    protected Void doInBackground(Void... unused) {

        for (Stream stream : AppSettings.getStreamList()) {

            if (stream.getBlogId().equalsIgnoreCase("popular")) {
                stream.setPosts(Post.getPopularStream());
            }
            else if (stream.getBlogId().equalsIgnoreCase("recent")) {
                stream.setPosts(Post.getRecentStream());
            }
            else {
                Blog blog = Blog.getBlog(stream.getBlogId());
                stream.setPosts(Post.getPostsForBlog(blog));
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {

        populateGrids();
        dismissDialog(DIALOG_PLEASE_WAIT);
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_PLEASE_WAIT:
            ProgressDialog mProgressDialog;
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Loading Streams...");
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.v(TAG, "onFling");
        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) {
                flipper.setInAnimation(slideLeftIn);
                flipper.setOutAnimation(slideLeftOut);
                flipper.showNext();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                flipper.setInAnimation(slideRightIn);
                flipper.setOutAnimation(slideRightOut);
                flipper.showPrevious();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

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


@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    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
    return false;
}


@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}


@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

}

推荐答案

我不断看到了同样的滑动手势检测code遍布计算器,所以我试图把它清理干净一点。这是一个通用的监听器,可以发射在右侧或左侧刷卡事件(很容易地扩展到其他

I keep seeing the same swipe gesture detection code all over StackOverflow, so I have attempted to clean it up a bit. This is a generic listener which can fire events for right or left swipes (easily extended to other

public class SwipeListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private SwipeHandler handler;

    public SwipeHandler getHandler() {
        return handler;
    }

    public void setHandler(SwipeHandler handler) {
        this.handler = handler;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        // left
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            if (handler != null) {
                handler.onLeft();

                return true;
            }

        // right
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            if (handler != null) {
                handler.onRight();

                return true;
            }
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }

    public static abstract class SwipeHandler{
        public void onLeft() {}
        public void onRight() {}
    }
}

然后使用这个任何地方,只需创建一个实例,设置您的刷卡事件处理程序,并将其安装在:

And then to use this anywhere, just create an instance, setup your swipe event handlers, and attach it:

    SwipeListener swipeListener = new SwipeListener();
    swipeListener.setHandler(new SwipeListener.SwipeHandler() {
        @Override
        public void onRight() {
            doSomething();
        }
    });

    someView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

这篇关于实现对ViewFlipper刷卡动作多GridView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:56