2015-08-14 2 views
0

Мой код делает разные вещи, но ничего точно такого, что я хочу.Добавить ProgressDialog в NavigationDrawer

Проблема: Когда приложение запускается, дом не показывает. Когда элемент выбран, боковая панель (NavigationDrawer) не скрыта. Он не показывает следующий фрагмент, пока я не выйду за пределы боковой панели.

Пожалуйста, помогите :(


 

 
import java.util.ArrayList; 
 

 
import org.json.JSONException; 
 
import org.w3c.dom.Document; 
 
import org.w3c.dom.Element; 
 
import org.w3c.dom.NodeList; 
 

 
import com.paypal.android.sdk.payments.PayPalAuthorization; 
 
import com.paypal.android.sdk.payments.PayPalConfiguration; 
 
import com.paypal.android.sdk.payments.PayPalService; 
 
import com.paypal.android.sdk.payments.PaymentActivity; 
 
import com.paypal.android.sdk.payments.PaymentConfirmation; 
 

 
import android.annotation.SuppressLint; 
 
import android.app.ActionBar; 
 
import android.app.Activity; 
 
import android.app.Fragment; 
 
import android.app.FragmentManager; 
 
import android.app.ProgressDialog; 
 
import android.content.Context; 
 
import android.content.Intent; 
 
import android.content.SharedPreferences; 
 
import android.content.SharedPreferences.Editor; 
 
import android.content.res.Configuration; 
 
import android.content.res.TypedArray; 
 
import android.net.Uri; 
 
import android.os.Bundle; 
 
import android.support.v4.app.ActionBarDrawerToggle; 
 
import android.support.v4.widget.DrawerLayout; 
 
import android.util.Log; 
 
import android.view.Gravity; 
 
import android.view.Menu; 
 
import android.view.MenuItem; 
 
import android.view.View; 
 
import android.widget.AdapterView; 
 
import android.widget.AdapterView.OnItemClickListener; 
 
import android.widget.ListView; 
 
import android.widget.TextView; 
 
import android.widget.Toast; 
 

 

 

 

 

 
@SuppressLint("NewApi") 
 
public class MainActivity extends Activity { 
 
\t //paypal 
 
\t public static final String TAG = "Inscripción Evento"; 
 
\t public static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX; 
 
\t public static final String CONFIG_CLIENT_ID = "AU3fdxNPtXe8cz2lbvcBYt7jvJ12uA9CeYpQBNZgfHy5CvxCS6SDDGHIp7nqbBMNksrQh9u0zL3zXRnM"; 
 
\t public static final int REQUEST_CODE_PAYMENT = 1; 
 
    public static final int REQUEST_CODE_FUTURE_PAYMENT = 2; 
 
    public static final int REQUEST_CODE_PROFILE_SHARING = 3; 
 
    public static PayPalConfiguration config = new PayPalConfiguration().environment(CONFIG_ENVIRONMENT).clientId(CONFIG_CLIENT_ID).languageOrLocale("es_MX").merchantName("iESNAJ").merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy")).merchantUserAgreementUri(Uri.parse("https://www.example.com/legal")); 
 
\t //fin paypal 
 
\t 
 
\t private DrawerLayout mDrawerLayout; 
 
\t private ListView mDrawerList; 
 
\t private ActionBarDrawerToggle mDrawerToggle; 
 

 
\t // nav drawer title 
 
\t private CharSequence mDrawerTitle; 
 

 
\t // used to store app title 
 
\t private CharSequence mTitle; 
 
\t TextView BarTitulo; 
 

 
\t // slide menu items 
 
\t private String[] navMenuTitles; 
 
\t private TypedArray navMenuIcons; 
 

 
\t private ArrayList<NavDrawerItem> navDrawerItems; 
 
\t private NavDrawerListAdapter adapter; 
 
\t static String URL = ""; 
 
\t iGuepardosVariables variables = new iGuepardosVariables(); 
 

 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_main); 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t  getActionBar().setCustomView(R.layout.iguepardos_action_bar); 
 
\t \t getActionBar().setDisplayShowHomeEnabled(true); 
 
\t \t getActionBar().setHomeButtonEnabled(true); 
 
\t \t getActionBar().setIcon(R.drawable.space); 
 
\t \t //getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
 
\t \t getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.header)); 
 
\t \t getActionBar().setDisplayHomeAsUpEnabled(true);//agregado 
 
\t \t ((ActionBar) getActionBar()).setHomeButtonEnabled(true); 
 
\t \t 
 
\t \t ProgressDialog progressDialog = new ProgressDialog(this); 
 
\t \t \t progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
 
\t \t \t progressDialog.setMessage("Loading..."); 
 
