昨天,我发布了一个关于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); //最后一页
但这与listview订单冲突。
e.g。列表视图:
1
2
3
当我们使用上面的行时,它显示1 < - 2 < - 3(从3开始)
我希望3&lt; - 2&lt; - 1(从1开始)
答案 0 :(得分:1)
我面对这个问题,我解决了这个库 使用它,你的分页将完美地工作 它与ViewPager一样工作
https://github.com/diego-gomez-olvera/RtlViewPager
<com.booking.rtlviewpager.RtlViewPager
android:id="@+id/arabicViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
arabicViewPager = (ViewPager) findViewById(R.id.arabicViewPager);
adapterViewPager = new Adpt_fragmentPageAdapter(getSupportFragmentManager());
arabicViewPager.setAdapter(adapterViewPager);
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(arabicViewPager);
答案 1 :(得分:0)
感谢AleksG
以下是他建议做的工作以达到所需目的,因此请在列表中添加以下行:
setCurrentItem(count - 1)
然后在光标中,您需要在光标"_desc"
再次感谢。