如何访问我的 viewpager 页面之一中的按钮?

发布于 2024-12-09 13:21:55 字数 3353 浏览 0 评论 0原文

我对这个 android 和 java 很陌生。

我的 viewpager 现在可以滑动我所有的 xml 布局,但我不知道如何访问其中的项目。

我尝试使用 Button mybutton = (Button) findViewById (R.layout.result) ,然后在 mybutton 中设置 onClickListener ,但每次运行时它都被强制关闭它。

这是我的代码:

public class main extends Activity {

    private ViewPager myviewpager;
    private ViewPagerAdapter myviewpageradapter;
    private static int NUM_AWESOME_VIEWS = 12;
    Button button_1;



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

        myviewpageradapter = new ViewPagerAdapter();
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);
        myviewpager.setAdapter(myviewpageradapter);

            button_1 = (Button) myviewpager.findViewById(R.id.first_button);
            button_1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    finish();
                }
            });
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
                return NUM_AWESOME_VIEWS;
        }

        @Override
        public Object instantiateItem(View collection, int position) {
                LayoutInflater layoutinflater = getLayoutInflater();

                View view;
                view = layoutinflater.inflate(R.layout.page1, null);

                switch (position)
                {
                    case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
                    case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
                    case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
                    case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
                    case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
                    case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
                    case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
                    case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
                    case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
                    case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
                    case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
                    case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
                }
                ((ViewPager) collection).addView(view,0);
                return view;
        }

        @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) {}

    }

I'm new to this android and java.

My viewpager now can slide all my xml layout, but i don't know how to access item in them.

I tried using Button mybutton = (Button) findViewById (R.layout.result), then set the onClickListener in mybutton, but then it was forced to close every time I run it.

Here is my code:

public class main extends Activity {

    private ViewPager myviewpager;
    private ViewPagerAdapter myviewpageradapter;
    private static int NUM_AWESOME_VIEWS = 12;
    Button button_1;



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

        myviewpageradapter = new ViewPagerAdapter();
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);
        myviewpager.setAdapter(myviewpageradapter);

            button_1 = (Button) myviewpager.findViewById(R.id.first_button);
            button_1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    finish();
                }
            });
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
                return NUM_AWESOME_VIEWS;
        }

        @Override
        public Object instantiateItem(View collection, int position) {
                LayoutInflater layoutinflater = getLayoutInflater();

                View view;
                view = layoutinflater.inflate(R.layout.page1, null);

                switch (position)
                {
                    case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
                    case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
                    case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
                    case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
                    case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
                    case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
                    case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
                    case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
                    case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
                    case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
                    case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
                    case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
                }
                ((ViewPager) collection).addView(view,0);
                return view;
        }

        @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) {}

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-12-16 13:21:55

这可能只是问题,您的项目可能没有找到,所以尝试这个(如果您的视图不是 LinearLayout 更改它)

LayoutInflater getView = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout view1 =(LineaLayout)getView.inflate(R.layout.your_view,null);

但是您的按钮可能会抛出异常,而且还设置了一个 onClickListener 。
如果只是在 XML 中定义按钮:

android:onClick"buttonClick"

那么在代码中

public void buttonClick(View v){

// do something

}

如果这是不可能的,因为您稍后需要引用按钮,我遇到了同样的问题,并且想知道它是如何工作的。

It may just be the Problem, that your items are not found probably so try this(If your view is no LinearLayout change it)

LayoutInflater getView = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout view1 =(LineaLayout)getView.inflate(R.layout.your_view,null);

But Your Button may throw Exception furthermore there is an onClickListener set to it.
If it's just the Button define it in XML:

android:onClick"buttonClick"

then in Code

public void buttonClick(View v){

// do something

}

If this is not possible because you need to reference the Button later, I stuck with the same Problem and would like to know how it works.

若有似无的小暗淡 2024-12-16 13:21:55

我认为这里的问题是您正在尝试查找尚未初始化(膨胀)的视图(findViewById)。尝试在 ViewPagerAdapter 类型的 instatiateItem 方法内设置按钮侦听器。

例子:

    switch(position){
    case 0 : 
    view = layoutinflater.inflate(R.layout.page1, null); 
    button_1 = (Button) view.findViewById(R.id.first_button);
    button_1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
               finish();
               }
         });
    break;
    (...)    
    }

I think the problem here is that you are trying to find a view (findViewById) that wasn't instatiated (inflated) yet. Try to set the button listener inside the instatiateItem method of ViewPagerAdapter type.

Example:

    switch(position){
    case 0 : 
    view = layoutinflater.inflate(R.layout.page1, null); 
    button_1 = (Button) view.findViewById(R.id.first_button);
    button_1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
               finish();
               }
         });
    break;
    (...)    
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文