Created
August 22, 2017 21:28
-
-
Save luisrenemas/b3cf18f78d7f3eb4b7c7798edca72019 to your computer and use it in GitHub Desktop.
Ayuda - ¿Como actualizar items de RecyclerView cuando se elimina un registro, actualiza o agrega?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/bt_eliminar" | |
android:text="Eliminar" | |
android:layout_width="match_parent" | |
android:layout_height="40dp"/> | |
<LinearLayout | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginTop="50dp"> | |
<FrameLayout | |
android:id="@+id/content" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1"/> | |
<FrameLayout | |
android:id="@+id/detail" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1"/> | |
</LinearLayout> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.a01luisrene.comunicacionfragments; | |
public class Datos { | |
private String mTitle; | |
private String mSubTitle; | |
Datos(String title, String subTitle){ | |
mTitle = title; | |
mSubTitle = subTitle; | |
} | |
public String getmTitle() { | |
return mTitle; | |
} | |
public void setmTitle(String mTitle) { | |
this.mTitle = mTitle; | |
} | |
public String getmSubTitle() { | |
return mSubTitle; | |
} | |
public void setmSubTitle(String mSubTitle) { | |
this.mSubTitle = mSubTitle; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="?attr/selectableItemBackground" | |
android:layout_marginBottom="8dp" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/vertical_list_item_title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:textSize="20sp" /> | |
<TextView | |
android:id="@+id/vertical_list_item_subtitle" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.a01luisrene.comunicacionfragments.FragmentA"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/fragment_recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.a01luisrene.comunicacionfragments.FragmentB"> | |
<Button | |
android:id="@+id/bt_agregar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Agregar"/> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.a01luisrene.comunicacionfragments; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.ArrayList; | |
public class FragmentA extends Fragment implements RecyclerViewAdapter.MyClickListener { | |
private RecyclerView mRecyclerView; | |
private RecyclerViewAdapter mAdapter; | |
public FragmentA() { | |
// Required empty public constructor | |
} | |
public static FragmentA newInstance() { | |
return new FragmentA(); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
} | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
// Inflate the layout for this fragment | |
View view = inflater.inflate(R.layout.fragment_a, container, false); | |
mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_recycler_view); | |
mRecyclerView.setHasFixedSize(true); | |
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); | |
mAdapter = new RecyclerViewAdapter(getDataSet()); | |
mRecyclerView.setAdapter(mAdapter); | |
mAdapter.setOnItemClickListener(this); | |
return view; | |
} | |
private ArrayList<Datos> getDataSet() { | |
ArrayList results = new ArrayList<>(); | |
for (int index = 0; index < 5; index++) { | |
Datos obj = new Datos("Some Primary Text " + index, | |
"Secondary " + index); | |
results.add(index, obj); | |
} | |
return results; | |
} | |
@Override | |
public void onItemClick(final int position, View v) { | |
new AlertDialog.Builder(getActivity()) | |
.setTitle("Eliminar item") | |
.setMessage("¿stas seguro de eliminar item?") | |
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// continue with delete | |
mAdapter.deleteItem(position); | |
Log.v("Posicion", String.valueOf(position)); | |
} | |
}) | |
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// do nothing | |
} | |
}) | |
.setIcon(android.R.drawable.ic_dialog_alert) | |
.show(); | |
} | |
public void addItem() { | |
new AlertDialog.Builder(getActivity()) | |
.setTitle("Agregar Item") | |
.setMessage("¿Estas seguro de agregar el nuevo item?") | |
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// continue with delete | |
actionAdd(); | |
} | |
}) | |
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// do nothing | |
} | |
}) | |
.setIcon(android.R.drawable.ic_dialog_alert) | |
.show(); | |
} | |
public void eliminar(final int position){ | |
mAdapter.deleteItem(position); | |
Log.v("Posicion", String.valueOf(position)); | |
} | |
public void actionAdd() { | |
Datos object = new Datos("Some Primary Text " + mAdapter.getItemCount(), | |
"Secondary " + mAdapter.getItemCount()); | |
mAdapter.addItem(object, mAdapter.getItemCount()); | |
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.a01luisrene.comunicacionfragments; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
public class FragmentB extends Fragment implements View.OnClickListener { | |
Button btAgregar; | |
FragmentA mFragment; | |
// Interfaz Actualizar | |
public interface Actualizar{ | |
// Método de la interfaz | |
public void actualizarItem(); | |
} | |
// Objeto de la interfaz actualizar, con este objeto llamaremos el | |
// método de la interfaz | |
Actualizar actualizar; | |
public FragmentB() { | |
// Required empty public constructor | |
} | |
// Instancia | |
public static FragmentB newInstance(){ | |
return new FragmentB(); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
} | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View v = inflater.inflate(R.layout.fragment_b, container, false); | |
btAgregar = (Button) v.findViewById(R.id.bt_agregar); | |
btAgregar.setOnClickListener(this); | |
return v; | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()){ | |
case R.id.bt_agregar: | |
// Se llama el método de la interfaz Actualizar. Al implementar la | |
// interfaz Actualizar en la actividad, este método se sobrescribirá | |
// y al presionar el botón guardar de este fragment este método se | |
// ejecutara en la actividad. | |
actualizar.actualizarItem(); | |
break; | |
} | |
} | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
// Aquí nos aseguramos de que en la actividad se haya implementado la interfaz, | |
// si el programador no la implementado se lanza el mensaje de error. | |
try { | |
actualizar = (Actualizar) context; | |
} catch (ClassCastException e) { | |
throw new ClassCastException(context.toString() + " Debe implementar la interfaz Actualizar en su Activity"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.a01luisrene.comunicacionfragments; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter | |
.DataObjectHolder> { | |
private ArrayList<Datos> mDataset; | |
private static RecyclerViewAdapter.MyClickListener sClickListener; | |
static class DataObjectHolder extends RecyclerView.ViewHolder | |
implements View | |
.OnClickListener { | |
TextView mLabel; | |
TextView mDateTime; | |
DataObjectHolder(View itemView) { | |
super(itemView); | |
mLabel = (TextView) itemView.findViewById(R.id.vertical_list_item_title); | |
mDateTime = (TextView) itemView.findViewById(R.id.vertical_list_item_subtitle); | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
sClickListener.onItemClick(getAdapterPosition(), v); | |
} | |
} | |
void setOnItemClickListener(RecyclerViewAdapter.MyClickListener myClickListener) { | |
this.sClickListener = myClickListener; | |
} | |
RecyclerViewAdapter(ArrayList<Datos> myDataset) { | |
mDataset = myDataset; | |
} | |
@Override | |
public RecyclerViewAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, | |
int viewType) { | |
View view = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.datos_item, parent, false); | |
DataObjectHolder dataObjectHolder = new DataObjectHolder(view); | |
return dataObjectHolder; | |
} | |
@Override | |
public void onBindViewHolder(RecyclerViewAdapter.DataObjectHolder holder, int position) { | |
holder.mLabel.setText(mDataset.get(position).getmTitle()); | |
holder.mDateTime.setText(mDataset.get(position).getmSubTitle()); | |
} | |
void addItem(Datos dataObj, int index) { | |
mDataset.add(dataObj); | |
notifyItemInserted(index); | |
} | |
void deleteItem(int index) { | |
mDataset.remove(index); | |
notifyItemRemoved(index); | |
} | |
@Override | |
public int getItemCount() { | |
return mDataset.size(); | |
} | |
interface MyClickListener { | |
void onItemClick(int position, View v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola Luis