我试图用导航抽屉创建BaseActivity并将其扩展到其他活动中,以便我可以重用导航抽屉代码。
到目前为止我已经弄清楚了:
但是,当我在一个活动(例如activityA)中有一个按钮并且想要加载另一个活动(activityB),通过单击该按钮来显示吐司等时,除非我编写,否则点击侦听器将不起作用BaseActivity的onCreate方法中用于侦听器的代码。
这对我来说是没有意义的,因为它迫使我在BaseActivity内编写所有代码,而这不是编写代码的最有效方式(活动的行为类似于片段,因此我只能使用片段)。
我想知道的是,如何在扩展BaseActivity的所有活动上加载导航抽屉,并仍然允许活动保留其行为。
代码示例
我的activity_home.xml(BaseActivity)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
tools:openDrawer="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/custom_tool"
layout="@layout/custom_bar"/>
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_display"
app:menu="@menu/nav_drawer_menu"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml(activityA)
<?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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="147dp"
android:layout_marginStart="105dp"
android:layout_marginLeft="105dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="105dp"
android:layout_marginRight="105dp"
android:layout_marginBottom="48dp"
android:contentDescription="@string/app_logo"
app:layout_constraintBottom_toTopOf="@+id/search_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@mipmap/ic_launcher" />
<SearchView
android:id="@+id/search_bar"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="50dp"
android:background="@drawable/input_default"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/search_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<Button
android:id="@+id/search_button"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:layout_marginBottom="228dp"
android:background="@drawable/button_default"
android:text="@string/search"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_bar" />
</android.support.constraint.ConstraintLayout>
当应用最初加载时,BaseActivity的“ main_content”会被activity_main布局夸大。
当我单击activity_main.xml中的search_button时,我想加载另一个活动,该活动仅在我在基本活动中编写代码时发生。
家庭活动
package com.example.naisse;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class HomeActivity extends AppCompatActivity {
protected static int position;
private DrawerLayout drawer;
protected FrameLayout frames;
Button searchButton;
/**
* This flag is used just to check that launcher activity is called first time
* so that we can open appropriate Activity on launch and make list item position selected accordingly.
* */
private static boolean isLaunch = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.custom_tool);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
frames = findViewById(R.id.main_container);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
/*inflating the layout with activity_main*/
getLayoutInflater().inflate(R.layout.activity_main, frames);
searchButton = findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*removing the activity_main and inflating they layout with activity_result*/
frames.removeAllViews();
getLayoutInflater().inflate(R.layout.activity_result, frames);
}
});
}
@Override
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
}
}
使用HomeActivity扩展MainActivity
MainActivity
package com.example.naisse;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends HomeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
如何解决此问题?
答案 0 :(得分:0)
有关答案和理解,请阅读我和Mike M之间的讨论。
我最初的工作是使用布局夸大“ main_container”,因此没有触发Activity类。为此,我必须重写BaseActivity(在我的情况下为HomeActivity)内部的 setContentView()方法,并将所有可共享的代码(例如工具栏和导航抽屉)放入其中。
我新的HomeActivity.class
import numpy as np
OAGTokensWOStop = []
for i in range(2708):
for tweet in OAG_Tokenized[i]:
if tweet not in stop_words:
OAGTokensWOStop.append(np.array(tweet))
现在,所有活动都共享工具栏和导航抽屉。