将数据从RecyclerView Adapter传递到Parent Fragment

时间:2016-08-28 17:18:13

标签: android android-fragments android-recyclerview android-edittext android-adapter

我有一个带有EditText的屏幕来查找内容和RecyclerView。

EditText将在我的网络服务器中找到

RecyclerView显示最常用的标签

所以,我想要做的是,当用户点击RecyclerView列表的任何元素时 选择的值将传递给editText。

我有一个片段,它会显示文件,显示所有内容:

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>

TagsFragment.java 中,我有这个方法:

 @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;
    }

最后在我的适配器中。

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();

            }
        });
    }

有什么想法吗? 感谢

更新并已解决:

嗨,@ Brian Nam,谢谢你的帮助。

做了一些改变。 首先在构造函数中建议:

    public BuscarTagsAdapter(Context context, List<TagMoreUsedRecyclerView> items, TagsListInterface tagsListInterface) {

            this.context = context;
            this.tags = items;
            this.tagsListInterface = tagsListInterface;

}

但在TagsFragment中没有正确初始化tagsListInterface, 所以我在onCreate方法中添加了这个:

 tagsListInterface = new TagsListInterface() {
            @Override
            public void onTagClicked(String tagName, int posicion) {
                EditText editText = (EditText) view.findViewById(R.id.inputSearch);
                editText.setText(tagName);
            }
        };

最后在onClick上查看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);


            }
        });
    }

2 个答案:

答案 0 :(得分:2)

不幸的是,没有&#34;好&#34;这样做的方式。我说的不是接口,你应该查看一个事件总线实现,比如Greenrobot的EventBus。

https://github.com/greenrobot/EventBus

使用像EventBus(基本上是发布者 - 订阅者模式)这样的东西的好处是你节省了很多匿名类,接口,但你也使代码更少耦合。更不用说任何课程都可以订阅这些活动,所以你基本上可以说&#34;用户这样做了#34;并且多个片段可以对它做出反应。如果您执行复杂的UI /动画,这将非常有用。

关于EventBus的坏处是你开始有很多事件代码质量受损,有时很难知道发生了什么以及在哪里。

但正如我所说,那里没有&#34;好&#34;这样做的方式,就是android框架的工作原理。

答案 1 :(得分:1)

正如@Brian Nam所写,您可以将对TagsFragment个实例的引用传递给BuscarTagsAdapter

类似但IMO多一点&#39;清洁&#39;解决方案是创建一个接口,例如:

public interface TagsListInterface {
    void onTagClicked(String tagName);
}

然后您的TagsFragment将实现该接口并将其实例传递给BuscarTagsAdapter。然后,当单击Recycler View的元素时,您将调用该接口的onTagClicked方法。

另一个解决方案是通过您的活动将RecyclerView与您的片段进行通信,我相信,这是最像&#39;类似Android的&#39;一。它还可能涉及创建一个自定义界面,但它是实现它而不是片段的Activity。您可以阅读更多相关信息here。这篇文章解释了如何在片段之间进行通信,但我相信它可以向您展示一般的想法,以便您可以将它应用到您的情况中。

<强>更新

在您呼叫的onCreateView方法中:

mBuscarTagsAdapter = new BuscarTagsAdapter(getActivity(), tagMoreUsedRecyclerViewList);

此处唯一需要更改的是更新BuscarTagsAdapter的构造函数以接收TagsListInterface的实例,然后从TagsFragment传递您的实现。然后,您可以在单击元素时使用它来调用onTagClicked

相关问题