保存recyclerview片段的实例

时间:2017-06-13 21:33:00

标签: android android-studio android-fragments android-recyclerview

我在一个活动中有片段。其中一个片段包含一个recyclerview,它将列出添加在其他片段中的项目。当我去其他片段点击导航抽屉项目并回到列表碎片时,项目只是增加了消失。

这是我的列表片段

public class ListaIncidenciasTroncalFragment extends Fragment {

    Context context;   
    Incidencia incidencia = new Incidencia();

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        if (savedInstanceState==null){}
        else {}
    }

    private RecyclerView rvListaIncidencias;
    private ListaIncidenciasTroncalAdapter mlistaIncidenciasTroncalAdapter;

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);    
    }

    @Override
    public void onPause() {
        super.onPause();            
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }    

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);  
    }

    //METODO PARA CAPTURAR EL BOTON BACK EN UN FRAGMENTO
    @Override
    public void onResume() {
        super.onResume();

        if(getView() == null){
            return;
        }

        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){

                    return true;
                }
                return false;
            }
        });
    }

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

        setRetainInstance(true);
        final View x =  inflater.inflate(R.layout.listaincidenciastroncalfragment,null);
        context = x.getContext();


        detector = new GestureDetector(getActivity(), new RecyclerViewOnGestureListener());

        rvListaIncidencias=(RecyclerView)x.findViewById(R.id.rv_listaIncidenciasTroncal);
       // mLayoutManager = new LinearLayoutManager(getActivity());
        rvListaIncidencias.setLayoutManager(new LinearLayoutManager(getContext()));
        //rvListaIncidencias.setLayoutManager(mLayoutManager);
        mlistaIncidenciasTroncalAdapter = new ListaIncidenciasTroncalAdapter();
        rvListaIncidencias.setAdapter(mlistaIncidenciasTroncalAdapter);
        mlistaIncidenciasTroncalAdapter.notifyDataSetChanged();


        Bundle args = getArguments();
            if (args != null) {

                incidencia = args.getParcelable("newIncident");
                mlistaIncidenciasTroncalAdapter.add(incidencia);

            } else {

            }

        return x;
    }

}

这是在添加片段

的createView内部
   addbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) { 

 ListaIncidenciasTroncalFragment fragmentGet = new ListaIncidenciasTroncalFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("NewIncidencia", incidencia);
        fragmentGet.setArguments(bundle);

        //Next lines is necessary to pass the bundle to the fragment but 
        //this means go to the listfragment after addclick (I DONT WANT IT).
        //I want it to show another fragment after add click
        FragmentManager manager= getFragmentManager();
        manager.beginTransaction().replace(R.id.containerView,fragmentGet).commit();

    }

这是适配器

