我有一个带有三个按钮的片段。根据按下的按钮,我想调用另外三个片段。我的顶部片段如下所示。
<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 :(得分:2)
如下所示:
获取片段布局的根视图(返回的布局) onCreateView(LayoutInflater,ViewGroup,Bundle)),如果提供的话。
并且您在getView
中调用onCreateView
方法之前从返回片段布局视图。
所以使用myview
View对象从Fragment layout.like中访问View:
ImageButton bt1 = (ImageButton)myview.findViewById(R.id.videoimageButton);