我尝试使用ViewPager创建登录。
问题是当我在FragmentAccount中按下Login时会转到FragmentPage,但FragmentPage中的信息不会更新。
(我的理论) 对我来说,感觉就像加载FragmentAccount时它还预加载FragmentPage,因此当它加载页面时它使用FragmentPage的非更新信息。虽然,我对Android Studio不太了解......
我认为问题出在前两个类中,但为了以防万一我添加了其他两个。
我非常了解帮助。
我做了测试,数据存储正确。
用户输入登录详细信息并按下登录按钮:
public class FragmentAccount extends Fragment implements View.OnClickListener {
private static final String TAG = "FragmentAccount";
private EditText fieldUsername;
private EditText fieldPassword;
private Button bLogIn;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_account, container, false);
fieldUsername = (EditText) view.findViewById(R.id.fragment_account_field_username);
fieldPassword = (EditText) view.findViewById(R.id.fragment_account_field_password);
bLogIn = (Button) view.findViewById(R.id.fragment_account_button_login);
bLogIn.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
boolean logedIn = MainActivity.user.logIn(fieldUsername.getText().toString(), fieldPassword.getText().toString());
fieldPassword.setText("");
if (logedIn) {
fieldUsername.setText("");
((MainActivity) getActivity()).setViewPager(5);
}
}
}
登录详情应显示在此处:
public class FragmentProfile extends Fragment {
private static final String TAG = "FragmentProfile";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
final TextView usernameText = (TextView) view.findViewById(R.id.fragment_profile_text_username_2);
final TextView passwordText = (TextView) view.findViewById(R.id.fragment_profile_text_password_2);
final Button bLogOut = (Button) view.findViewById(R.id.fragment_profile_button_logout);
String s1 = MainActivity.user.getLogedInUsername();
String s2 = MainActivity.user.getLogedInPassword();
usernameText.setText(s1);
passwordText.setText(s2);
Log.i(TAG, s1 + ", " + s2);
bLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.user.logOut();
((MainActivity) getActivity()).setViewPager(4);
}
});
return view;
}
}
存储登录数据的位置:
public class Users {
private static final String TAG = "Users";
private boolean isUserLogedIn = false;
private String logedInUsername = "";
private String loggedInPassword = "";
final private String adminUsername = "1";
final private String adminPassword = "2";
public Users(){
}
public boolean logIn(String usr, String pass){
if( usr.equals(adminUsername) && pass.equals(adminPassword)){
isUserLogedIn = true;
logedInUsername = usr;
loggedInPassword = pass;
Log.i(TAG, logedInUsername + ", " + loggedInPassword);
return isUserLogedIn;
}else{
return isUserLogedIn;
}
}
public void logOut(){
isUserLogedIn = false;
logedInUsername = "";
loggedInPassword = "";
}
public boolean isUserLogedIn(){
return isUserLogedIn;
}
public String getLogedInUsername(){
return logedInUsername;
}
public String getLogedInPassword(){
return loggedInPassword;
}
}
主要活动,以及我在页面之间切换的地方
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
static final public Users user = new Users();
private SectionsStatePageAdapter mSectionsStagePageAdapter;
private ViewPager mViewPager;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//body
mSectionsStagePageAdapter = new SectionsStatePageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
//sidebar
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
setNavigationListner();
}
private void setNavigationListner(){
NavigationView mNavigationview = (NavigationView) findViewById(R.id.nav_view);
mNavigationview.setNavigationItemSelectedListener(this);
}
/**
* Add all of the fragment to the adapter, so we can change the fragment that is displayed.
*
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager){
SectionsStatePageAdapter adapter = new SectionsStatePageAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentDictionary(), "FragmentDictionary"); //0
adapter.addFragment(new FragmentCheck(), "FragmentCheck"); //1
adapter.addFragment(new FragmentHome(), "FragmentHome");//2
adapter.addFragment(new FragmentPricetag(), "FragmentPricetag");//3
adapter.addFragment(new FragmentAccount(), "FragmentAccount");//4
adapter.addFragment(new FragmentProfile(), "FragmentProfile"); //5
adapter.addFragment(new FragmentHelp(), "FragmentHelp");//7
adapter.addFragment(new FragmentSettings(), "FragmentSettings");//8
adapter.addFragment(new FragmentAboutUs(), "FragmentAboutUs");//9
viewPager.setAdapter(adapter);
setViewPager(2);
}
/**
* Changes the fragment by passing the corresponding i to the fragment in the adapter
* @param fragmentNumber
*/
public void setViewPager(int fragmentNumber){
mViewPager.setCurrentItem(fragmentNumber);
}
public void setmDrawerLayoutVisable(boolean setVisible){
if(setVisible){
mDrawerLayout.openDrawer(Gravity.RIGHT);
}else{
mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_account:
if(user.isUserLogedIn()){
setViewPager(5);
}else{
setViewPager(4);
}
setmDrawerLayoutVisable(false);
break;
case R.id.nav_help:
setViewPager(7);
setmDrawerLayoutVisable(false);
break;
case R.id.nav_settings:
setViewPager(8);
setmDrawerLayoutVisable(false);
break;
case R.id.nav_about_us:
setViewPager(9);
setmDrawerLayoutVisable(false);
break;
default:
break;
}
return false;
}
}
答案 0 :(得分:1)
你的理论确实是正确的。
默认情况下,ViewPager会加载2个额外页面,前一页和后一页。不幸的是,这也是屏幕外页面的最小限制。
您必须实现这些片段相互通信的方式,EventBus或其他方法。