Ich habe einen Bildschirm mit einem EditText, um Inhalte und einen RecyclerView zu finden.Daten vom RecyclerView Adapter an das übergeordnete Fragment übergeben
Die EditText in meinem Web-Server verwendet
Die RecyclerView zeigt beliebtesten Tags
So zu finden ist, was ich tun möchte, ist, wenn der Benutzer auf eine der Elemente der RecyclerView Liste Dieser ausgewählte Wert wird an editText übergeben.
Ich habe ein Fragment, das die Datei aufzublasen, das alle Sachen zeigen:
tags_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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="org............BuscarFragment">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="@string/busqueda"
android:inputType="textVisiblePassword"
android:lines="1"
android:singleLine="true" />
<TextView
android:id="@+id/buscar_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputSearch" />
<TextView
android:id="@+id/etiquetas_populares"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buscar_msg"
android:layout_marginLeft="16dp"
android:text="@string/etiquetas_populares"/>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tags_list"
android:name="org............TagsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/etiquetas_populares"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="org...........activities.TagsActivity"
tools:listitem="@layout/tags_list_content" />
</RelativeLayout>
</FrameLayout>
In TagsFragment.java Ich habe diese Methode:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tags_list, container, false);
//RecyclerView Section
mRecyclerView = (RecyclerView) view.findViewById(R.id.tags_list);
mBuscarTagsAdapter = new BuscarTagsAdapter(getActivity(), tagMoreUsedRecyclerViewList);
mRecyclerView.setAdapter(mBuscarTagsAdapter);
addKeyListener();
return view;
}
Endlich in meinem Adapter.
BuscarTagsAdapter.java
@Override
public void onBindViewHolder(final BuscarTagsViewHolder holder, int position) {
final int posicion = position;
//onClick for the list
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When the user click I want to pass this value to my EditText, but
//I don't see how to do it from this Adapter
Toast.makeText(context, tags.get(posicion).getTagName(), Toast.LENGTH_SHORT).show();
}
});
}
Jede Idee? Dank
AKTUALISIERT und GELÖST:
Hallo, @ Brian Nam, vielen Dank für Hilfe.
Einige Änderungen vorgenommen. Zuerst im Constructor, wie Sie vorgeschlagen:
public BuscarTagsAdapter(Context context, List<TagMoreUsedRecyclerView> items, TagsListInterface tagsListInterface) {
this.context = context;
this.tags = items;
this.tagsListInterface = tagsListInterface;
}
aber tagsListInterface nicht richtig in TagsFragment initialisiert, so habe ich dies in onCreate Methode:
tagsListInterface = new TagsListInterface() {
@Override
public void onTagClicked(String tagName, int posicion) {
EditText editText = (EditText) view.findViewById(R.id.inputSearch);
editText.setText(tagName);
}
};
schließlich in onClick für die RecyclerView:
BuscarTagsAdapter.java
@Override
public void onBindViewHolder(final BuscarTagsViewHolder holder, int position) {
final int posicion = position;
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, tags.get(posicion).getTagName(), Toast.LENGTH_SHORT).show();
tagsListInterface.onTagClicked(tags.get(posicion).getTagName(), posicion);
}
});
}
Sie können Fragment zu Adapter und in Adapter übergeben Sie die Methode mit einem Wert aufrufen können, die Sie von Fragment übergeben wollen. –