相关疑难解决方法(0)

用活动组内的另一个片段替换片段

我在组活动中有一个片段,我想用另一个片段替换它:

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
SectionDescriptionFragment bdf = new SectionDescriptionFragment();
ft.replace(R.id.book_description_fragment, bdf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
Run Code Online (Sandbox Code Playgroud)

它在没有使用活动组的情况下作为单独的项目完成时工作正常,因为控件进入getview()内部,每个东西都可以在log cat中正常工作,但是没有视图可见,甚至没有出现任何异常,我希望书籍详细信息片段为由部分细节片段替换.

书籍详细信息片段的Xml具有id book_description_fragment,而用于部分描述片段的xml具有id section_description_fragment.

上面的代码在项目的onClick方法中,我希望当用户点击水平滚动视图中的项目时,片段会发生变化.

java android android-fragments

131
推荐指数
4
解决办法
27万
查看次数

getSupportFragmentManager()和getChildFragmentManager()有什么区别?

我的类继承了Fragment,这就是为什么它不能使用getSupportFragmentManager().我正在使用getChildFragmentManager,它显示错误 - IllegalArguementException:找不到id ...错误的视图.

任何指导将不胜感激.

调用AttachmentsListFragment的代码是

Bundle b = new Bundle();
b.putSerializable("AttachmentsList", msg.attachments);  
        AttachmentListFragment listfrag = new AttachmentListFragment(msg.attachments);
FragmentTransaction transaction = getFragmentManager().beginTransaction();       
transaction.add(R.id.attachmentslistcontainer, listfrag);
transaction.addToBackStack(null);
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

attachmentslayout.xml是

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

    <TextView
        android:id="@+id/textViewAttachmentHeader"
        style="@style/Normal.Header.Toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/list_separator_background"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        android:text="@string/attachments_header"
        android:textColor="#FFFFFFFF"
        android:textSize="22sp"
        android:textStyle="bold"
        android:visibility="visible" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

AttachmentsListFragment.java

public class AttachmentListFragment extends ListFragment implements IAttachmentsData {

    ArrayList<Attachments> items = null;
    Integer cellLayoutID;
    Integer index;

    public AttachmentListFragment() {

    } …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-listfragment

64
推荐指数
3
解决办法
4万
查看次数

片段添加或替换不起作用

我正在使用此参考中的代码.

当我在我的程序中输入该代码时,我得到一个错误,如下图所示. 在此输入图像描述

出错的原因是什么? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)

我的主要活动代码:

public void red(View view) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ExampleFragments fragment = new ExampleFragments();
        fragmentTransaction.replace(R.id.frag, fragment);
        fragmentTransaction.commit();
    }
Run Code Online (Sandbox Code Playgroud)

ExampleFragments.java

package com.example.learn.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragments extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments

57
推荐指数
2
解决办法
8万
查看次数

Fragment Transaction上的片段重复

好的,每当我尝试替换我的应用程序中的片段时,它只会将容器内部的片段添加到另一个片段中,并保留当前片段.我试过调用replace并引用包含片段的视图,并引用片段本身.这些都不奏效.我可以使用片段事务管理器将一个片段添加到视图中,但即使我尝试在添加它之后将其删除,它也不起作用.任何帮助,将不胜感激.这是我的文件.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Setup the actionabar. Use setDrawableBackground to set a background image for the actionbar.
    final ActionBar actionbar = getActionBar();
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayUseLogoEnabled(true);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.addTab(actionbar.newTab().setText(R.string.home_tab_text).setTabListener(this),true);
    actionbar.addTab(actionbar.newTab().setText(R.string.insert_tab_text).setTabListener(this));

    Fragment fragment = new insert_button_frag();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.button_fragment, fragment);
    transaction.addToBackStack(null);

    transaction.commit();
}
Run Code Online (Sandbox Code Playgroud)

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/button_fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:name="com.bv.silveredittab.home_button_frag"
            android:id="@+id/button_fragment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <fragment
            android:name="com.bv.silveredittab.quick_insert_frag"
            android:id="@+id/quick_insert_frag"
            android:layout_width="350dip"
            android:layout_height="fill_parent" />

        <fragment
            android:name="com.bv.silveredittab.editor_frag"
            android:id="@+id/editor_frag"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" …
Run Code Online (Sandbox Code Playgroud)

android transactions android-fragments

45
推荐指数
2
解决办法
3万
查看次数

在单个活动中交换片段?

在平板电脑上,我们有两个彼此相邻的片段(相同数据的两个不同视图).在移动设备上,我们只需按一下按钮即可在这两个片段之间切换.移动布局看起来像这样:

