简体   繁体   中英

How can I change the page if I press the button? (Android, ViewPager)

I want to load a new page with the given "page" position from editText. How is this possible? Thanks for the help.

@Override
public Object instantiateItem(final ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View item_view = mLayoutInflater.inflate(R.layout.swipe_layout, container, false);
    final ImageView imageView = (ImageView) item_view.findViewById(R.id.fortepanImageView);
    TextView maxPages = (TextView) item_view.findViewById(R.id.fortepanMaxTextView);
    final EditText currentEditText = (EditText) item_view.findViewById(R.id.fortepanEditText);
    Button button = (Button) item_view.findViewById(R.id.fortepanJumpButton);
    imageView.setImageResource(image_resources[position]);
    currentEditText.setText(position + "");
    maxPages.setText(getCount() + "");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int page = Integer.parseInt(currentEditText.getText().toString());
            instantiateItem(imageView, page);
            /*imageView.setImageResource(image_resources[page]);
            currentEditText.setText(page + "");*/
        }
    });

    container.addView(item_view);
    return item_view;
}

MyActivity:

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

Fragment:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      MyEdittext = getText.toString();
      int pageno = Integer.parseInt(MyEdittext.getText().toString());
      ((MyActivity)getActivity()).setCurrentItem (pageno, true);
    }
});

I understand it correctly helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM