1

我正在为 ViewPager https://github.com/astuetz/PagerSlidingTabStrip使用 PagerSlidingTabStrip 库。

当我按下按钮到特定片段时,我想更改片段。

MainActivity.java:

package com.astuetz.viewpager.extensions.sample;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity {

private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;

private int currentColor = 0xFF96AA39;

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

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);

    tabs.setIndicatorColor(currentColor);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.action_contact:
        QuickContactFragment dialog = new QuickContactFragment();
        dialog.show(getSupportFragmentManager(), "QuickContactFragment");
        return true;

    }

    return super.onOptionsItemSelected(item);
}


public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
            "Top New Free", "Trending" };

    public MyPagerAdapter(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) {
        return SuperAwesomeCardFragment.newInstance(position);
    }

}

}

片段.java:

public class SuperAwesomeCardFragment extends Fragment {

private static final String ARG_POSITION = "position";

private int position;

public static SuperAwesomeCardFragment newInstance(int position) {
    SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
    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) {

    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    FrameLayout fl = new FrameLayout(getActivity());
    fl.setLayoutParams(params);

    final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
            .getDisplayMetrics());

    TextView v = new TextView(getActivity());
    params.setMargins(margin, margin, margin, margin);
    v.setLayoutParams(params);
    v.setLayoutParams(params);
    v.setGravity(Gravity.CENTER);
    v.setBackgroundResource(R.drawable.background_card);
    if(position==0){
        v.setText("dddd");
        Button bb=new Button(getActivity());
        bb.setText("goo");
        **bb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {




            }**
        });




    }else
        v.setText("CARD " + (position + 1));


    fl.addView(v);
    return fl;
}

}
4

1 回答 1

0

为什么不直接使用FragmentManageron 按钮来添加新片段并删除旧片段?一个类似的例子可以在这里看到

于 2013-09-17T21:12:07.843 回答