简体   繁体   中英

Multiple Fragments in One Activity

The issue of many fragments in one activity has been covered here before, but I cannot seem to find a solution for my positioning problem.

I want a horizontal scroll bar on top of the screen and under it a custom ListFragment. The code I am posting below puts up the scroll bar and then the ListFragment overwrites it. When I try to modify the hosting FrameLayout to have two FrameLayouts I get runtime errors.

Simply, How can I position the listFragment under the scrollbar?

 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();  

    }  */
}
 }

Here is the scroll bar fragment:

 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();
}

 }

This is the 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;
}

And finally the hosting layout

 <?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>

I just updated the activity layout. The problem was caused by the weight values I was using. By going to 90 and 10 I got exactly what I wanted. All of the code above now works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM