android:在pager滑动标签条中用按钮事件更改标签和页面

时间:2014-02-22 14:03:22

标签: android android-fragments tabs pagerslidingtabstrip

我在我的应用程序中使用寻呼机滑动标签条,我想用按钮事件更改标签和寻呼机我该怎么办? 我使用此代码,但它不起作用

public class CheckoutMethod_Fragment extends Fragment {

private static final String ARG_POSITION = "position";

private int position;
private Button btn_continue;
ViewPager pager;


public static CheckoutMethod_Fragment newInstance(int position) {
    CheckoutMethod_Fragment f = new CheckoutMethod_Fragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    position = getArguments().getInt(ARG_POSITION); 
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.activity_checkout_method__fragment,
            container, false);

    final ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
    pager.setAdapter(new ViewPagerAdapter_PlaceCartOrder(getFragmentManager()));

    btn_continue = (Button) v.findViewById(R.id.button_continue);
    btn_continue.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            //Replace right fragment with another fragment

            pager.setCurrentItem(2);
        }
    });
    return v;
}}

这是ViewPagerAdapter_PlaceCartOrder

public class ViewPagerAdapter_PlaceCartOrder extends FragmentPagerAdapter {

private final String[] TITLES = { "Checkout Method","bbbb", "aaa ","ggg", "Order Review" };

public ViewPagerAdapter_PlaceCartOrder(FragmentManager fm) {
    super(fm);
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

@Override
public int getCount() {
    return TITLES.length;
}

@Override
public Fragment getItem(int position) {

    switch (position) {

    case 0:
        return CheckoutMethod_Fragment.newInstance(position);
    case 1:
        return CustomerInformation_Fragment.newInstance(position);
    case 2:
        return BillingInformation_Fragment.newInstance(position);
    case 3:
        return ShippingInformation_Fragment.newInstance(position);
    case 4:
        return ShippingMethod_Fragment.newInstance(position);
    case 5:
        return PaymentInformation_Fragment.newInstance(position);
    case 6:
        return OrderReview_Fragment.newInstance(position);
    case 7:
        return CheckoutMethod_Fragment.newInstance(position);
    case 8:
        return CheckoutMethod_Fragment.newInstance(position);
    case 9:
        return CheckoutMethod_Fragment.newInstance(position);
    default:
        return SuperAwesomeCardFragment.newInstance(6000);
    }

    // return SuperAwesomeCardFragment.newInstance(position);
}}

public class PlaceOrderActivity extends FragmentActivity {

ViewPager pager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_place_order);

    Typeface typeface = Typeface.createFromAsset(getAssets(),
            "fonts/Yekan.ttf");

    // Initialize the ViewPager and set an adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ViewPagerAdapter_PlaceCartOrder(getSupportFragmentManager()));
    //pager.setCurrentItem(7);

    // Bind the tabs to the ViewPager
    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
    tabs.setIndicatorColor(Color.rgb(225, 19, 18));
    tabs.setIndicatorHeight(3);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.place_order, menu);
    return true;
}

public void ChangeFragment(){
    pager.setCurrentItem(2);
}}

1 个答案:

答案 0 :(得分:1)

您的代码有两个问题:

  1. final View v中的onCreateView被作为View v方法的参数传递的onClick所遮蔽。
  2. 您需要先设置寻呼机才能使用它。目前,您使用onClick方法进行设置。您需要将寻呼机设置代码移至onCreateView方法。