0

タブと一緒にデュアルペインフラグメントを持つための解決策を何日も見つけようとした後、あきらめて、タブレット用の非常に大きなレイアウトで、タブを管理するためのフラグメントのみを持つ独自のリストビューを作成しました。タブ内の古いフラグメントを削除し、リスト内でクリックされた項目に基づいて新しいフラグメントを作成しようとしている 1 つの部分を除いて、すべてのコードが機能します。

タブのフラグメントが生成される場所は次のとおりです。

public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
   public AppSectionsPagerAdapter(FragmentManager fm) {
       super(fm);                                 
    }
   //THESE FRAGMENTS ARE GENERATED WITH TABS AT START

 @Override
 public Fragment getItem(int i) {
     switch (i) {     
     case 0:
            return new DescriptionFragment();
     case 1:
      return new ImagesFragment();
     default:
            return new DescriptionFragment();
  }
}

リストビューのクリックからフラグメントを追加する場所:

public  void addfragment() {   
//THIS FRAGMENT GOES ON TOP AFTER LISTVIEW CLICK
    kick_activity.this.getSupportFragmentManager().popBackStack();
    DescriptionFragment fragment = new DescriptionFragment();
    android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

私のDescriptionFragmentフラグメント:

 public static class DescriptionFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_description, container, false);  
        text = ((TextView) rootView.findViewById(R.id.description));         
        text.setText(Html.fromHtml(description_text));
        return rootView;
 }

私は自分が間違っていることを知っていますが、それを修正する方法がわかりません。getItem() からのフラグメントが作成されているようで、リストビューをクリックして addfragment() が呼び出されると、フラグメントがリンクされていないためにオーバーレイされます。すべてのフラグメントがリンクされ、オーバーレイされないように、「return new DescriptionFragment」の代わりに「case 0」に何を配置できますか?

注 - getItem() で ft.repalce を使用しようとすると、アプリがクラッシュし、LogCat によると、R.id.container が見つからないため、そのコードを単純に移動することはできません。

あなたの助けを前もって感謝します.私はかなりの数日間これに行き詰まっているので、どんな解決策でも大歓迎です.

4

2 に答える 2

0

実行中のコードと addFragment メソッドを使用できない場合は true にしてください。

パブリック クラス ViewPagerExample は FragmentActivity を拡張します

{ プライベート MyAdapter mAdapter; プライベート ViewPager mPager; private static Activity アクティビティ。

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    mAdapter = new MyAdapter(getSupportFragmentManager());
    activity = this;
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
}

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

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

@Override public Fragment getItem(int i) { switch (i) {
case 0: return new DescriptionFragment(); ケース 1: 新しい ImagesFragment() を返します。デフォルト: 新しい DescriptionFragment() を返します。}

}

}

}

于 2013-07-04T11:38:26.213 に答える