2016-07-12 28 views
0

我有一個帶有三個按鈕的片段。基於按鈕按下我想調用另外三個fragment.my頂部片段如下。使用按鈕調用單個片段中的不同片段

<FrameLayout 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" 
tools:context="com.example.ramakanta_chandra.chechiacc.BlankFragment"> 

<!-- TODO: Update blank fragment layout --> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fragment1" 
    android:background="#e0e0e0" 
    android:autoText="true" /> 

<TextView 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:text="This is Video Segment" 
    android:id="@+id/textView" 
    android:layout_gravity="left|top" 
    android:textAlignment="center" 
    android:textColor="#000000" 
    android:textStyle="normal" /> 

<TextView 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:text="This is Audio Segment" 
    android:id="@+id/textView5" 
    android:layout_gravity="center_horizontal|top" 
    android:textColor="#000000" 
    android:textAlignment="center" /> 

<TextView 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:text="This is Ebook Segment" 
    android:id="@+id/textView6" 
    android:layout_gravity="right|top" 
    android:textAlignment="center" 
    android:textColor="#000000" /> 

<ImageButton 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/videoimageButton" 
    android:layout_below="@+id/textView" 
    android:layout_gravity="top|left" 
    android:src="@mipmap/video" 
    android:layout_marginTop="30dp" 
    android:clickable="false" 
    android:nestedScrollingEnabled="false" /> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/audioimageButton" 
    android:layout_gravity="center_horizontal|top" 
    android:src="@mipmap/audio" 
    android:layout_marginTop="30dp"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageButton3" 
    android:layout_gravity="right|top" 
    android:src="@mipmap/book" 
    android:layout_marginTop="30dp"/> 
<LinearLayout 
    android:id="@+id/fragment2" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" /> 

現在我想調用)命名爲BlankFragment4(三種不同的片段,BlankFragment5(),BlankFragment6()。爲此我的代碼是

import android.app.FragmentManager; 
import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.annotation.Nullable; 
import android.app.Fragment; 
import android.widget.ImageButton; 

public class BlankFragment extends Fragment { 
    View myview; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    myview = inflater.inflate(R.layout.fragment_blank, container, false); 
    final FragmentManager fm3 = getFragmentManager(); 
    ImageButton bt1 = (ImageButton)getView().findViewById(R.id.videoimageButton); 
    bt1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      fm3.beginTransaction() 
        .replace(R.id.fragment2, new BlankFragment4()) 
        .addToBackStack(null) 
        .commit(); 
     } 
    }); 
    ImageButton bt2 = (ImageButton)getView().findViewById(R.id.audioimageButton); 
    bt2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      fm3.beginTransaction() 
        .replace(R.id.fragment2, new BlankFragment5()) 
        .addToBackStack(null) 
        .commit(); 
      // Do something in response to button click 
     } 
    }); 
    ImageButton bt3 = (ImageButton)getView().findViewById(R.id.audioimageButton); 
    bt3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      fm3.beginTransaction() 
        .replace(R.id.fragment2, new BlankFragment6()) 
        .addToBackStack(null) 
        .commit(); 
      // Do something in response to button click 
     } 
    }); 
    // Inflate the layout for this fragment 
    return myview; 
} 

}

這是行不通的。對於這個操作,我沒有在主要活動中寫任何東西。

+0

什麼是實際發生的事情時,你說這是不是工作? –

+0

將ID爲'fragment2'的LinearLayout更改爲FrameLayout,並嘗試將整個佈局放入ScrollView中。 – Drv

+0

關注@ρяσѕρєяK回答...... –

回答

2

正如在這裏看到:

Fragment.getView():

獲取根視圖的片段的佈局(由 onCreateView(LayoutInflater,ViewGroup中,束)返回的一個),如果提供。

您在調用內部onCreateViewgetView方法從中返回片段佈局視圖之前。

所以使用myview View對象訪問視圖的片段從layout.like:

ImageButton bt1 = (ImageButton)myview.findViewById(R.id.videoimageButton); 
+0

非常感謝你@ρяσѕρєяK –