导航抽屉慢开

时间:2014-11-03 03:01:57

标签: android navigation-drawer

我正在使用导航抽屉的应用程序中工作。

开始时,抽屉打开后,工作快速而顺畅。但是我在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()中使用的很多代码,但我不确定。

1 个答案:

答案 0 :(得分:0)

在我们的应用程序中检查您是否在super.onDrawerClosed(view)之前打开片段;被称为。

如果在此之前打电话,您可能会觉得抽屉的关闭速度很慢。

在super.onDrawerClosed(view)之后移动你的代码;在onDrawerClosed方法中。