Android 一个活动中有多个片段
一个活动中的多个片段的问题以前已经在这里讨论过,但是我似乎找不到解决定位问题的方法 我想在屏幕顶部有一个水平滚动条,下面有一个自定义的ListFragment。下面我发布的代码显示了滚动条,然后ListFragment覆盖了它。当我试图修改宿主框架布局以拥有两个框架布局时,会出现运行时错误 简单地说,如何将listFragment定位在滚动条下Android 一个活动中有多个片段,android,android-fragments,Android,Android Fragments,一个活动中的多个片段的问题以前已经在这里讨论过,但是我似乎找不到解决定位问题的方法 我想在屏幕顶部有一个水平滚动条,下面有一个自定义的ListFragment。下面我发布的代码显示了滚动条,然后ListFragment覆盖了它。当我试图修改宿主框架布局以拥有两个框架布局时,会出现运行时错误 简单地说,如何将listFragment定位在滚动条下 public class GoogleNewsMainActivity extends FragmentActivity{ public stati
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();
}
}
这是列表片段
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;
}
公共类GoogleNewsMainFragment扩展ListFragment实现LoaderCallbacks{
私有静态最终字符串列\u ID=“ID”;
公共最终静态字符串NEWSFROMSERVICE=“NEWSFROMSERVICE”;
静态图像视图缩略图;
选择静态字符串msection;
私人新闻中心;
私有ListView lv;
专用新闻光标适配器;
公共静态最终字符串ID=“ID”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRetainInstance(真);
设置选项菜单(真);
mSectionSelected=“;//直到他们选择一个
getLoaderManager().initLoader(0,null,this);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图v=充气机。充气(R.layout.fragment\u google\u news\u main,container,false);
getActivity().setTitle(R.string.news\u list\u title);
返回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,我得到了我想要的。上面的所有代码现在都可以工作了。我想你也应该在xml中添加片段,以了解更多信息,只需查看特定章节中的vogella示例我更改了正如最初发布的那样,将片段放在活动布局中-正如上面评论中的某人所建议的那样。这个想法是让滚动视图沿着屏幕顶部,屏幕的其余部分被listFragment占据。与我在运行时尝试这样做时遇到的问题相同。滚动条出现在屏幕顶部,然后然后listfragment占据整个屏幕并覆盖滚动条。如何将两个片段一个放置在另一个之上???