public class ListaIncidenciasTroncalAdapter extends RecyclerView.Adapter<ListaIncidenciasTroncalAdapter.ListaIncidenciasTroncalAdapterViewHolder>
         {

    private List<Incidencia> mLstIncidencia = new ArrayList<>();
    //private OnItemClickListener listener;


    public void add(Incidencia incidencia){

        mLstIncidencia.add(incidencia);
        notifyItemInserted(mLstIncidencia.size()-1);

    }



    @Override
    public ListaIncidenciasTroncalAdapter.ListaIncidenciasTroncalAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listaincidencias_item,parent,false);
        ListaIncidenciasTroncalAdapterViewHolder  mlistaIncidenciasTroncalAdapterViewHolder = new ListaIncidenciasTroncalAdapterViewHolder(view);
        return mlistaIncidenciasTroncalAdapterViewHolder;
    }

    @Override
    public void onBindViewHolder(ListaIncidenciasTroncalAdapter.ListaIncidenciasTroncalAdapterViewHolder holder, final int position) {

        final Incidencia incidencia= mLstIncidencia.get(position);
        holder.tv_FechaRegistro.setText("Registro: "+ incidencia.getFechaRegistro()+" - "+incidencia.getHoraRegistro());
        holder.tv_Falla.setText(incidencia.getTipoFalla());
        holder.tv_Estacion.setText(incidencia.getNomEstacion());
        holder.tv_FechaSolucion.setText("Solucion: "+incidencia.getFechaSolucion()+" - "+incidencia.getHoraSolucion());


    }

    @Override
    public int getItemCount() {
        return mLstIncidencia.size();
    }


    static class ListaIncidenciasTroncalAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        //private OnItemClickListener listener;
        TextView tv_FechaRegistro, tv_Falla, tv_Estacion,tv_FechaSolucion;
        LinearLayout linearLayout;
        public DetalleIncidencia detalleincidencia;

        public ListaIncidenciasTroncalAdapterViewHolder(View itemView) {
            super(itemView);
            tv_FechaRegistro= (TextView) itemView.findViewById(R.id.tvRegistro);
            tv_Falla= (TextView) itemView.findViewById(R.id.tvFalla);
            tv_Estacion= (TextView) itemView.findViewById(R.id.tvEstacion);
            tv_FechaSolucion= (TextView) itemView.findViewById(R.id.tvSolucion);

            final Context context = itemView.getContext();
            //detalleincidencia=(DetalleIncidencia) itemView.findViewById(R.id.)

            linearLayout=(LinearLayout) itemView.findViewById(R.id.linearItem);

        }

        @Override
        public void onClick(View v) {
            int pos = getAdapterPosition();

            }

        }}

这是包含片段的主要活动

public class MainActivity extends ActionBarActivity implements IngresoIncidenciaTroncalFragment.OnButtonClickedListener{


    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    TextView tvNombre, tvEmail;
    Bundle mContent;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

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

        if (savedInstanceState != null) {

        }

        //SETEO EL DRAWERLAYOUT Y EL NAVIGATIONVIEW
        final Toolbar mitoolbar = (Toolbar) findViewById(R.id.toolbar);
             mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
             mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
        setSupportActionBar(mitoolbar);

        tvNombre=(TextView) findViewById(R.id.txtNombreUsuario);

            //ACA INFLO EL PRIMER FRAGMENTO QUE SERA EL FONDO DEL NAVIGATION DRAWER CUANDO SE ABRA LA APP
             mFragmentManager = getSupportFragmentManager();
             mFragmentTransaction = mFragmentManager.beginTransaction();
             mFragmentTransaction.replace(R.id.containerView,new FondoInicioFragment()).commit();


            //EVENTO CLICK DE CADA ITEM DEL NAVIGATION VIEW
             mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
             @Override
             public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();

                 if (menuItem.getItemId() == R.id.nav_item_ingresoIncidencia) {
                     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                     fragmentTransaction.replace(R.id.containerView,new IngresoIncidenciaTroncalFragment()).commit();
                     mitoolbar.setTitle("Ingreso Incidencia Troncal");
                 }

                else if (menuItem.getItemId() == R.id.nav_item_listarIncidencia) {
                     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                     fragmentTransaction.replace(R.id.containerView,new ListaIncidenciasTroncalFragment()).commit();
                     mitoolbar.setTitle("Lista Incidencias Troncal");

                 }

                 return false;
            }

        });


                 //SETUP DRAWER TOGGLE OF THE TOOLBAR
                android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
                ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.app_name);
                mDrawerLayout.setDrawerListener(mDrawerToggle);
                mDrawerToggle.syncState();

    }



    //METODO IMPLEMENTADO DE INGRESOINCIDENCIATRONCALFRAGMENT PARA PONERLE UN KEY A LA INCIDENCIA AGREGADA Y ASI
    // SE LA PUEDA MANDAR A LA LISTA DE INCIDENCIAS

    public void onButtonClicked(Incidencia incidencia){

        ListaIncidenciasTroncalFragment fragment = new ListaIncidenciasTroncalFragment();
        Bundle args = new Bundle();
        args.putParcelable("newIncident", incidencia);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction().replace(R.id.containerView, fragment).commit();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }



}

0 个答案:

没有答案