Created
November 11, 2014 15:15
-
-
Save showsky/b9f991b5fd65f774e809 to your computer and use it in GitHub Desktop.
ViewPager onFocus / onUnFocus listener use ViewGroup
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
| <android.support.v4.view.ViewPager | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/main_viewpager" | |
| android:layout_width="fill_parent" | |
| android:layout_height="fill_parent"/> |
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.example.showsky.test; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.graphics.Color; | |
| import android.os.Bundle; | |
| import android.support.v4.view.ViewPager; | |
| import android.util.Log; | |
| import android.view.View; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class MainActivity extends Activity { | |
| private final static String TAG = "MainActivity"; | |
| private Context context = null; | |
| private TestPagerAdapter testPagerAdapter = null; | |
| private ViewPager viewPager = null; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| Log.i(TAG, "onCreate"); | |
| context = this; | |
| if (testPagerAdapter == null) { | |
| testPagerAdapter = new TestPagerAdapter(); | |
| } | |
| setContentView(R.layout.activity_main); | |
| viewPager = (ViewPager) findViewById(R.id.main_viewpager); | |
| viewPager.setAdapter(testPagerAdapter); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| Log.i(TAG, "onResume"); | |
| } | |
| @Override | |
| protected void onPause() { | |
| super.onPause(); | |
| Log.i(TAG, "onPause"); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| Log.i(TAG, "onDestroy"); | |
| } | |
| private class TestPagerAdapter extends ViewGroupPagerAdapter { | |
| private List<Integer> colors = new ArrayList<Integer>(); | |
| public TestPagerAdapter() { | |
| colors.add(Color.BLACK); | |
| colors.add(Color.RED); | |
| colors.add(Color.GREEN); | |
| colors.add(Color.BLUE); | |
| colors.add(Color.CYAN); | |
| colors.add(Color.MAGENTA); | |
| colors.add(Color.YELLOW); | |
| } | |
| @Override | |
| public int getCount() { | |
| return colors.size(); | |
| } | |
| @Override | |
| public View getItem(int position) { | |
| TestViewGroup view = new TestViewGroup(context); | |
| view.index = position; | |
| view.setBackgroundColor(colors.get(position)); | |
| return view; | |
| } | |
| } | |
| } |
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.example.showsky.test; | |
| import android.content.Context; | |
| import android.util.AttributeSet; | |
| import android.util.Log; | |
| import android.view.ViewGroup; | |
| /** | |
| * Created by showsky on 14/11/11. | |
| */ | |
| public class TestViewGroup extends ViewGroup implements ViewGroupPagerAdapter.ViewGroupPagerListener { | |
| private final static String TAG = "TestViewGroup"; | |
| public int index = 0; | |
| public TestViewGroup(Context context) { | |
| super(context); | |
| } | |
| public TestViewGroup(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| public TestViewGroup(Context context, AttributeSet attrs, int defStyle) { | |
| super(context, attrs, defStyle); | |
| } | |
| @Override | |
| protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
| } | |
| @Override | |
| protected void onAttachedToWindow() { | |
| super.onAttachedToWindow(); | |
| Log.d(TAG, "Index: " + index + " onAttachedToWindow"); | |
| } | |
| @Override | |
| protected void onDetachedFromWindow() { | |
| super.onDetachedFromWindow(); | |
| Log.d(TAG, "Index: " + index + " onDetachedFromWindow"); | |
| } | |
| @Override | |
| public void onFocus() { | |
| Log.d(TAG, "Index: " + index + " onFocus"); | |
| } | |
| @Override | |
| public void onUnFocus() { | |
| Log.d(TAG, "Index: " + index + " onUnFocus"); | |
| } | |
| } |
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.example.showsky.test; | |
| import android.support.v4.view.PagerAdapter; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| /** | |
| * Created by showsky on 14/11/11. | |
| */ | |
| public abstract class ViewGroupPagerAdapter extends PagerAdapter { | |
| private final static String TAG = "ViewGroupPagerAdapter"; | |
| private View currentView = null; | |
| public interface ViewGroupPagerListener { | |
| public void onFocus(); | |
| public void onUnFocus(); | |
| } | |
| @Override | |
| public void setPrimaryItem(ViewGroup container, int position, Object object) { | |
| super.setPrimaryItem(container, position, object); | |
| View view = (View) object; | |
| if (view != currentView) { | |
| if (currentView != null && currentView instanceof ViewGroupPagerListener) { | |
| ((ViewGroupPagerListener) currentView).onUnFocus(); | |
| } | |
| if (view != null && view instanceof ViewGroupPagerListener) { | |
| ((ViewGroupPagerListener) view).onFocus(); | |
| } | |
| currentView = view; | |
| } | |
| } | |
| public abstract View getItem(int position); | |
| @Override | |
| public Object instantiateItem(ViewGroup container, int position) { | |
| View view = getItem(position); | |
| container.addView(view); | |
| return view; | |
| } | |
| @Override | |
| public void destroyItem(ViewGroup container, int position, Object object) { | |
| container.removeView((View) object); | |
| } | |
| @Override | |
| public boolean isViewFromObject(View view, Object object) { | |
| return (view == object); | |
| } | |
| public View getCurrentView() { | |
| return currentView; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment