0

同じクラスをビューページャーに数回追加して、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;
    }
}

何が起こっているのかというと、ビューページャーはこれらのフレームのビューを作成しますが、常に表示フレームにデータを入力しています。これは、layout.xml が同じであるため、ID が 1 つしかなく、すべてのオブジェクトが表示されているレイアウトを指しているためだと思います。

4

1 に答える 1

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/horizo​​ntal-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);
  }
}
于 2013-02-26T20:36:29.587 に答える