<RelativeLayout>
  <fragment id="container" name="fragA"/>
  <ImageButton onClick="swapFragments" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在activity的swapFragments(View)方法中,我试图使用FragmentManager替换fragAfragB:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new FragB());
fragmentTransaction.commit();
Run Code Online (Sandbox Code Playgroud)

...但我总能看到fragA通过的透明部分fragB,导致我相信,它只是把fragB之上fragA,而不是取代它.

我开始hide(Fragment)在事务中使用和类似方法的路径,但这看起来不是正确的方法.

有关如何以正确方式交换这些碎片的任何提示?

编辑:我看到了这个问题的答案.这让我感到困惑,因为我需要能够为平板电脑和手机指定不同的布局.如果我必须以编程方式添加片段,如何避免特定于活动中每个布局的代码(即

 if(TABLET) {
   addFragmentA();
   addFragmentB();
 } else {
   addFragmentA();
 }
Run Code Online (Sandbox Code Playgroud)

layout android android-fragments

6
推荐指数
1
解决办法
1万
查看次数

替换Viewpager中的片段/选项卡

我目前正在尝试为我未来的应用程序制作框架.我非常喜欢ActionBar和ViewPager,但错过了一个功能.我需要在运行时替换Fragment/Tab.

使用官方示例,我希望看到类似于replaceTab()的内容,但无论我做什么,Fragment本身都不会更新.

android fragment android-fragments android-viewpager

6
推荐指数
1
解决办法
4541
查看次数

如何在现有片段上添加另一个片段?

我正在努力创造tab layout with fragments.当我在项目上添加另一个片段时,它应该在它上面显示另一个片段.我无法达到目标.片段刚好超过现有片段,但片段内容都可见.请不要将链接抛向任何开发者页面以获得解释.

这是我的代码

home_activity.xml

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <fragment
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.tabsfragment.School_Fragment" >
            </fragment>

            <fragment
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.tabsfragment.Sports_Fragment" >
            </fragment>
        </FrameLayout>
    </LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

Home_Activity.java

public class Home_Activity extends FragmentActivity {

TabHost mHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity);

    mHost = (TabHost) findViewById(android.R.id.tabhost);

    mHost.setup();

    TabSpec schoolSpec = mHost.newTabSpec("School").setIndicator("School")
            .setContent(R.id.tab1);
    mHost.addTab(schoolSpec);

    TabSpec sportSpec = mHost.newTabSpec("Sports").setIndicator("Sportss")
            .setContent(R.id.tab2); …
Run Code Online (Sandbox Code Playgroud)

android fragment

6
推荐指数
1
解决办法
1万
查看次数

使用Android新片段更新片段

我有一个布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">
        <LinearLayout android:id="@+id/linearLayout13" android:layout_height="wrap_content" android:paddingRight="70dp" android:orientation="vertical" android:paddingLeft="70dp" android:layout_width="wrap_content">
            <ImageView android:id="@+id/baby_icon" android:layout_height="wrap_content" android:src="@drawable/baby" android:clickable="true" android:layout_width="wrap_content"></ImageView>
        </LinearLayout>
    </LinearLayout>
    <fragment
        android:name="com.nicu.health.FragAFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/yellow_cardlist_fragment"
        android:layout_weight="1"
        >
    </fragment>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在启动时,它确实显示了正确的片段(FragAFragment).现在点击按钮baby_icon我尝试删除当前片段并添加一个具有完全不同布局的新片段(FragBFragment).

虽然我看到该onCreateView方法被调用但它确实返回了一个非空视图,但新片段屏幕上的UI没有得到更新.我使用下面的代码来更新片段.

                        Fragment baby = FragBFragment.newInstance(1);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
//                  ft.remove(getFragmentManager().findFragmentById(R.id.yellow_cardlist_fragment));
//                  ft.add(R.id.yellow_cardlist_fragment, baby);
                    ft.replace(R.id.yellow_cardlist_fragment, baby);
                    ft.addToBackStack(null);
                    Log.i(TAG, "Code for commit = "+ft.commit());
Run Code Online (Sandbox Code Playgroud)

我曾尝试过删除,替换和添加的所有组合来获取片段的东西,但是徒劳无功!

我还尝试使用代码作为

<fragment
    android:name="com.nicu.health.FragBFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/yellow_cardlist_fragment"
    android:layout_weight="1"
    >
Run Code Online (Sandbox Code Playgroud)

这确实可以在启动时显示第二个片段!!!!

帮助将在reeally appreicated.

谢谢,

android android-fragments

3
推荐指数
1
解决办法
6076
查看次数