2013-03-08 45 views
1

我在我的數據庫中有問題列表。問題依次顯示在我的頁面上,並帶有上一個/下一個按鈕。 我想添加viewpager在問題之間滑動。但是讓FragmentAdapter的內容讓我困惑。將內容列表獲取到FragmentAdapter(viewpager)

FragmentAdapter:

class TestFragmentAdapter extends FragmentPagerAdapter { 
    protected static final String[] CONTENT = new String[] { "1", "2", "3", }; 

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

    @Override 
    public Fragment getItem(int position) { 
     return TestFragment.newInstance(CONTENT[position]); 
    } 

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

回答

1

當你從數據庫的內容,你不需要使用字符串內容;

您需要使用片段。用textview創建片段,然後爲每個下一個位置填充它。然後用position更新您的內容

public class TestFragment extends Fragment { 

public static TestFragment newInstance(int index) { 

     TestFragment TestFragment = new TestFragment(); 
     Bundle bundle = new Bundle();   
     bundle.putInt("index", index); 
     TestFragment.setArguments(bundle); 
     return TestFragment; 
    } 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
     View view = inflater.inflate(R.layout.yourlayout, container, false); 
      text = (TextView) view.findViewById(R.id.yourtext); 
text.setText("your position: " + getArguments().getInt("index")) // where you get index from your newinstance 
} 
}