2015-08-18 58 views
-1

我有一個滑動Activity,與2個滑動頁面,我添加了第一頁的內容和第二頁上的內容是重複的,我怎樣才能設置不同的內容到第二頁在我的滑動視圖?Android Swipe查看幫助需要

public class ListItemClicked extends ActionBarActivity { 

static Bundle extras; 

SectionsPagerAdapter mSectionsPagerAdapter; 
static ImageLoader imageLoader; 
static DisplayImageOptions options; 

ViewPager mViewPager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list_item_clicked); 



    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    extras = getIntent().getExtras(); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 


    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build(); 
    imageLoader = ImageLoader.getInstance(); 
    imageLoader.init(config); 

    //Setup options for ImageLoader so it will handle caching for us. 
    options = new DisplayImageOptions.Builder() 
      .cacheInMemory() 
      .cacheOnDisc() 
      .build(); 



} 


    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
      case 0: 
       return getString(R.string.title_section4).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section5).toUpperCase(l); 
     } 
     return null; 
    } 
} 


public static class PlaceholderFragment extends Fragment { 


    private static final String ARG_SECTION_NUMBER = "section_number"; 


    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

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

     View rootView = inflater.inflate(R.layout.fragment_list_item_clicked, container, false); 


     TextView pDate = (TextView) rootView.findViewById(R.id.textView); 
     pDate.setText(extras.getString("pdate")); 


     TextView ptitle = (TextView) rootView.findViewById(R.id.section_label); 
     ptitle.setText(extras.getString("pname")); 


     TextView pnText = (TextView) rootView.findViewById(R.id.textView2); 
     pnText.setText(extras.getString("pText")); 

     return rootView; 
    } 
} 

} 

回答

1

Android Developers網站對ViewPager確實有很好的解釋。你應該看看: http://developer.android.com/training/animation/screen-slide.html

下面是一個例子,我寫道:

activity_screen_slide.xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

Java代碼:

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
... 

public class ScreenSlidePagerActivity extends FragmentActivity { 

    private static final int NUM_PAGES = 2; 
    private ViewPager mPager; 
    private PagerAdapter mPagerAdapter; 

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

     mPager = (ViewPager) findViewById(R.id.pager); 
     mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
     mPager.setAdapter(mPagerAdapter); 
    } 

    @Override 
    public void onBackPressed() { 
     if (mPager.getCurrentItem() == 0) { 
      super.onBackPressed(); 
     } else { 
      mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
     } 
    } 

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
     public ScreenSlidePagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      //Create these fragments with your preferable names 
      switch (position) { 
       case 0: 
        return new ScreenSlidePageFragment(); 
       case 1: 
        return new ScreenSlidePageFragment2(); 
       default: 
        break; 
      } 
     } 

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

這裏是你的一個看法那應該看起來像是其中fragment_screen_slide_page是您的佈局之一:

import android.support.v4.app.Fragment; 
... 

public class ScreenSlidePageFragment extends Fragment { 

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

     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false); 

     return rootView; 
    } 
} 

你應該真的閱讀Android開發者網站了解更多細節。

1

可以返回不同的片段是這樣的:

@Override 
public Fragment getItem(int position) { 
    if (position == 0) { 
     return PlaceholderFragment.newInstance(position + 1); 
    } else { 
     return anotherFragment.newInstance(); 
    } 
} 

如果你想使用相同的Fragment,你可以改變這取決於要傳遞到片段中的位置的內容。

+0

newInstance(position + 1)函數的參數範圍是什麼? – Andrew

+0

由於您在片段類中有片段的新實例,具體取決於您是否可以更改內容 –

+0

非常感謝! – Andrew