0

现在我使用 TabActivity 来显示我的两个列表,但现在我想使用带有页面指示器的 swype,我不知道如何修改下面的类以使其工作。

我在这里找到了一个项目:http ://www.zylinc.com/blog-reader/items/viewpager-page-indicator.html课程活动是:

public class ViewPagerIndicatorActivity extends FragmentActivity {
PagerAdapter mPagerAdapter;
ViewPager  mViewPager;
ViewPagerIndicator mIndicator;
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static SimpleDateFormat readableDateFormat = new SimpleDateFormat("yyyy - MM/dd");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create our custom adapter to supply pages to the viewpager.
    mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);

    // Start at a custom position
    mViewPager.setCurrentItem(0);

    // Find the indicator from the layout
    mIndicator = (ViewPagerIndicator)findViewById(R.id.indicator);

    // Set the indicator as the pageChangeListener
    mViewPager.setOnPageChangeListener(mIndicator);

    // Initialize the indicator. We need some information here:
    // * What page do we start on.
    // * How many pages are there in total
    // * A callback to get page titles
    mIndicator.init(0, mPagerAdapter.getCount(), mPagerAdapter);
    Resources res = getResources();
    Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow);
    Drawable next = res.getDrawable(R.drawable.indicator_next_arrow);
    mIndicator.setFocusedTextColor(new int[]{255, 0, 0});

    // Set images for previous and next arrows.
    mIndicator.setArrows(prev, next);

    mIndicator.setOnClickListener(new OnIndicatorClickListener());
}

class OnIndicatorClickListener implements ViewPagerIndicator.OnClickListener{
    @Override
    public void onCurrentClicked(View v) {
        Toast.makeText(ViewPagerIndicatorActivity.this, "Hello", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNextClicked(View v) {
        mViewPager.setCurrentItem(Math.min(mPagerAdapter.getCount() - 1, mIndicator.getCurrentPosition() + 1));
    }

    @Override
    public void onPreviousClicked(View v) {
        mViewPager.setCurrentItem(Math.max(0, mIndicator.getCurrentPosition() - 1));
    }

}

class PagerAdapter extends FragmentPagerAdapter implements ViewPagerIndicator.PageInfoProvider{
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, pos - getCount() / 2);
        return ItemFragment.newInstance(cal.getTime());
    }

    @Override
    public int getCount() {
        return 20;
    }

    @Override
    public String getTitle(int pos){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, pos - getCount() / 2);
        return readableDateFormat.format(cal.getTime());
    }
}

public static class ItemFragment extends ListFragment{
    Date date;

    static ItemFragment newInstance(Date date) {
        ItemFragment f = new ItemFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        args.putString("date", sdf.format(date));
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            this.date = sdf.parse(getArguments().getString("date"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.date_fragment, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText(readableDateFormat.format(date));
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, list));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}

public static final String[] list = new String[]{"France", "London", "Sweden", "Denmark", "Germany", "Finland", "Thailand", "Taiwan", "USA", "Norway", "Denmark (again)", "Lithuania", "Bosnia", "Russia", "Vietnam", "Australia"};
4

1 回答 1

1

您必须修改的部分在 PagerAdapter (public Fragment getItem(int pos)) 中:

@Override
public Fragment getItem(int pos) {
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.DAY_OF_MONTH, pos - getCount() / 2);
  return ItemFragment.newInstance(cal.getTime());
}

正是这种方法返回片段以显示在您的 ViewPager 上

因此,您必须使用ListFragment 扩展您的 2 ListView并在此处返回

对于指标,如果您想要复古兼容性,我向您推荐这个库,实现起来非常简单:http: //viewpagerindicator.com/

于 2012-08-27T09:06:47.307 回答