简体   繁体   English

如何与我的适配器类中的片段进行通信

[英]How to communicate with fragment from my adapter class

I have created a custom adapter class.我创建了一个自定义适配器类。 In that class, I have a code that will have to send a message to my fragment, when I clicked on my listview layout.在那个类中,当我单击我的列表视图布局时,我有一个代码必须向我的片段发送消息。 After googling, the best approach to do it may be using interface.谷歌搜索后,最好的方法可能是使用界面。 Most of them are the example of communicating between activity with the fragment.它们中的大多数是在活动与片段之间进行通信的示例。 But in my case, I don't have any ideas about how to communicate between my adapter class with my fragment class.但就我而言,我对如何在适配器类与片段类之间进行通信没有任何想法。 let says I create an interface in my adapter class like:假设我在我的适配器类中创建了一个接口,例如:

public interface SuccessResponse{
    void onSuccess();
}

and on the LinearLayout inside my adapter class I want it to be something like:在我的适配器类中的 LinearLayout 上,我希望它是这样的:

linearLayout.setOnClickListener(new View.OnClickListener{
    @Override
    public void onClick (View view){
        SuccessResponse.onSuccess();
    }
})

Then I want to make sure my fragment page get the onSuccess() method and do something like :然后我想确保我的片段页面获得 onSuccess() 方法并执行以下操作:

public class MyFragment extends ListFragment implements Adapter.SuccessResponse{
    @Override
    public void onSuccess(){
        //do Something
    }
}

Is there any way to do something like above?有没有办法做类似上面的事情?

Update If you are looking for a solution in Kotlin更新如果您正在寻找 Kotlin 中的解决方案

class ExampleFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val adapter = MyAdapter(){
            Log.d("zzzz", "$it ")
        }
    }
}


class MyAdapter (val onClick : (value: String) -> Unit): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.itemView.setOnClickListener {
            onClick("something")
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        TODO("Not yet implemented")
    }
}

JAVA The following code may help you. JAVA下面的代码可能会对你有所帮助。

    public class ExampleFragment extends Fragment implements MyAdapter.SuccessResponse{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View contentView  = inflater.inflate(R.layout.my_layout, container, false);
            MyAdapter myAdapter = new MyAdapter(getActivity(), 0);
            myAdapter.successResponse = this;
            return contentView;
        }
    
        @Override
        public void onSuccess() {
    
        }
    }

    
    class MyAdapter extends ArrayAdapter{
        SuccessResponse successResponse;
    
        public MyAdapter(Context context, int resource) {
            super(context, resource);
        }
    
        public interface SuccessResponse{
            void onSuccess();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //ur views
            linearLayout.setOnClickListener(new View.OnClickListener{
                @Override
                public void onClick (View view){
                    if(successResponse!=null)
                        successResponse.onSuccess();
                }
            })
        }
    }

Do it in your CustomAdapter在您的 CustomAdapter 中执行此操作

1) Import Fragment 1) 导入片段

import android.support.v4.app.Fragment;   

2) Declare frag object in your adapter 2) 在你的适配器中声明片段对象

     Fragment frag

        public YourAdapter(Fragment frag) {
            this.frag = frag;
        }

3) Now call your fragment method 3)现在调用你的片段方法

        linearLayout.setOnClickListener(new View.OnClickListener{
            @Override
            public void onClick (View view){
                ((HomeFragment) frag).onSuccess();// here your fragment name HomeFragment
            }
        })

4) In HomeFragment 4) 在 HomeFragment 中

    public void onSuccess(){
         // do your stuff here
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从适配器类调用片段? - How to call fragment from adapter class? 如何将数据库中的值从Adapter类插入到片段对话框类? - How to insert values in database from Adapter class to fragment dialog class? 如何从片段类到适配器类访问方法 - How to access a method from fragment class to adapter class 如何从片段引用适配器类中的子视图 - How to reference child view in adapter class from fragment 如何从适配器调用片段 - How to call fragment from adapter 从Android中的适配器访问Fragment内的类 - Accessing Class inside Fragment from Adapter in Android 我的代码中有错误,用于将 aduio url(从 Firebase 检索)从适配器类传递到片段 - I have error in my code, which is for passing aduio url (Retrieved from Firebase) from a adapter class to a fragment 如何在适配器中获取片段 class 名称 - How to get Fragment class name in adapter 即使我在我的适配器 class 中初始化它,我是否可以从不同的 Activity/Fragment 暂停/停止 ExoPlayer? - Can I pause / stop ExoPlayer from a different Activity / Fragment even though I initialize it in my Adapter class? 如何从MVVM体系结构中的Web服务的存储库类与活动/片段进行通信 - how to communicate to activity/fragment from repository class for web service in MVVM architecture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM