我的应用程序往往是一个活动和一些页面。主要活动中有一个导航视图。 Main活动中的第一个视图是登录页面。登录成功后,此页面会转到第二页。 这是问题所在。翻转之后,OnLayoutChangeListener会触发多次,我不知道为什么。 因为我在Java代码的第一部分使用了viewFlipper.setDisplayedChild,但是却没有发生这种情况。但是在第二次调用setDisplayedChild之后,我看到了这个问题。 这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("first activity");
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu menu = navigationView.getMenu();
v_camera = menu.findItem(R.id.nav_camera);
v_contacts = menu.findItem(R.id.nav_contacts);
v_parties = menu.findItem(R.id.nav_parties);
v_gallery = menu.findItem(R.id.nav_gallery);
v_logout = menu.findItem(R.id.nav_logout);
v_manage = menu.findItem(R.id.nav_manage);
v_share = menu.findItem(R.id.nav_share);
v_send = menu.findItem(R.id.nav_send);
vf = findViewById(R.id.viewflipper);
vf.setDisplayedChild(vf.indexOfChild(findViewById(R.id.page1)));
changeNavMenuItems(false);
page1 = findViewById(R.id.page1);
page2 = findViewById(R.id.page2);
SharedPreferences.OnSharedPreferenceChangeListener sharedPref_listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.e("SharedChanged",
"key= " + key + " " + sharedPreferences.getString(key, null));
if (sharedPreferences.getString(key, null) == null)
changeNavMenuItems(false);
else
changeNavMenuItems(true);
}
};
G.sharedPref.registerOnSharedPreferenceChangeListener(sharedPref_listener);
rq = Volley.newRequestQueue(MainActivity.this);
onLayoutChangeListener = new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.e("check view", v.toString());
if (vf.getCurrentView() == page1) {
Button signIn_btn = findViewById(R.id.signInBtn);
signIn_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText userName = findViewById(R.id.username);
EditText passWord = findViewById(R.id.password);
str_username = userName.getText().toString();
str_password = passWord.getText().toString();
StringRequest JsonReq = new StringRequest(Request.Method.POST,
"https://www.ghasedakia.com/core/api/cc_login",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response);
String session_id = null;
try {
JSONObject jsonObj = new JSONObject(response);
session_id = jsonObj.getString("session_id");
} catch (JSONException e) {
e.printStackTrace();
}
if (session_id != null) { ///Authorize successful
Log.e("LOG", session_id);
G.sharedPref.edit().putString("session_id", session_id).apply();
vf.setDisplayedChild(vf.indexOfChild(findViewById(R.id.page2)));
} else { //// not authorized
Log.e("LOG", "User Not found");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error", error.toString());
}
}) {
Map<String, String> params = new HashMap<String, String>();
@Override
public Map<String, String> getParams() {
params.put("password", str_password);
params.put("username", str_username);
return params;
}
};
JsonReq.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
rq.add(JsonReq);
}
});
} else if (vf.getCurrentView() == page2) {
Log.e("layout", "changed to page2");
getContactsFromServer(G.sharedPref.getString("session_id", null));
}
}
};
vf.addOnLayoutChangeListener(onLayoutChangeListener);
}
xml文件: activity_main.xml->
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
app_bar_main.xml->
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="com.ghasedakia.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ViewFlipper
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewflipper">
<include
android:id="@+id/page1" layout="@layout/content_main" />
<include
android:id="@+id/page2" layout="@layout/contacts_page" />
</ViewFlipper>
nav_header_main.xml->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/bg"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/nav_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/nav_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
activity_main_drawer.xml->
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_contacts"
android:icon="@drawable/ic_person_black"
android:title="Contacts" />
<item
android:id="@+id/nav_parties"
android:icon="@drawable/ic_cake_black"
android:title="Parties" />
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/ic_menu_logout"
android:title="Log out" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
page1和page2是我要在其间切换的xml文件。但是问题出现了。
答案 0 :(得分:0)
它用于StringRequest。我更改了onLayoutChangeListener的策略。 我使用animationListener。一切正常。