2013-10-03 99 views
2

一个活动中的许多片段的问题已经在这里讨论过了,但我似乎无法找到解决我的定位问题的解决方案。一个活动中的多个片段

我想要一个水平滚动条在屏幕上方,并在它下面定制一个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> 
+0

我想你应该在xml中添加片段以及更多信息只需看看特定章节中的vogella示例 –

+0

我更改了最初发布的代码并将片段放入活动布局中 - 如有人在上面评论。这个想法是沿着屏幕顶部的滚动视图和由listFragment占据的屏幕的其余部分。和我在运行时试图做到这一点一样的问题。滚动条出现在屏幕的顶部,然后listfragment占据整个屏幕并覆盖滚动条。你如何将这两个片段放在另一个之上? – Glenn

回答

1

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

相关问题