单选的RecyclerView在Fragment中不起作用。

huangapple 未分类评论57阅读模式
英文:

Single selection recyclerView not working in fragment

问题

我正在使用singleSelection recyclerView,在activity中它正常工作,但当我将其从activity转换为fragment时,它显示了错误,类似于单选的RecyclerView在Fragment中不起作用。

我在片段中使用了这段代码

public class HomeworkFragment extends Fragment implements HomeworkAdapter.SingleClickListener {
    // ...
    adapter = new HomeworkAdapter(view.getContext(), productList);
    adapter.setOnItemClickListener(view.getContext()); // 这里出现错误
    recyclerView.setAdapter(adapter);
    // ...
}

@Override
public void onItemClickListener(int position, View view) {
    adapter.selectedItem();
}

适配器中的代码

private static SingleClickListener sClickListener;

public void setOnItemClickListener(SingleClickListener clickListener) {
    sClickListener = clickListener;
}

public interface SingleClickListener {
    void onItemClickListener(int position, View view);
}

我不知道问题出在哪里,但我尝试了一些方式,如getActivity()view.getContext(),但都没有起作用。当它在activity中时,我是这样做的:adapter.setOnItemClickListener(HomeworkActivity.this);,在activity中它正常工作。

提前感谢!

祝编码愉快!

英文:

I'm using singleSelection recyclerView it was working correct in activity but when i conevert it activity to fragment it's showing me error like 单选的RecyclerView在Fragment中不起作用。

I'm using this code in freagment

public class HomeworkFragment extends Fragment implements HomeworkAdapter.SingleClickListener

.

adapter = new HomeworkAdapter(view.getContext(), productList);
                        adapter.setOnItemClickListener(view.getContext()); //error here
                        recyclerView.setAdapter(adapter); 

.

 @Override
    public void onItemClickListener(int position, View view) {
        adapter.selectedItem();
    }

This code in Adapter

private static SingleClickListener sClickListener;


 public void setOnItemClickListener(SingleClickListener clickListener) {
        sClickListener = clickListener;
    }

    public interface SingleClickListener {
        void onItemClickListener(int position, View view);
    }

I don't know what is the problem but i tries only fewThings like getActivity(), view.getContext() but nothing works

When it was in activity I was doing this adapter.setOnItemClickListener(HomeworkActivity.this); it was working fine in activity

Thanks in advance!

Happy coding!

答案1

得分: 0

你的方法setOnItemClickListener(SingleClickListener clickListener)接受一个参数为SingleClickListener的参数,所以你应该传递this或者HomeFragment.this,而不是context

英文:

your method setOnItemClickListener(SingleClickListener clickListener) takes SingleClickListener as an argument so you should pass this or HomeFragment.this instead of context

huangapple
  • 本文由 发表于 2020年5月29日 22:00:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/62087711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定