当前位置: 首页 > 知识库问答 >
问题:

如何实现从RecolyerView到fragment的意图

齐宗清
2023-03-14

SeriesListener

public interface SeriesListener {void seriesKnowledge(String title,String desc,int thumb);}

序列rvadapter

public class SeriesRVAdapter extends RecyclerView.Adapter<SeriesRVAdapter.MyViewHolder> {

private Context mContext;
private List<SeriesDB> mData;
private SeriesListener mSeriesListener;

public SeriesRVAdapter(Context mContext, List<SeriesDB> mData) {
    this.mContext = mContext;
    this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.cardview_item_categories,parent,false);
    MyViewHolder holder = new MyViewHolder(view,mSeriesListener);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

    holder.seriesTitle.setText(mData.get(position).getTitle());
    holder.seriesImage.setImageResource(mData.get(position).getThumbnail());
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mSeriesListener.seriesKnowledge(mData.get(position).getTitle(),mData.get(position).getDescription(),mData.get(position).getThumbnail());
        }
    });
}

@Override
public int getItemCount() {
    return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{

    TextView seriesTitle;
    ImageView seriesImage;
    CardView cardView;
    SeriesListener mSeriesListener;

    public MyViewHolder(View itemView, SeriesListener seriesListener){
        super(itemView);

        seriesTitle = itemView.findViewById(R.id.series_title_id);
        seriesImage = itemView.findViewById(R.id.series_img_id);
        cardView =  itemView.findViewById(R.id.cardview_series_id);
        mSeriesListener = seriesListener;
    }

}
}

描述活动性

public class DescriptionActivity extends AppCompatActivity implements  SeriesListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_description);
}


@Override
public void seriesKnowledge(String title, String desc, int thumb) {

    FragmentManager manager = getFragmentManager();

    DecriptionFragment decriptionFragment = (DecriptionFragment) manager.findFragmentById(R.id.description_fr);
    decriptionFragment.sendData(title,desc,thumb);

}
}
public class DecriptionFragment extends Fragment {

View view;
ImageView img;
TextView tx1;
TextView tx2;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_description,container,false);

    img = view.findViewById(R.id.description_image_id);
    tx1 = view.findViewById(R.id.description_series_id);
    tx2 = view.findViewById(R.id.description_series_category);

    return view;
}

public void sendData(String title, String desc, int thumb)
{
    img.setImageResource(thumb);
    tx1.setText(title);
    tx2.setText(desc);
}
}
View view;
List<SeriesDB> seriesDBS;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_categories, container, false);

    seriesDBS = new ArrayList<>();
    seriesDBS.add(new SeriesDB("Arrow", "Crime","Decription Arrow",R.drawable.arrow));
    seriesDBS.add(new SeriesDB("Flash", "Crime","Decription Flash",R.drawable.flash));
    seriesDBS.add(new SeriesDB("Supernatural", "Crime","Decription Supernatural",R.drawable.supernatural));
    seriesDBS.add(new SeriesDB("The Originals", "Crime","Decription Originals",R.drawable.originals));
    seriesDBS.add(new SeriesDB("The Legacies", "Crime","Decription Legacies",R.drawable.legacies));
    seriesDBS.add(new SeriesDB("Peaky Blinders", "Crime","Decription Peaky Blinders",R.drawable.peaky));
    seriesDBS.add(new SeriesDB("Arrow", "Crime","Decription Arrow",R.drawable.arrow));
    seriesDBS.add(new SeriesDB("Flash", "Crime","Decription Flash",R.drawable.flash));
    seriesDBS.add(new SeriesDB("Supernatural", "Crime","Decription Supernatural",R.drawable.supernatural));
    seriesDBS.add(new SeriesDB("The Originals", "Crime","Decription Originals",R.drawable.originals));
    seriesDBS.add(new SeriesDB("The Legacies", "Crime","Decription Legacies",R.drawable.legacies));
    seriesDBS.add(new SeriesDB("Peaky Blinders", "Crime","Decription Peaky Blinders",R.drawable.peaky));

    RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerView_series_id);
    SeriesRVAdapter myAdapter = new SeriesRVAdapter(getActivity(),seriesDBS);
    myrv.setLayoutManager(new GridLayoutManager(getContext(),3));
    myrv.setAdapter(myAdapter);
    return view;
}
--------- beginning of crash

2018-12-02 17:44:33.663 6704-6704/com.example.forev.neizledim E/AndroidRuntime:致命异常:主进程:com.example.forev.neizledim,pid:6704 java.lang.nullpointerexception:尝试在com.example.forev.neizledim.serieslistener.seriesknowledge(java.lang.string,java.lang.string,int)上调用com.example.forev.neizledim.adapter.recyclerview.seriesrvadapter$1上的空

共有1个答案

束俊英
2023-03-14

由于mserieslistener未初始化,适配器内的onClick方法处出现nullpointer异常,因此将其添加到seriesrvadapter:

public void setSeriesListener(SeriesListener mSeriesListener) {
    this.mSeriesListener = mSeriesListener;
}

categoriesfragment:

SeriesRVAdapter myAdapter = new SeriesRVAdapter(getActivity(),seriesDBS);
myAdapter.setSeriesListener(this);

并在CategoriesFragment处实现SeriesKnowledge方法:

public void seriesKnowledge(String title,String desc,int thumb){
}
 类似资料:
  • 本文向大家介绍如何实现Fragment的滑动?相关面试题,主要包含被问及如何实现Fragment的滑动?时的应答技巧和注意事项,需要的朋友参考一下 将Fragment与viewpager绑定,通过viewpager中的touch事件,会进行move事件的滑动处理。 Fragment布局   viewpager布局:   MainActivity代码:   在这段代码中,我们 首先fragment以

  • 我的要求: 发现错误: 在这一行中..为什么值不显示在mainfragment中??

  • 我正在使用显示项目的名称。我的行包含单个。项名存储在中。 若要更改的内容,我将替换中的字符串,并在上调用()。 但是,如果我试图在RecyclerView滚动时更改的内容,有时它会给我 如果的大小比以前小,就会发生这种情况。那么,更改内容的正确方法是什么?这是中的bug吗? 以下是异常的完整堆栈跟踪:

  • 我有一个受支持的片段活动,它将加载差异片段。片段有一些,带有我想得到它的句柄,但score的

  • 目前情况: 只有一个活动称为mainActivity。 MainActivity维护navhostFragment。 内部有一个WebViewFragment盗用了WebView。 navhostfragment控制许多其他片段。 参见NavigationBasicSample@GitHub 参见导航入门@Android开发者之家

  • 主要内容:本节引言:,1.逻辑流程讲解:,2.代码示例:简单新闻类APP列表和内容切换的实现,3.代码下载,本节小结:本节引言: 相信大家对点击列表,然后进入详情这种App并不陌生吧,在购物类App和新闻类App中最为常见: 下面我们简单来讲一下流程逻辑! 1.逻辑流程讲解: 刚好公司测试妹子的测试机上装了楚楚街9块9的APP,呵呵,直接就照这个来研究吧: 嘿嘿,市面上很多APP都是这种样子的,而这个可以用我们学到的Fragment来实现: 可能gif动画看不清,笔者用界面原型工具画个大概吧: