视图寻呼机中的相同类/布局

时间:2013-02-26 19:35:45

标签: android android-viewpager fragment

我试图将相同的类多次添加到viewpager,以显示从sqlite DB读取的不同信息。问题在于创建视图时,总是覆盖当前视图中的对象,文本视图等,而不是每个页面的片段。

    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,所有对象都指向可见的布局。

1 个答案:

答案 0 :(得分: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方法的Fragment。查看此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);
  }
}