向ViewPager添加多个功能

时间:2013-05-04 11:50:46

标签: android android-layout android-viewpager

我对Android开发还很陌生,我正在尝试创建一个带有按钮的滑动旋转木马,这些按钮链接到其他活动,这是我到目前为止所拥有的......

Main.java

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);


    //Sliding Carousel controls
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);



}   

//Sliding Carousel controls
class MyPagerAdapter extends PagerAdapter {
    public int getCount() {
        return 5;
    }
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.carousel_1;
            break;
        case 1:
            resId = R.layout.carousel_2;
            break;
        case 2:
            resId = R.layout.carousel_3;
            Button myButton = (Button) findViewById(R.id.paymybill);
            myButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view) {
                   Intent intent = new Intent();
                   setResult(RESULT_OK, intent);
                   finish();
               }

            });
            break;





        case 3:
            resId = R.layout.carousel_4;
            break;
        case 4:
            resId = R.layout.carousel_5;
            break;
        }

        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

我正试图让按钮在案例2上工作(但最终所有案例都有2到4个按钮)。

当我用

将它加载到我的模拟器上时
                Button myButton = (Button) findViewById(R.id.paymybill);
            myButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view) {
                   Intent intent = new Intent();
                   setResult(RESULT_OK, intent);
                   finish();
               }

            });
            break;

注释掉它完美无缺,但当我加入它时,它会在加载时崩溃。

我认为问题是我没有正确地将按钮指向寻呼机视图并且应用程序崩溃,因为main_activity没有我引用的任何按钮或布局。

我花了最近两天挖掘类似的问题,但我不确定我做错了什么,例如,this case

v = inflater.inflate(R.layout.dashboard_media, null);
接缝是同样的问题,但它使用了不同的适配器,我不知道我可以替换什么,我不能(我已经尝试过,没有任何接缝工作),正如我说我是新的,我相信这是我想念的简单事情!

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

理想情况下,替换以下行

Button myButton = (Button) findViewById(R.id.paymybill);

Button myButton = (Button) view.findViewById(R.id.paymybill);

会解决这个问题。

但在您的情况下,carousel_3.xml已经是按钮paymybill 那么在paymybill按钮中添加以下内容

android:OnClick="payMyBill"

并在java中添加一个函数来处理你想要做的事情

public void payMyBill(View view){
  Intent intent = new Intent();
  setResult(RESULT_OK, intent);
  finish();
}