How to press an element of the RecyclerView?

0

As the title says, I need help, specifically I need to leave an element of recyclerview and that the symbol appears in toolbar to eliminate that element.

    
asked by Pedro Montenegro 27.11.2017 в 17:25
source

1 answer

0
public class Pantalla_Preferencias extends AppCompatActivity implements Serializable,CineFragment.OnFragmentInteractionListener,
    MusicaFragment.OnFragmentInteractionListener,OtrosFragment.OnFragmentInteractionListener,Lista_PreferenciaFragment.OnFragmentInteractionListener{

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
private SectionsPagerAdapter mSectionsPagerAdapter;
String numero;
/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pantalla_preferencias);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(mViewPager);
    Bundle mibundle;
    mibundle = this.getIntent().getExtras();
    numero = mibundle.getString("numero_telefonito");
    SharedPreferences dato = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = dato.edit();
    editor.putString("numerito",numero);
    editor.apply();
    toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

@Override
public void onFragmentInteraction(Uri uri) {

}


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Fragment newInstance(int sectionNumber) {
        Fragment fragment = null;

        switch (sectionNumber){
            case 1: fragment=new Lista_PreferenciaFragment();
                break;
            case 2:fragment= new CineFragment();
                break;
            case 3:fragment= new MusicaFragment();
                break;
            case 4:fragment= new OtrosFragment();
                break;
        }
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        // TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "COMIDA";
            case 1:
                return "CINE";
            case 2:
                return "MUSICA";
            case 3:
                return "OTROS";
        }
        return null;
    }
}
public void iraAgregarPref(View v){
    Intent i = new Intent(this,Pantalla_Agregar_Preferencia.class);
    Bundle mibundle = new Bundle();
    mibundle.putString("numero_telefonito",numero);
    i.putExtras(mibundle);
    startActivity(i);
}

I have this activity that has a SwipeView with Tabs, each tab shows me 1 different fragment with a recyclerview with separate content separated into 4 categories: "food", "cinematographic", "music" and "others".

public class Lista_PreferenciaFragment extends Fragment {

BDHelper conn;


private OnFragmentInteractionListener mListener;
RecyclerView recyclerViewPref;
ArrayList<Preferencia> lista_pref;
String numero;
Detalle_Preferencia preferencias;
Adaptador_Preferencias adapter;
public Lista_PreferenciaFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    conn = new BDHelper(getContext(),"bd_vinculator",null,1);
    if (getArguments() != null) {

    }
}

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

    View vista = inflater.inflate(R.layout.fragment_lista__preferencia, container, false);
    lista_pref = new ArrayList<>();
    preferencias = new Detalle_Preferencia();
    SharedPreferences dato = PreferenceManager.getDefaultSharedPreferences(getContext());
    numero = dato.getString("numerito","");
    recyclerViewPref = (RecyclerView) vista.findViewById(R.id.recycler_pref);
    adapter = new Adaptador_Preferencias(lista_pref);
    adapter.setOnclickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent i = new Intent(getContext(),Recomendacion.class);
            preferencias.setGusto(lista_pref.get(recyclerViewPref.getChildAdapterPosition(v)).getGusto());
            preferencias.setNum_telef(numero);
            preferencias.setCod_cat("CO");
            Bundle mibundle = new Bundle();
            mibundle.putSerializable("detalle_pref",preferencias);
            i.putExtras(mibundle);
            startActivity(i);
        }
    });
    return vista;
}

private void llenarLista() {
    SQLiteDatabase db = conn.getReadableDatabase();
    Preferencia pref = null;
    lista_pref.clear();
    Cursor cursor = db.rawQuery("SELECT gusto FROM preferencia WHERE num_telef = '"+numero+"' AND cod_cat = 'CO'" ,null);
    while(cursor.moveToNext()){
        pref = new Preferencia();
        pref.setGusto(cursor.getString(0));
       lista_pref.add(pref);
    }
    db.close();
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
public void onResume(){
    super.onResume();
    recyclerViewPref.setLayoutManager(new LinearLayoutManager(getContext()));
    llenarLista();
    recyclerViewPref.setAdapter(adapter);

}
/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

This is the tab code that shows the recycler of the category "food"

public class Adaptador_Preferencias extends RecyclerView.Adapter<Adaptador_Preferencias.PreferenciaViewHolder> implements View.OnClickListener {

ArrayList<Preferencia> list_pref;
private View.OnClickListener listener;

public Adaptador_Preferencias(ArrayList<Preferencia> list_pref) {
    this.list_pref = list_pref;

}

@Override
public PreferenciaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_pref,null,false);
    view.setOnClickListener(this);
    return new PreferenciaViewHolder(view);
}

@Override
public void onBindViewHolder(PreferenciaViewHolder holder, int position) {
    holder.preferencia.setText(list_pref.get(position).getGusto());

}

@Override
public int getItemCount() {
    return list_pref.size();
}
public void setOnclickListener(View.OnClickListener listener){
    this.listener = listener;
}
@Override
public void onClick(View view) {
    if (listener != null){
        listener.onClick(view);
    }
}

public class PreferenciaViewHolder extends RecyclerView.ViewHolder {
    TextView preferencia;

    public PreferenciaViewHolder(View itemView) {
        super(itemView);
        preferencia = (TextView) itemView.findViewById(R.id.txt_pref);


    }



}

Finally here I upload the adapter code of my recyclerview.

    
answered by 03.12.2017 в 01:06