\t \t \t progressDialog.setCancelable(false); 
 
\t \t 
 
\t \t \t 
 
\t \t try 
 
\t \t { 
 
\t \t \t int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); 
 
\t \t  TextView yourTextView = (TextView) findViewById(titleId); 
 
\t \t  yourTextView.setTextColor(getResources().getColor(R.color.list_item_title)); 
 
\t \t  yourTextView.setTextSize(13); 
 
\t \t  yourTextView.setGravity(Gravity.CENTER_HORIZONTAL);//agregado 
 
\t \t  yourTextView.setVisibility(View.INVISIBLE); 
 
\t \t } 
 
\t  catch (Exception e) {} 
 

 
\t \t CreaMenu(); 
 

 
\t \t mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
 
\t \t \t \t R.drawable.menu01, //nav menu toggle icon 
 
\t \t \t \t R.string.app_name, // nav drawer open - description for accessibility 
 
\t \t \t \t R.string.app_name // nav drawer close - description for accessibility 
 
\t \t) { 
 
\t \t \t public void onDrawerClosed(View view) { 
 
\t \t \t \t getActionBar().setTitle(mTitle); 
 
\t \t \t \t // calling onPrepareOptionsMenu() to show action bar icons 
 
\t \t \t \t invalidateOptionsMenu(); 
 
\t \t \t } 
 

 
\t \t \t public void onDrawerOpened(View drawerView) { 
 
\t \t \t \t //getActionBar().setTitle(mDrawerTitle); 
 
\t \t \t \t // calling onPrepareOptionsMenu() to hide action bar icons 
 
\t \t \t \t invalidateOptionsMenu(); 
 
\t \t \t } 
 
\t \t }; 
 
\t \t mDrawerLayout.setDrawerListener(mDrawerToggle); 
 

 
\t \t if (savedInstanceState == null) { 
 
\t \t \t // on first time display view for first nav item 
 
\t \t \t if (savedInstanceState == null) { 
 
\t \t \t \t if (navDrawerItems.get(0).getTitle() != null & navDrawerItems.get(1).getTitle() != null) { 
 
\t \t \t \t \t 
 
\t \t \t \t } 
 
\t \t \t \t else if (navDrawerItems.get(0).getTitle() != null) { 
 
\t \t \t \t \t 
 
\t \t \t \t } else { 
 
\t \t \t \t \t 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
\t 
 
\t private void CreaMenu() 
 
\t { 
 
\t \t this.VerificaPerfilLocal(); 
 
\t \t 
 
\t \t mTitle = mDrawerTitle = getTitle(); 
 
\t \t mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
\t \t mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 
 
\t \t 
 
     //Declaramos el header el caul sera el layout de header.xml 
 
     //View header = getLayoutInflater().inflate(R.layout.header, null); 
 
     //Establecemos header 
 
     //mDrawerList.addHeaderView(header); 
 
\t \t \t \t 
 
\t \t // load slide menu items 
 
\t \t navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 
 
\t \t \t \t \t 
 
\t \t // nav drawer icons from resources 
 
\t \t navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 
 
\t \t   \t 
 
\t \t navDrawerItems = new ArrayList<NavDrawerItem>(); 
 

 
\t \t // adding nav drawer items to array 
 
\t \t 
 
\t \t //Perfil 
 
\t \t if(variables.usuario.equals("")){ 
 
\t \t \t navDrawerItems.add(new NavDrawerItem(R.id.icon, "Usuario", "No ha iniciado sesión", null, null, 0, false)); 
 
\t \t } 
 
\t \t else 
 
\t \t { 
 
\t \t \t navDrawerItems.add(new NavDrawerItem(R.id.icon, "Usuario", variables.usuario, null, null, 0, false)); 
 
\t \t } 
 
\t \t // Home 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
 
\t \t // Calendario de Eventos 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -2))); 
 
\t \t // Resultados 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
 
\t \t // Contacto 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1))); 
 
\t \t // Registra tu Evento 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); 
 
\t \t // Onclick MX 
 
\t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1))); 
 
\t \t 
 
\t \t // Iniciar sesión 
 
\t \t if(variables.usuario.equals("")) 
 
\t \t \t navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1))); 
 
\t \t else 
 
\t \t \t navDrawerItems.add(new NavDrawerItem("Cerrar Sesión", navMenuIcons.getResourceId(6, -1))); 
 
\t \t 
 
\t \t // Recycle the typed array 
 
\t \t navMenuIcons.recycle(); 
 

 
\t \t mDrawerList.setOnItemClickListener((OnItemClickListener) new SlideMenuClickListener()); 
 

 
\t \t // setting the nav drawer list adapter 
 
