2016-11-11 193 views
0

對於第一個我製作Android應用程序,學生可以租用房間。現在我開始登錄登錄頁面。從片段到另一個片段

我有一個註冊頁面的問題。在我的應用程序中,可以有兩種類型的用戶註冊他們被稱爲「學生」和「Verhuurder」。在帳戶片段中,有兩個用於註冊頁面的按鈕。我不能讓它工作在兩頁..

我曾與活性片段這麼大的麻煩......

誰能幫助我?

我的代碼:

賬戶片段

public class AccountFragment extends Fragment implements View.OnClickListener{ 


public AccountFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    if (MainActivity.loginId == "") 
    { 
     return inflater.inflate(R.layout.fragment_account, container, false); 
    } 

    else 
    { 
     return inflater.inflate(R.layout.inlog_account, container, false); 
    } 
} 


// BUTTON 
Button btnStudent = (Button) view.findViewById(R.id.btnRegisterStudent); 
btnStudent.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.btnStudent: 
       //what to put here 
       FragmentManager fm = getFragmentManager(); 
       FragmentTransaction ft = fm.beginTransaction(); 
       ft.replace(R.id.fragment_container, new AccountFragmentStudent()); 
       ft.commit(); 
       break; 
     } 
    } 
}); 
} 

XML帳戶片段

<Button 
     android:id="@+id/btnRegisterStudent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFF" 
     android:text="Student op zoek naar een kamer" 
     android:background="@color/colorPrimary" 
     android:onClick="StudentRegister"/> 

MainActivity

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, Callback<LoginResults> { 

    private EditText emailInput; 
    private EditText passwordInput; 
    private HomeFragment fragment; 

    public static String loginId = ""; 
    public static String loginSecret = ""; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    HomeFragment fragment = new HomeFragment(); 
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.fragment_container, fragment); 
    fragmentTransaction.commit(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

public void Login(View v) { 
    emailInput = (EditText) findViewById(R.id.email); 
    passwordInput = (EditText) findViewById(R.id.password); 
    String email = emailInput.getText().toString(); 
    String password = passwordInput.getText().toString(); 
    doLogin(email, password); 
} 

public void onResponse(Response<LoginResults> response) { 
    if (response.isSuccess() && response.body() != null) { 

     loginId = response.body().clientId; 
     loginSecret = response.body().clientSecret; 
     fragment.setLoginToken1(loginId); 
     fragment.setLoginToken2(loginSecret); 

     new AlertDialog.Builder(MainActivity.this) 
       .setTitle("Gelukt") 
       .setMessage("U bent ingelogd") 
       .setCancelable(false) 
       .setPositiveButton("oke", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
        } 
       }).create().show(); 

     // Create fragment and give it an argument specifying the article it should show 
     InlogAccountFragment newFragment = new InlogAccountFragment(); 
     Bundle args = new Bundle(); 
     newFragment.setArguments(args); 

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 


     transaction.replace(R.id.fragment_container, newFragment); 
     transaction.addToBackStack(null); 


     transaction.commit(); 

    } 
    else 

     new AlertDialog.Builder(MainActivity.this) 
       .setTitle("Mislukt") 
       .setMessage("Uw inloggegevens zijn incorrect") 
       .setCancelable(false) 
       .setPositiveButton("oke", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
        } 
       }).create().show(); 
    { 

    } 
} 

public void onFailure(Throwable t) { 

    new AlertDialog.Builder(MainActivity.this) 
      .setTitle("Er is iets fouts gegaan") 
      .setMessage("Probeer opnieuw") 
      .setCancelable(false) 
      .setPositiveButton("oke", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
       } 
      }).create().show(); 



} 
public void doLogin(String email, String password){ 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("BLABLA") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    Login service = retrofit.create(Login.class); 

    service.loginResults(
      email, 
      password 

    ).enqueue(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 

     HomeFragment fragment = new HomeFragment(); 
     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_favorieten) { 

     FavoriteFragment fragment = new FavoriteFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_berichtenbox) { 

     MessageFragment fragment = new MessageFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_account) { 

     AccountFragment fragment = new AccountFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_instellingen) { 

     SettingsFragment fragment = new SettingsFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 


    } 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 


} 
+0

您可以使用EventBus庫。 [鏈接](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&sqi=2&ved=0ahUKEwjUl4L-yKDQAhWKuRQKHdnnB34QFgglMAE&url=http%3A%2F%2Fgreenrobot.org% 2Feventbus%2F&USG = AFQjCNFjPWDQF013NE2rSeW1MrhsSvQVAg&BVM = bv.138169073,d.d24) –

回答

1

我一般儘量保持我的片段亮和decoupl編輯,並將繁重的工作(特別是與其他片段的交互)委託給正在使用它們的活動。你可能會想嘗試以下方法:

嘗試添加監聽器接口您AccountFragment - 這樣的事情:

public interface Listener { 
    void onStudentRegistrationSelected(); 
    void onVerhuurderRegistrationSelected(); 
} 

您還需要添加:

// Member declaration at the top of the fragment class 
private Listener mListener; 

@Override public void onAttach(Context context) { 
    super.onAttach(context); 
    try { 
     mListener = (Listener) context; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(context.toString() + " must implement " + 
       Listener.class.getSimpleName() + "."); 
    } 
} 

這將將使用片段的活動設置爲偵聽器。

然後,設置AccountFragment中相應按鈕的點擊處理程序,以在偵聽器上調用適當的方法。是這樣的:

view.findViewById(R.id.btnRegisterStudent).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     mListener.onStudentRegistrationSelected(); 
    } 
} 

回到MainActivity,實現AccountFragment.Listener接口,重寫收聽者的方法,以及創建和提交相應的註冊片段中的每個收聽方法覆蓋的 - 例如,onStudentRegistrationSelected()將顯示AccountFragmentStudent片段。此外,如果將此添加到片段backstack中,那麼如果用戶在AccountFragmentStudent上點擊後退按鈕,該用戶將返回到AccountFragment,這是一個很好的UX。

希望有幫助!

相關問題