android swipe action

from Develop/Android 2012.06.15 01:13


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;

}

저작자 표시 비영리 변경 금지
신고

티스토리 툴바