\t \t adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); 
 
\t \t mDrawerList.setAdapter(adapter); \t 
 
\t } 
 

 

 
\t /** 
 
\t * Slide menu item click listener 
 
\t * */ 
 
\t private class SlideMenuClickListener implements OnItemClickListener { 
 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
 
      final int thePos = position; 
 
      mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){ 
 

 
       @Override 
 
       public void onDrawerClosed(View drawerView) { 
 

 
        boolean wasChecked = !mDrawerList.isItemChecked(thePos); 
 

 
        //Toast.makeText(homeActivity, "Item pulsado: " + wasChecked, Toast.LENGTH_SHORT).show(); 
 
        mDrawerList.setItemChecked(thePos, !wasChecked); 
 
        Fragment fragment = null; 
 
        switch (thePos) { 
 
        /*case 0: 
 
     \t \t \t fragment = new HomeFragment(); 
 
     \t \t \t break;*/ 
 
     \t \t case 1: 
 
     \t \t \t fragment = new HomeFragment(); 
 
     \t \t \t break; 
 
     \t \t case 2: 
 
     \t \t \t fragment = new CalendarioFragment(); 
 
     \t \t \t break; 
 
     \t \t case 5: 
 
     \t \t \t fragment = new ContactoFragment(); 
 
     \t \t \t break; 
 
     \t \t case 6: 
 
     \t \t \t fragment = new OnclickFragment(); 
 
     \t \t \t break; 
 
     \t \t default: 
 
     \t \t \t break; 
 
     \t \t } 
 

 
     \t \t if (fragment != null) { 
 
     \t \t \t try{ 
 
     \t \t \t FragmentManager fragmentManager = getFragmentManager(); 
 
     \t \t \t fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); 
 

 
     \t \t \t // update selected item and title, then close the drawer 
 
     \t \t \t mDrawerList.setItemChecked(thePos, true); 
 
     \t \t \t mDrawerList.setSelection(thePos); 
 
     \t \t \t setTitle(navMenuTitles[thePos]); 
 
     \t \t \t mDrawerLayout.closeDrawer(mDrawerList); 
 
     \t \t \t } 
 
     \t \t \t catch(Exception E) 
 
     \t \t \t { 
 
     \t \t \t \t Log.e("MainActivity", "Error in creating fragment"); 
 
     \t \t \t } 
 
     \t \t } 
 
     \t \t else { 
 
     \t \t \t // error in creating fragment 
 
     \t \t \t Log.e("MainActivity", "Error in creating fragment"); 
 
     \t \t } 
 
     \t } 
 
      }); 
 
     } 
 
    } 
 

 
\t @Override 
 
\t public boolean onCreateOptionsMenu(Menu menu) { 
 
\t \t getMenuInflater().inflate(R.menu.main, menu); 
 
\t \t return true; 
 
\t } 
 

 
\t @Override 
 
\t public boolean onOptionsItemSelected(MenuItem item) { 
 
\t \t // toggle nav drawer on selecting action bar app icon/title 
 
\t \t if (mDrawerToggle.onOptionsItemSelected(item)) { 
 
\t \t \t return true; 
 
\t \t } 
 
\t \t // Handle action bar actions click 
 
\t \t switch (item.getItemId()) { 
 
\t \t case R.id.action_settings: 
 
\t \t \t return true; 
 
\t \t default: 
 
\t \t \t return super.onOptionsItemSelected(item); 
 
\t \t } 
 
\t } 
 

 
    
 
\t /* * 
 
\t * Called when invalidateOptionsMenu() is triggered 
 
\t */ 
 
\t @Override 
 
\t public boolean onPrepareOptionsMenu(Menu menu) { 
 
\t \t // if nav drawer is opened, hide the action items 
 
\t \t boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
 
\t \t menu.findItem(R.id.action_settings).setVisible(!drawerOpen); 
 
\t \t 
 
\t \t CreaMenu(); 
 
\t \t 
 
\t \t return super.onPrepareOptionsMenu(menu); 
 
\t } 
 

 
\t /** 
 
\t * Diplaying fragment view for selected nav drawer list item 
 
\t * */ 
 

 
\t @Override 
 
\t public void setTitle(CharSequence title) { 
 
\t \t mTitle = title; 
 
\t \t getActionBar().setTitle(mTitle); 
 
\t } 
 

 
\t /** 
 
\t * When using the ActionBarDrawerToggle, you must call it during 
 
\t * onPostCreate() and onConfigurationChanged()... 
 
\t */ 
 

 
\t @Override 
 
