2014-11-03 178 views
1

我正在一個應用程序中,我有一個導航抽屜。導航抽屜緩慢打開

開始時,當抽屜被打開時,它工作得很快且平穩。但是我在onCreateView()中添加了一些代碼,用於填充listView的數據,其中包含7個文本和1個小圖像,顯着減慢。我怎樣才能改善抽屜的行爲?

這裏面的代碼我`onCreateView()〜(這是在一個PlaceHolderFragment)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    startDate=(EditText)rootView.findViewById(R.id.startDate); 
    endDate=(EditText)rootView.findViewById(R.id.endDate); 

    setDates(); 

    final DatePickerDialog.OnDateSetListener datePickerListenerInicio = new DatePickerDialog.OnDateSetListener() { 
     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) { 
      selectedMonth++; 

      day = selectedDay; 
      month = selectedMonth-1; 
      year = selectedYear; 

      String temp; 

      if(selectedDay < 10) 
      { 
       temp = "0"+selectedDay+"/"; 
      } 
      else 
      { 
       temp = selectedDay+"/"; 
      } 

      if(selectedMonth < 10) 
      { 
       temp +="0"+selectedMonth+"/"; 
      } 
      else 
      { 
       temp +=selectedMonth+"/"; 
      } 

      temp +=selectedYear; 
      fechaInicio=temp; 
      startDate.setText(fechaInicio); 
     } 
    }; 

    startDate.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus){ 
       try 
       { 
        DatePickerDialog datePicker = new DatePickerDialog(getActivity(), 
          datePickerListenerInicio, 
          year, 
          month, 
          day); 
        datePicker.setCancelable(true); 
        datePicker.setTitle("Seleccione la fecha"); 
        datePicker.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS); 
        datePicker.show(); 
       } 
       catch (Exception e) 
       { 

       }finally { 
        startDate.clearFocus(); 
       } 
      } 
     } 
    }); 

    final DatePickerDialog.OnDateSetListener datePickerListenerFinal = new DatePickerDialog.OnDateSetListener() { 
     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) { 
      selectedMonth++; 

      day = selectedDay; 
      month = selectedMonth-1; 
      year = selectedYear; 

      String temp; 

      if(selectedDay < 10) 
      { 
       temp = "0"+selectedDay+"/"; 
      } 
      else 
      { 
       temp = selectedDay+"/"; 
      } 

      if(selectedMonth < 10) 
      { 
       temp +="0"+selectedMonth+"/"; 
      } 
      else 
      { 
       temp +=selectedMonth+"/"; 
      } 

      temp +=selectedYear; 
      fechaFinal=temp; 
      endDate.setText(fechaFinal); 
     } 
    }; 

    endDate.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus){ 
       try 
       { 
        DatePickerDialog datePicker = new DatePickerDialog(getActivity(), 
          datePickerListenerFinal, 
          year, 
          month, 
          day); 
        datePicker.setCancelable(true); 
        datePicker.setTitle("Seleccione la fecha"); 
        datePicker.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS); 
        datePicker.show(); 
       } 
       catch (Exception e) 
       { 

       }finally { 
        endDate.clearFocus(); 
       } 
      } 
     } 
    }); 

    populateRegistros(); 
    populateListView(rootView); 
    registroCallBack(rootView); 

    return rootView; 
} 

這是populateRegistros代碼

private void populateRegistros() { 
    mRegistro.add(new Registro("Jose Andres Valencia Acosta","JVALENCIA-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
    mRegistro.add(new Registro("Jose Angel Caballero Preciado","JCABALLERO-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
    mRegistro.add(new Registro("Jose Andres Valencia Acosta","JVALENCIA-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
    mRegistro.add(new Registro("Jose Andres Valencia Acosta","JVALENCIA-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
    mRegistro.add(new Registro("Jose Andres Valencia Acosta","JVALENCIA-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
    mRegistro.add(new Registro("Jose Andres Valencia Acosta","JVALENCIA-PC","Informática","08:50:30","Chrome","Youtube","7:10:25",R.drawable.logo)); 
} 

的populateListView代碼:

private void populateListView(View rootView) { 
    ArrayAdapter<Registro> mAdapter=new MyListAdapter(); 
    ListView list=(ListView)rootView.findViewById(R.id.list_registros); 
    list.setAdapter(mAdapter); 
} 

類MyListAdapter和itemClick的最終方法

private class MyListAdapter extends ArrayAdapter<Registro>{ 
    public MyListAdapter(){ 
     super(getActivity(),R.layout.item_view,mRegistro); 
    } 

    @Override 
    public View getView(int position,View convertView,ViewGroup parent){ 
     //Make sure we have a view to work with (May have been given null) 
     View itemView=convertView; 
     if(itemView==null){ 
      itemView=getActivity().getLayoutInflater().inflate(R.layout.item_view,parent,false); 
     } 

     //Find the register to work with 
     Registro registro=mRegistro.get(position); 

     //General 
     TextView general=(TextView)itemView.findViewById(R.id.txtGENERAL); 
     general.setText(registro.getGeneral()); 

     //Terminal 
     TextView terminal=(TextView)itemView.findViewById(R.id.txtTERMINAL); 
     terminal.setText(registro.getTerminal()); 

     //Area 
     TextView area=(TextView)itemView.findViewById(R.id.txtAREA); 
     area.setText(registro.getArea()); 

     //Uso 
     TextView uso=(TextView)itemView.findViewById(R.id.txtUSO); 
     uso.setText(registro.getUso()); 

     //Proceso 
     TextView proceso=(TextView)itemView.findViewById(R.id.txtPROCESO); 
     proceso.setText(registro.getProceso()); 

     //Actividad 
     TextView actividad=(TextView)itemView.findViewById(R.id.txtACTIVIDAD); 
     actividad.setText(registro.getActividad()); 

     //Tiempo Perdido 
     TextView tiempoPerdido=(TextView)itemView.findViewById(R.id.txtTIEMPOPERDIDO); 
     tiempoPerdido.setText(registro.getTiempoPerdido()); 

     //Imagen 
     ImageView imageView=(ImageView)itemView.findViewById(R.id.imgFOTO); 
     imageView.setImageResource(registro.getIcon()); 

     return itemView; 
    } 
} 

private void registroCallBack(View rootView) { 
    ListView list=(ListView)rootView.findViewById(R.id.list_registros); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Registro clickedRegistro=mRegistro.get(position); 
      String mensaje="Posicion: "+position 
        +" Usuario: "+clickedRegistro.getGeneral(); 
      Toast.makeText(getActivity(),mensaje,Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

我認爲問題是我在onCreateView()中使用的很多代碼,但我不確定。

回答

0

在我們的應用程序中檢查您是否在super.onDrawerClosed(view);之前打開片段。叫做。

如果在此之前打電話,您可能會感覺到抽屜關閉緩慢。

移動你的代碼後super.onDrawerClosed(視圖);在onDrawerClosed方法中。