我正在使用viewpager(带有图片),在viewpager中,我设置了共享首选项,例如当用户启动活动时,他可以在退出活动时看到最后看到的页面...
viewpager的共享引用对我来说很好。我正在使用搜索栏在viewpager中滑动图像,但我如何使用相同的共享偏好 在搜索栏中将搜索栏的位置更新到最后看到的页面或点。 或者如果用户滑动viewpager上的图像,我如何自动更新搜索栏的进度。我是android领域的新手,这是我的第一篇帖子..请有人帮帮我..
我的代码:
public static ViewPager viewPager;
Customswipeadapter adapter;
public static int a;
public static int idName;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
adapter = new Customswipeadapter(this);
viewPager = (ViewPager) findViewById(R.id.viewpager);
seekBar = (SeekBar) findViewById(R.id.seekBar);
viewPager.setAdapter(adapter);
viewPager.setPageTransformer(true, new RotateDownTransformer());
viewPager.setCurrentItem(images_resources.length);
a = viewPager.getCurrentItem();
////SHARED PREFERENCES///
android.content.SharedPreferences prefs = getSharedPreferences("page_store", MODE_PRIVATE);
idName = prefs.getInt("Page_number", 0); //0 is the default value.
Toast.makeText(this, " Name"+idName , Toast.LENGTH_SHORT).show();
if (idName != 0) {
viewPager.setCurrentItem(idName);
//Toast.makeText(viewpager.this,"Page no:"+ idName, Toast.LENGTH_LONG).show();
} else {
//Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();
}
//Seekbar start
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
progress = images_resources.length-progresValue;
viewPager.setCurrentItem(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
//if you want to do anything at the start of
// touching the seekbar
progress = seekBar.getProgress();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
//textView.setText(progress + "/" + seekBar.getMax());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
//this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} ///Seekbar End
//BACK PRESS METHOD
@Override
public void onBackPressed() {
a = viewPager.getCurrentItem();
SharedPreferences.Editor editor = getSharedPreferences("page_store", MODE_PRIVATE).edit();
editor.putInt("Page_number", a);
//Toast.makeText(this, " "+a , Toast.LENGTH_SHORT).show();
editor.commit();
finish();
}
//END OF SHARED PREFERENCES
}
答案 0 :(得分:1)
您可以向viewPager添加addOnPageChangeListener并在onPageSelected中实现他的方法,您可以在%中设置搜索栏的进度。
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
seekbar.setProgress((position * 100)/viewPager.getChildCount());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});