\t protected void onPostCreate(Bundle savedInstanceState) { 
 
\t \t super.onPostCreate(savedInstanceState); 
 
\t \t // Sync the toggle state after onRestoreInstanceState has occurred. 
 
\t \t mDrawerToggle.syncState(); 
 
\t } 
 

 
\t @Override 
 
\t public void onConfigurationChanged(Configuration newConfig) { 
 
\t \t super.onConfigurationChanged(newConfig); 
 
\t \t // Pass any configuration change to the drawer toggls 
 
\t \t mDrawerToggle.onConfigurationChanged(newConfig); 
 
\t } 
 
\t private void sendAuthorizationToServer(PayPalAuthorization authorization) { 
 

 
     /** 
 
     * TODO: Send the authorization response to your server, where it can 
 
     * exchange the authorization code for OAuth access and refresh tokens. 
 
     * 
 
     * Your server must then store these tokens, so that your server code 
 
     * can execute payments for this user in the future. 
 
     * 
 
     * A more complete example that includes the required app-server to 
 
     * PayPal-server integration is available from 
 
     * https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend 
 
     */ 
 

 
    } 
 
\t @Override 
 
\t public void onActivityResult(int requestCode, int resultCode, Intent data) { 
 
    \t if (requestCode == MainActivity.REQUEST_CODE_PAYMENT) { 
 
      if (resultCode == Activity.RESULT_OK) { 
 
       PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 
 
       if (confirm != null) { 
 
        try { 
 
         Log.i(MainActivity.TAG, confirm.toJSONObject().toString(4)); 
 
         Log.i(MainActivity.TAG, confirm.getPayment().toJSONObject().toString(4)); 
 
         /** 
 
         * TODO: send 'confirm' (and possibly confirm.getPayment() to your server for verification 
 
         * or consent completion. 
 
         * See https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/ 
 
         * for more details. 
 
         * 
 
         * For sample mobile backend interactions, see 
 
         * https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend 
 
         */ 
 
         
 
         Fragment f = MainActivity.this.getFragmentManager().findFragmentById(R.id.frame_container); 
 
       \t \t if (f instanceof EventoFragment) 
 
       \t \t \t EventoFragment.SendEmailConfirmation(variables.direccionIpWS); 
 

 
         Toast.makeText(getApplicationContext(), "Se ha enviado un e-mail con la confirmación de la inscripción.", Toast.LENGTH_LONG).show();       
 
         //Toast.makeText(getApplicationContext(), "PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG).show(); 
 
        } catch (JSONException e) { 
 
         Log.e(MainActivity.TAG, "an extremely unlikely failure occurred: ", e); 
 
        } 
 
       } 
 
      } else if (resultCode == Activity.RESULT_CANCELED) { 
 
       Log.i(MainActivity.TAG, "The user canceled."); 
 
      } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { 
 
       Log.i(MainActivity.TAG,"An invalid Payment or PayPalConfiguration was submitted. Please see the docs."); 
 
      } 
 
     } 
 
\t } 
 
\t @Override 
 
    public void onDestroy() { 
 
     // Stop service when done 
 
\t \t MainActivity.this.stopService(new Intent(MainActivity.this, PayPalService.class)); 
 
     super.onDestroy(); 
 
    } 
 
\t private void VerificaPerfilLocal() 
 
\t { 
 
\t \t try 
 
\t \t { 
 
\t \t \t SharedPreferences preferencias; 
 
\t \t \t preferencias = getSharedPreferences("loginGuepardos", Context.MODE_PRIVATE); 
 
\t \t \t 
 
\t \t \t if(variables.usuario.toString().equals("") && preferencias.contains("ID_EMAIL")) 
 
\t \t \t { 
 
\t \t \t \t variables.usuario = preferencias.getString("ID_EMAIL", null); \t \t \t 
 
\t \t \t } 
 
\t \t } 
 
\t \t catch(Exception e) {} 
 
\t } 
 
}

Если у вас есть идея о том, что это не так, пожалуйста, помогите мне исправить поведение.

ответ

0

Для начала, попробуйте это, он должен скрыть боковую панель, когда вы нажимаете на элемент:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.menu01, //nav menu toggle icon 
      R.string.app_name, // nav drawer open - description for accessibility 
      R.string.app_name // nav drawer close - description for accessibility 
    ) { 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
      getActionBar().setTitle(mTitle); 
      // calling onPrepareOptionsMenu() to show action bar icons 
      invalidateOptionsMenu(); 
     } 

     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      //getActionBar().setTitle(mDrawerTitle); 
      // calling onPrepareOptionsMenu() to hide action bar icons 
      invalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

Теперь я посмотрю глубже в вашу проблему.

+0

это помогает, спасибо! –

Смежные вопросы