2013-02-26 36 views
0

我想多次添加相同的類到viewpager顯示從sqlite數據庫讀取不同的信息。問題是當創建視圖時,總是在當前視圖中覆蓋對象,文本視圖等,而不是在每個頁面的片段中。相同的類/佈局在視圖尋呼機

names = new ArrayList<String>(); 
    for (int d : descritions) { 
     Description temp = DBCompanies.getDescriptionById(db_path, 
       GSSettings.DBCODE, d, Locale.getDefault().getLanguage(), 
       GSSettings.DEFAULTLANGUAGE, false); 
     names.add(temp.getTitle()); 

    } 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
    // mViewPager.setCurrentItem(0); 
    mViewPager.refreshDrawableState(); 
... 

我該如何避免這種行爲?

謝謝。

編輯:

我增加了更多的代碼來澄清我的問題:

framecompany100.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ly_main" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:scrollbarDefaultDelayBeforeFade="500" 
android:scrollbarFadeDuration="200" 
android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb_company" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <Gallery 
     android:id="@+id/ga_image" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="2dp" /> 

    <RelativeLayout 
     android:id="@+id/ly_details" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/ga_image" > 

     <ImageView 
      android:id="@+id/iv_options" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_margin="5dp" 
      android:src="@drawable/ic_button_options" /> 

     <RelativeLayout 
      android:id="@+id/ly_details_text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="5dp" 
      android:layout_toRightOf="@+id/iv_options" 
      android:background="#77ffffff" > 

      <TextView 
       android:id="@+id/tv_address" android:text="kldfjhskdjhfksj" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:textSize="15sp" /> 
     </RelativeLayout> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/tv_description" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/ly_details" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp" 
     android:textSize="15sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

與此佈局的類:

FrameCompany100。 java

public class FrameCompany100 extends Fragment implements OnClickListener, 
    PictureUpdater, FragmentName { 

private Bundle data = new Bundle(); 
private Gallery image; 
private Description description; 
private BaseAdapter adapter; 
private String db_path; 
private GuiParams main_params; 
private Company company; 
private String name = ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    data = this.getArguments(); 
    if (savedInstanceState != null) 
     data = savedInstanceState; 

    db_path = "/data/data/" + getActivity().getPackageName() 
      + GSSettings.DB_PATH; 

    description = DBCompanies.getDescriptionById(db_path, 
      GSSettings.DBCODE, data.getInt("descriptionId"), Locale 
        .getDefault().getLanguage(), "es", false); 
    company = DBCompanies.getCompany(db_path, GSSettings.DBCODE, 
      description.getCompanyId()); 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.framecompany100, container, false); 
} 

void init() { 
    try { 
     Display display = getActivity().getWindowManager() 
       .getDefaultDisplay(); 
     int height = display.getHeight(); 
     final int width = display.getWidth(); 

     // params 
     main_params = DBCompanies.getGuiParams(db_path, GSSettings.DBCODE, 
       description.getCompanyId(), GSSettings.body); 
     if (main_params == null || main_params.getBackgroundcolor() == null 
       || main_params.getBackgroundcolor().equals("") 
       || main_params.getTextcolor() == null 
       || main_params.getTextcolor().equals("")) 
      main_params = DBApp.getGuiParam(db_path, GSSettings.DBCODE, 
        GSSettings.body); 

     // main Layout 
     RelativeLayout ly_main = (RelativeLayout) getActivity() 
       .findViewById(R.id.ly_main); 
     try { 
      ly_main.setBackgroundColor(Color.parseColor(main_params 
        .getBackgroundcolor())); 
     } catch (Exception e) { 
     } 

     Typeface font_body = null; 
     try { 
      font_body = Typeface.createFromAsset(getActivity().getAssets(), 
        "fonts/" + main_params.getFont()); 
     } catch (Exception e) { 
      font_body = Typeface.createFromAsset(
        getActivity().getAssets(), 
        "fonts/" 
          + DBApp.getGuiParam(db_path, GSSettings.DBCODE, 
            GSSettings.body).getFont()); 
     } 

     TextView tv_description = (TextView) getActivity().findViewById(
       R.id.tv_description); 
     try { 
      tv_description.setTextColor(Color.parseColor(main_params 
        .getTextcolor())); 
     } catch (Exception e) { 
     } 
     if (description == null) { 
      tv_description.setText(R.string.nodescription); 
     } else { 
      tv_description.setText(description.getText()); 
     } 

     TextView tv_address = (TextView) getActivity().findViewById(
       R.id.tv_address); 
     try { 
      tv_address.setTextColor(Color.parseColor(main_params 
        .getTextcolor())); 
     } catch (Exception e) { 
     } 
..... 
} 

@Override 
public void onResume() { 
    init(); 
    // ((MainLayout) getActivity()).setDescription(description); 
    super.onResume(); 
} 

的FragmentPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter { 
    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Description temp = DBCompanies.getDescriptionById(db_path, 
       GSSettings.DBCODE, descritions[i], Locale.getDefault() 
         .getLanguage(), GSSettings.DEFAULTLANGUAGE, false); 

     Fragment fragment = null; 
     try { 
      fragment = new CompanyLayout100(); 
      ((FragmentName) fragment).setName(temp.getTitle()); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("descriptionId", temp.getId()); 
      fragment.setArguments(bundle); 
     } catch (Exception e) { 
     } 

     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return descritions.length; 
     // return fragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     String name = ""; 
     try { 
      name = names.get(position); 
     } catch (Exception e) { 
     } 
     return name; 
    } 
} 

發生的事情是,在viewpager創建這些幀的看法,但總是在可見幀輸入數據。我認爲是因爲layout.xml是相同的,所以只有一個id,所有的對象都指向可見的佈局。

回答

0

您需要創建的實例的FragmentPagerAdapter ...

public class PagerAdapter extends FragmentPagerAdapter { 

    int pages; 

    public PagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     return MyFragment.newInstance(position); 
    } 

    public void setPages(int no) { 
     pages = no; 
    } 
} 

因此,在主要活動您將附加PagerAdapter ...

adapter = new PagerAdapter(getSupportFragmentManager()); 
    adapter.setPages(noPages); 
    pager.setAdapter(adapter); 

最後,你需要你的片段,包括newInstance方法。看看這http://android-developers.blogspot.de/2011/08/horizontal-view-swiping-with-viewpager.html更多。

乾杯!

編輯#1:MyFragment; :有必要作爲一個參數傳遞

public class MyFragment extends SherlockFragment { 
    private static final String KEY_POSITION="position"; 

    static MyFragment newInstance(int position) { 
    MyFragment frag=new MyFragment(); 
    Bundle args=new Bundle(); 

    args.putInt(KEY_POSITION, position); 
    frag.setArguments(args); 

    return(frag); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
    View result=inflater.inflate(R.layout.editor, container, false); 
    EditText editor=(EditText)result.findViewById(R.id.editor); 
    int position=getArguments().getInt(KEY_POSITION, -1); 

    editor.setHint(String.format(getString(R.string.hint), position + 1)); 

    return(result); 
    } 
} 
+0

我有一個fragmentpageadapter的位置,不同的對你的建議是,我創建片段的列表,並通過它時,我創建適配器,mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager(),fragments);然後我返回片段,公共片段getItem(int位置){0}返回fragments.get(i);} – 2013-02-26 21:27:03

+0

你的意思是該片段應該在FragmentPagerAdapter中創建? – 2013-02-26 21:33:25

+0

這就是我在很多例子中看到它的方式,以及我如何實現它。工作很好。這個例子看起來更容易:https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Fragments – Trinimon 2013-02-26 21:38:55

相關問題