昨天发了一个关于PagerAdapter以及在使用数据库时如何实现的问题。我设法把它整理出来,但是,我不希望 viewpager 向左滑动/滚动我想要相反的(向右滑动)。以下是我的代码:
public class ScrollViewTest6 extends ActionBarAppActivity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS;
private Context cxt;
private AwesomePagerAdapter awesomeAdapter;
TextView tv;
int _id;
int row_numbers;
int row_position;
private DataAdapter mySQLiteAdapter;
private Cursor cursor;
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cxt = this;
awesomeAdapter = new AwesomePagerAdapter(awesomeAdapter);
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
mySQLiteAdapter = new DataAdapter(this);
mySQLiteAdapter.createDatabase();
mySQLiteAdapter.open();
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
_id = extras.getInt("id");
row_numbers = extras.getInt("row_num");
row_position = extras.getInt("row_pos");
NUM_AWESOME_VIEWS = row_numbers;
awesomePager.setCurrentItem(row_position);
cursor = mySQLiteAdapter.get_AllColumns();
this.startManagingCursor(cursor);
this.awesomePager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){
}
@Override
public void onPageSelected(int position){
}
});
}
private class AwesomePagerAdapter extends PagerAdapter{
public AwesomePagerAdapter(PagerAdapter fm) {
}
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public int getItemPosition(Object object) {return POSITION_NONE;}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.mylayout, null);
TextView Title = (TextView)layout.findViewById(R.id.title);
if (cursor != null ) {
if (cursor.moveToPosition(position)) {
//do {
//cursor.moveToPosition(position);
String text = cursor.getString(
cursor.getColumnIndexOrThrow(DataAdapter.KEY_Title));
Title.setText(text);
}
}
((ViewPager) collection).addView(layout,0);
return layout;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
}
我知道有些人建议使用 setCurrentItem(int); //最后一页
但这与列表视图顺序冲突。
例如列表视图:
1
2
3
当我们使用上面的行时,它显示 1 <- 2 <- 3(从 3 开始)
我想要 3 <- 2 <- 1 (从 1 开始)