AFragment 向 BFragment 傳值

第一步ide

        在AFragment中定義接口this

//1.定義一個接口 寫一個回調方法
public interface OnItemClickListener {
    void setOnItemClickListener(ShiTi.ResultBean.DataBean dataBean);
}
public OnItemClickListener itemClickListener;
public void setOnItemClickListener(OnItemClickListener listener) {
    this.itemClickListener = listener;
}

第二步對象

        在AFragment中         接口

        裏面有個適配器get

            

adapter.OnLongItemClickListener(new RecyAdapter.OnLongItemClickListener() {
                            @Override
                            public void OnLongItemClickListener(View v, int position) {
//                                Intent intent = new Intent(getActivity(), Main3Activity.class);
//                                intent.putExtra("tv1",data.get(position).getCategory());
//                                intent.putExtra("tv2",data.get(position).getDate());
//                                intent.putExtra("tv3",data.get(position).getTitle());
//                                intent.putExtra("image1",data.get(position).getThumbnail_pic_s02());
//                                startActivity(intent);
                                if (itemClickListener!=null)
                                    itemClickListener.setOnItemClickListener(data.get(position));
                            }
                        });

第三步it

        在MainActivity中  把Fragment先new 成對象io

        

aFragment.setOnItemClickListener(new AFragment.OnItemClickListener() {
    @Override
    public void setOnItemClickListener(ShiTi.ResultBean.DataBean dataBean) {

        bFragment.getData(dataBean);
        vp.setCurrentItem(1);
    }
});

 

第四步class

        在BFragment中List

                寫一個方法方法

public void getData(ShiTi.ResultBean.DataBean dataBean) {
    Log.e("Tag", dataBean.toString());
    title.setText(dataBean.getAuthor_name());
    text.setText(dataBean.getDate());
    content.setText(dataBean.getTitle());

}
相關文章
相關標籤/搜索