开发者

how to get access of my button in one of my viewpager pages?

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 开发者_JAVA技巧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) {}

    }


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.


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;
    (...)    
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