一个活动中的多个碎片

时间:2013-10-03 18:30:08

标签: android android-fragments

之前已经介绍过一个活动中的许多碎片的问题,但我似乎无法找到解决我的定位问题的方法。

我想在屏幕顶部有一个水平滚动条,在它下面有一个自定义的ListFragment。我在下面发布的代码会弹出滚动条,然后ListFragment会覆盖它。当我尝试修改托管FrameLayout以具有两个FrameLayouts时,我得到运行时错误。

简单地说,如何将listFragment置于滚动条下?

 public class GoogleNewsMainActivity extends FragmentActivity{

public static Context c;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    c=this;

    setContentView(R.layout.activity_fragment);


     /* FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

   if (fragment == null) {
        Scroll_Menu_Fragment scrollFragment = new Scroll_Menu_Fragment();
        fm.beginTransaction()
            .add(R.id.fragmentContainer, scrollFragment)
            .commit();

        GoogleNewsMainFragment f = new GoogleNewsMainFragment();
        fm.beginTransaction()
            .add(R.id.fragmentContainer, f)
            .commit();  

    }  */
}
 }

这是滚动条片段:

 public class Scroll_Menu_Fragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.scrollbar_fragment, container, false);
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

 }

这是ListFragment

 public class GoogleNewsMainFragment extends ListFragment implements LoaderCallbacks<Cursor> {

private static final String COLUMN_ID = "id";
public final static String NEWSFROMSERVICE = "NEWSFROMSERVICE";



static ImageView thumbnail;
static String mSectionSelected;
private NewsCursor mCursor;
private ListView lv;

private NewsCursorAdapter adapter;


public static final String ID = "id";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setRetainInstance(true);

    setHasOptionsMenu(true);

    mSectionSelected = "";  // until they select one



    getLoaderManager().initLoader(0, null, this);




}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {



    View v = inflater.inflate(R.layout.fragment_google_news_main, container, false);

    getActivity().setTitle(R.string.news_list_title);

    return v;
}

最后是主机布局

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/fragmentContainer"
   android:baselineAligned="false"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" > 

  <fragment
    android:id="@+id/scrollFragment"
    android:layout_width="fill_parent"
    android:layout_weight="10"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    class="com.gilchrist.android.googlenews.Scroll_Menu_Fragment" ></fragment>

  <fragment
    android:id="@+id/listFragment"
    android:layout_width="fill_parent" 
    android:layout_weight="90"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    class="com.gilchrist.android.googlenews.GoogleNewsMainFragment" ></fragment>
  </LinearLayout>

1 个答案:

答案 0 :(得分:1)

我刚刚更新了活动布局。问题是由我使用的重量值引起的。通过90和10我得到了我想要的。上面的所有代码现在都可以使用。