在尝试更改视图Activity
之后,我正尝试启动新的OnNavigationItemSelected
。
我可以正确地改变视图:
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.commit();
setTitle(fragment.getArguments().getString("title"));
drawer.setSelected(true);
drawer.closeDrawer(GravityCompat.START);
Intent intent = new Intent(MainActivity.this, GalleryActivity.class);
startActivity(intent);
return true;
}
有了这个,我想根据所选择的选项更改我想要显示内容的主视图。
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="es.antonio.miBoda.MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
fragment_gallery (我使用此视图更新主视图)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/label"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="45dp"
android:text="Connect"
android:textStyle="bold"/>
<TextView
android:layout_below="@id/label"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Edit fragment_connect.xml to change the appearance"
android:id="@+id/textView2" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/ic_take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_marginBottom="18dp"
android:layout_marginEnd="17dp"
app:srcCompat="@android:drawable/ic_menu_camera" />
</RelativeLayout>
我可以完美地更新视图,但是当我开始新活动时,我无法获得浮动按钮ic_take_picture
,它总是null
。
我的 GalleryActivity
public class GalleryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton takePicture = (FloatingActionButton) findViewById(R.id.ic_take_picture);
if (null != takePicture) {
takePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
}
我可以获取按钮的唯一方法是setContentView(R.layout.fragment_gallery);
但是如果我这样做,我的视图会发生变化,并且不像主视图。
那么,在视图替换之后如何启动新的Activity以使浮动按钮可用?
答案 0 :(得分:0)
<强>解决强>
我已将此添加到我的片段中,而不是创建一个新的Activity:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FloatingActionButton takePicture = (FloatingActionButton) getView().findViewById(R.id.ic_take_picture);
if (null != takePicture) {
takePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
阅读Fragments它非常清楚。