无法使用按钮在viewpager中的片段之间滑动

时间:2014-12-01 17:12:52

标签: java android android-fragments android-viewpager

我会使用每个片段中的按钮更改viewpager中的片段。因此,在位置0的片段中,我有一个按钮,onClick我将改变第二个片段(位置1),依此类推。实际上我在位置0的片段中使用此代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.open_welcome_fragment_layout, container,
                             false);
            final WelcomeViewPager pagerV = new WelcomeViewPager();
            Button nextBtnOpen = (Button)view.findViewById(R.id.button_next_open);

            nextBtnOpen.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        ((WelcomeViewPager)getActivity()).setCurrentItem(1, true);
                    }
                });
            return view;    
    }

第1位:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.firstwelcomefrg, container, false);
            final WelcomeViewPager pagerV = new WelcomeViewPager();
            Button nextBtn = (Button)view.findViewById(R.id.button_next_one);

             nextBtn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        ((WelcomeViewPager)getActivity()).setCurrentItem(2, true);
                    }
                });
            return view;
    }

第2位:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.secondwelcomefrg, container,
                             false);
            final WelcomeViewPager pagerV = new WelcomeViewPager();
            Button nextBtnTwo = (Button)view.findViewById(R.id.button_next_two);

            nextBtnTwo.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        ((WelcomeViewPager)getActivity()).setCurrentItem(3, true);
                    }
                });
            return view;    
    }

在我的ViewPager Activity中我创建了这个方法:

public void setCurrentItem (int item, boolean smoothScroll) {
        mViewPager.setCurrentItem(item, smoothScroll);
    }

不幸的是不行。我可以从第一个片段更改为第二个片段,但每个片段都返回到第二个片段。似乎接受的唯一一个位置是第一个。在这种情况下:

((WelcomeViewPager)getActivity()).setCurrentItem(1, true);

为什么会出现这种情况?

2 个答案:

答案 0 :(得分:0)

我认为您要做的是使用标签和每个标签来帮助切换视图。你应该使用actionBar标签,而不是按钮。

This should help。这是一个非常详细的教程。

答案 1 :(得分:0)

你想要Button吗?我用于与听众解决类似的问题。下面的代码示例。也许有帮助。

PagerActivity.java

import com.example.zzztest2.MyPagerAdapter.OnPagerItemSelected;

public class PagerActivity extends Activity implements OnPagerItemSelected  {

ViewPager viewPager;
ArrayList<Bitmap> bitmapArray=null;
ArrayList<String> textArray=null;
ArrayList<String> urlArray=null;  
int pagexi;   

  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reklamapager);

        viewPager = (ViewPager) findViewById(R.id.view_pager);

         //setting adapter
         new readAsync().execute();

  }//end oncreate

  //switch page
  public void pagerItemSelected(int newposition) {

        viewPager.setCurrentItem(newposition);
  }

在onpostexecute Async中设置我的适配器

protected void onPostExecute(String file_url) {

            MyPagerAdapter adapter = new MyPagerAdapter(PagerActivity.this, textArray, urlArray, bitmapArray, PagerActivity.this );
            viewPager.setAdapter(adapter);
            //start position
            int pagexi = 2;
            viewPager.setCurrentItem(pagexi);

        }

MyPagerAdapte.java

public class MyPagerAdapter extends PagerAdapter {


    Context context;
    OnPagerItemSelected mListener;
    ArrayList<String> textArray;
    ArrayList<String> urlArray;
    ArrayList<Bitmap> bitmapArray;
    ImageView imageView;
    TextView textreklama1;
    Button btnSwitchPage;
    public Activity activity;
    String firmax;
    String adresarx;


    MyPagerAdapter(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray, OnPagerItemSelected listener){
        this.context=context;
        this.textArray=textArray;
        this.urlArray=urlArray;
        this.bitmapArray=bitmapArray;

        this.mListener = listener;
    }

    @Override
    public int getCount() {

        return textArray.size();
    }

    public Object instantiateItem(ViewGroup collection, int position) {
         LayoutInflater inflater = (LayoutInflater) collection.getContext()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


         View view = inflater.inflate(R.layout.reklamator_new, null);


            String indexx = textArray.get(position).toString();
            String delims2 = "[;]+";
            String[] riadokxxx = indexx.split(delims2);
            String ucex =  riadokxxx[0].trim();
            String dokx =  riadokxxx[1].trim();



            EditText inputUce = (EditText) view.findViewById(R.id.inputUce);
            inputUce.setText(ucex);                 
            EditText inputDok = (EditText) view.findViewById(R.id.inputDok);
            inputDok.setText(dokx);                 

            btnSwitchPage = (Button) view.findViewById(R.id.btnSwitchPage);
            btnSwitchPage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    //here set new position
                    int xxx=1;
                    mListener.pagerItemSelected(xxx);

                }


            });


         ((ViewGroup) collection).addView(view, 0);
         return view;
         }

         @Override
         public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
         ((ViewGroup) arg0).removeView((View) arg2);
         }

         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
         return arg0 == ((View) arg1);
         }

         //new position
         public interface OnPagerItemSelected {
             void pagerItemSelected(int xxx);
         }


}