4

昨天发了一个关于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 开始)

4

2 回答 2

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);
于 2017-02-24T07:39:40.850 回答
0

感谢 AleksG

以下是他建议的用于所需目的的操作,因此将以下行添加到列表中:

setCurrentItem(count - 1)

然后在光标中,您需要在光标中设置列表顺序"_desc"

再次感谢。

于 2013-06-13T15:03:41.723 回答