简体   繁体   English

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

[英]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. 我想从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 我了解它可以正确帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击按钮更改 viewpager2 默认页面? - How can i change viewpager2 default page through button click? 如何禁用任何 android 应用程序,以便在我更改/按下应用程序中的某个按钮之前无法打开它? - How can i disable any android app such that it cannot be opened until i change/press some button in app? 如何使按钮在ViewPager的特定页面中消失? - How can I make button gone in specific page of ViewPager? 如何在viewpager中的视图之间保存按钮按下的状态和值? - How can I save the state and value of button press between views in a viewpager? ViewPager-如何更改外观? -安卓 - ViewPager - How can I change how this looks? - android 如何在android中检测按钮的时间按下持续时间? - How can I detect time press duration of button in android? 如何在 Android Studio 中通过按下按钮启动和停止 for 循环? - How can I start and stop a for loop with the press of a button in Android Studio? 如何在Android中按下按钮拨打号码? - How can I call a number from button press in Android? Android:我如何抓住主页按钮按下 - Android: How i can catch home button press 如何在 viewPager 中使用按钮更改页面? - How to change page with button in viewPager?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM