2012-08-27 57 views
0

現在我使用TabActivity來顯示我的兩個列表,但現在我想使用Swype與頁面指標,我不知道如何修改下面的類,使其工作。查看傳呼機指標與兩個列表加載從web服務同步

我發現了一個項目位置: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"}; 

回答

1

您需要修改的部分是在PagerAdapter(公共片段的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

所以你必須花費你的2 ListView ListFragment並返回它在這裏

對於該指標,如果你想追溯兼容性我建議你這個庫,很容易實現:http://viewpagerindicator.com/