一个活动中有多个片段 [英] Multiple Fragments in One Activity

查看:71
本文介绍了一个活动中有多个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前已经在一个活动中解决了很多碎片的问题,但是我似乎找不到解决我定位问题的方法.

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.

我想在屏幕顶部有一个水平滚动条,并在其下方有一个自定义ListFragment.我在下面发布的代码放置了滚动条,然后ListFragment覆盖了它.当我尝试将托管FrameLayout修改为具有两个FrameLayout时,出现运行时错误.

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.

简单地说,如何将listFragment放在滚动条下方?

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

    }  */
}
 }

这是滚动条片段:

 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

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

最后是托管布局

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

推荐答案

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

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.

这篇关于一个活动中有多个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