Created
October 30, 2012 01:32
-
-
Save keyboardr/3977796 to your computer and use it in GitHub Desktop.
Updating a view within a ViewPager
This file contains 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
//This is in the inistantiateItem() method of my PagerAdapter. | |
//View view is my inflated layout, and SharedPreferences prefs contains my backing data. | |
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.transparency_seekbar); | |
seekBar.setMax(80); | |
seekBar.setProgress(prefs.getInt(seekBar.getContext().getString(R.string.pref_transparency), | |
0)); | |
seekBar.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { | |
private IntentFilter mTransparencyFilter = new IntentFilter( | |
ACTION_ADJUST_TRANSPARENCY); | |
private BroadcastReceiver mTransparencyReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
updateTransparency(context); | |
} | |
}; | |
//The work is done here | |
private void updateTransparency(Context context) { | |
seekBar.setProgress(prefs.getInt(context.getString(R.string.pref_transparency), 0)); | |
} | |
@Override | |
public void onViewDetachedFromWindow(View v) { | |
v.getContext().unregisterReceiver(mTransparencyReceiver); | |
} | |
@Override | |
public void onViewAttachedToWindow(View v) { | |
v.getContext().registerReceiver(mTransparencyReceiver, mTransparencyFilter); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment