如何在一个viewpager页面中访问mybutton?

时间:2017-02-27 09:32:13

标签: android android-viewpager findviewbyid

我是这个android和java的新手。

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

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

这是我的代码:

public class main扩展了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) {}

}

1 个答案:

答案 0 :(得分:1)

你不能像我的朋友ViewPager一样使用findViewById

由于您在适配器中对不同的视图进行了充气,​​因此Android无法找到您引用的Button,因为它不知道哪个xml布局包含它。

你可以做这样的事情......

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


}

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 = null;

        switch (position) {
            case 0:
                view = layoutinflater.inflate(R.layout.page1, null);
                Button bb = (Button) view.findViewById(R.id.first_button);
                bb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "clicked button in first item", Toast.LENGTH_SHORT).show();
                    }
                });
                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;

        }
        ((ViewPager) collection).addView(view);
        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);
    }

就像你可以单独在那些不同的xml布局文件中找到项目一样,当然你可以深入了解并寻找更好的解决方案,但你这样做会导致ANR。试试吧......