如果按下按钮,如何更改页面? (Android,ViewPager)

时间:2016-09-21 19:59:52

标签: android button onclicklistener android-pageradapter

我想从editText加载一个具有给定“页面”位置的新页面。这怎么可能? 谢谢您的帮助。

@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;
}

1 个答案:

答案 0 :(得分:0)

MyActivity:

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

片段:

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);
    }
});

我明白这对你有帮助