Created
January 7, 2016 09:54
-
-
Save longkai/14e477378ab1bb7355b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xiaolongtongxue.vp1; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
/** | |
* A customized view pager which listens when user is interacting with itself. | |
*/ | |
public class InteractingListenViewPager extends ViewPager { | |
public InteractingListenViewPager(Context context) { | |
super(context); | |
} | |
public InteractingListenViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override public boolean onTouchEvent(MotionEvent ev) { | |
switch (ev.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
case MotionEvent.ACTION_MOVE: | |
if (interactingListener != null) { | |
interactingListener.on(); | |
} | |
break; | |
case MotionEvent.ACTION_UP: | |
case MotionEvent.ACTION_CANCEL: | |
if (interactingListener != null) { | |
interactingListener.off(); | |
} | |
break; | |
} | |
return super.onTouchEvent(ev); | |
} | |
private InteractingListener interactingListener; | |
public void setInteractingListener(InteractingListener interactingListener) { | |
this.interactingListener = interactingListener; | |
} | |
public interface InteractingListener { | |
/** user is interacting with the view pager */ | |
void on(); | |
/** user has no interactions with the view pager */ | |
void off(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xiaolongtongxue.vp1; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.TypedValue; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private static final int DELAY_MILLIS = 2000; | |
private InteractingListenViewPager viewPager; | |
private Runnable timer = new Runnable() { | |
@Override public void run() { | |
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); | |
viewPager.postDelayed(this, DELAY_MILLIS); | |
} | |
}; | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.view_pager); | |
viewPager = (InteractingListenViewPager) findViewById(R.id.view_pager); | |
viewPager.setPageMargin((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); | |
viewPager.setAdapter(new SimpleAdapter()); | |
viewPager.setInteractingListener(new InteractingListenViewPager.InteractingListener() { | |
@Override public void on() { | |
viewPager.removeCallbacks(timer); | |
} | |
@Override public void off() { | |
viewPager.postDelayed(timer, DELAY_MILLIS); | |
} | |
}); | |
} | |
@Override protected void onResume() { | |
super.onResume(); | |
viewPager.postDelayed(timer, DELAY_MILLIS); | |
} | |
@Override protected void onPause() { | |
super.onPause(); | |
viewPager.removeCallbacks(timer); | |
} | |
static class SimpleAdapter extends PagerAdapter { | |
private static final int[] COLORS = new int[]{ | |
Color.parseColor("#673AB7"), | |
Color.parseColor("#FF5722"), | |
Color.parseColor("#E91E63"), | |
}; | |
private static final int REAL_COUNT = COLORS.length; | |
private final String[] strings; | |
public SimpleAdapter() { | |
strings = new String[REAL_COUNT]; | |
for (int i = 0; i < REAL_COUNT; i++) { | |
strings[i] = String.format("Page %d", i); | |
} | |
} | |
@Override public int getCount() { | |
return Integer.MAX_VALUE; | |
} | |
@Override public boolean isViewFromObject(View view, Object object) { | |
return view == object; | |
} | |
@Override public void destroyItem(ViewGroup container, int position, Object object) { | |
container.removeView((View) object); | |
} | |
@Override public Object instantiateItem(ViewGroup container, int position) { | |
final int i = position % REAL_COUNT; | |
final Context context = container.getContext(); | |
TextView tv = new TextView(context); | |
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | |
tv.setGravity(Gravity.CENTER); | |
tv.setTextColor(Color.WHITE); | |
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); | |
tv.setBackgroundColor(COLORS[i]); | |
tv.setText(strings[i]); | |
container.addView(tv); | |
return tv; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<com.xiaolongtongxue.vp1.InteractingListenViewPager | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/view_pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment