java新活动OnNavigationItemSelectedListener
我正在尝试在更改视图的一部分OnNavigationItemSelected
之后启动一个新的Activity
。
我能够正确更改视图:
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;
}
有了这个,我想改变我的主视图的一部分,我想根据选择的选项显示一些东西
主要内容。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓: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
安卓:id="@+id/content_frame"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
</安卓.support.constraint.ConstraintLayout>
片段库(我用此视图更新主视图)
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
<TextView
安卓:id="@+id/label"
安卓:layout_alignParentTop="true"
安卓:layout_marginTop="100dp"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center_horizontal"
安卓:textSize="45dp"
安卓:text="Connect"
安卓:textStyle="bold"/>
<TextView
安卓:layout_below="@id/label"
安卓:layout_centerInParent="true"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:textSize="12dp"
安卓:layout_marginTop="10dp"
安卓:gravity="center_horizontal"
安卓:text="Edit fragment_connect.xml to change the appearance"
安卓:id="@+id/textView2" />
<安卓.support.design.widget.FloatingActionButton
安卓:id="@+id/ic_take_picture"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_alignParentEnd="true"
安卓:layout_gravity="bottom|end"
安卓:layout_marginBottom="18dp"
安卓:layout_marginEnd="17dp"
app:srcCompat="@安卓:drawable/ic_menu_camera" />
</RelativeLayout>
我可以完美地更新视图,但当我启动新活动时,我无法获得浮动按钮ic_take_picture
,它总是null
我的厨房活动
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);
,但如果执行此操作,我的视图将发生变化,并且与主视图不同
那么,如何在视图替换后启动新活动,以使浮动按钮可用
# 1 楼答案
已解决
我已将其添加到片段中,而不是创建新活动:
阅读Fragments一切都很清楚