2017-09-13 85 views
-1

試圖將片段添加到我的活動中,但未顯示出來。片段未顯示在活動中

public class MainActivity extends AppCompatActivity { 
private static final String TAG = MainActivity.class.getSimpleName(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    WeatherFragment mainFragment = new WeatherFragment(); 
    getFragmentManager() 
      .beginTransaction() 
      .add(R.id.main_weather_container, mainFragment) 
      .commit(); 
    } 
} 

而且還有我的片段:

public class WeatherFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     boolean isAttachedToParent = false; 
     View inflatedView = inflater.inflate(R.layout.main_weather_fragment, container, isAttachedToParent); 
    return inflatedView; 
} 
} 

R.id.main_weather_container - FrameLayout裏在我的MainActivity。
R.layout.main_weather_fragment - 片段的佈局
我在做什麼錯了?
我試過使用FragmentActivity + v4支持片段和fragmentSupportManager,但它沒有任何區別。

MainActivity XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:id="@+id/main_weather_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:padding="@dimen/padding_16_dp" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/words_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" /> 

</LinearLayout> 

更新:這個問題是不是在片段交易或什麼的,我使用的工具:命名空間,這就是爲什麼沒有顯示的片段。比較遺憾的是:(

+0

請使用'getSupportFragmentManager()'而不是'getFragmentManager()'來顯示你想要添加片段的xml片段 –

+1

。 –

+0

我已經提到支持片段管理器沒有幫助 –

回答

0

正如我所看到的,使用的是AppCompatActivity但使用getFragmentManager。 隨着AppCompatActivity您將需要使用getSupportFragmentManager()而非getFragmentManager()

public class MainActivity extends AppCompatActivity { 
private static final String TAG = MainActivity.class.getSimpleName(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    WeatherFragment mainFragment = new WeatherFragment(); 
    getSupportFragmentManager() 
      .beginTransaction() 
      .add(R.id.main_weather_container, mainFragment) 
      .commit(); 
    } 
} 

WeatherFragment應延長android.support.v4.app.Fragment;

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="4" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:id="@+id/main_weather_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:padding="@dimen/padding_16_dp" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/words_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" /> 

</LinearLayout> 

在父佈局中添加WeightSum

+0

我已經提到,它沒有任何區別,我已經試過 –

+1

,並確保您的片段正在擴展支持片段類 –

+0

它擴展支持類和支持片段管理器,但它仍然不起作用 –

0

嘗試這種方式。在片段中的類活動通話片段

Fragment fragment = Video_fragment.newInstance(); 
       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
       transaction.replace(R.id.framecontainer, fragment).commit(); 

public class Video_fragment extends Fragment{ 
    View view; 

    public static Fragment newInstance() { 
     Video_fragment fragment = new Video_fragment(); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     view= inflater.inflate(R.layout.video_frag, container, false); 
     return view; 
    } 
} 
0
public class WeatherFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View inflatedView = inflater.inflate(R.layout.main_weather_fragment, null, false); 
    return inflatedView; 
    } 
} 
0

WeatherFragment創建的Fragment一個實例:在內部

public static WeatherFragment newInstance() { 
    Bundle args = new Bundle(); 
    WeatherFragment weatherFragment = new WeatherFragment(); 
    weatherFragment.setArguments(args); 
    return weatherFragment; 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_layout, container, false); 
} 

ActivityMainActivity

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    WeatherFragment weatherFragment = WeatherFragment.newInstance(); 
    getSupportFragmentManager() 
      .beginTransaction() 
      .replace(R.id.flContainerId, weatherFragment) 
      .commit(); 
} 

你的佈局文件將是:

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