40

fragment跳转另一个fragment的问题!

项目地址:
链接: https://pan.baidu.com/s/1OHSva0M5PIQMWNP0iwzlLg 提取码: csf5

求大能帮我解决一下。我不知道怎么去写这个跳转。

图片说明

鼠标点击需要跳转(例如点击“苹果手机”);

public class MsgFragment extends Fragment {

    View view;
    TextView textView;
    private int i;
    private String str;


    private String[] mVals = new String[] { "苹果手机", "笔记本电脑", "电饭煲", "腊肉",
            "特产", "剃须刀", "宝宝", "康佳" };
    private LayoutInflater mInflater;
    private FlowLayout mFlowLayout;

    //自定义recyclerveiw的适配器



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInflater = LayoutInflater.from(getContext());


    }

    public MsgFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       view = inflater.inflate(R.layout.fragment_msg, container, false);

        mFlowLayout = view.findViewById(R.id.id_flowlayout);


        initData();


        return view;
    }
    public void initData() {
        /**
         * 找到搜索标签的控件
         */
        for (  i = 0; i < mVals.length; i++) {
            final TextView tv = (TextView) mInflater.inflate(
                    R.layout.search_label_tv, mFlowLayout, false);
            tv.setText(mVals[i]);
            str = tv.getText().toString();
            //点击事件
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {

                        switch (str) {
                            case "苹果手机":


                                break;
                            case "笔记本电脑":

                                break;
                            case "电饭煲":

                                break;
                            case "腊肉":

                                break;
                            case "特产":

                                break;
                            case "剃须刀":

                                break;
                            case "宝宝":

                                break;
                            case "康佳":

                                break;
                            default:

                                break;
                        }





                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
            mFlowLayout.addView(tv);//添加到父View
        }


    }


}

需要跳转到的fragment

public class CollectFragment extends Fragment {
    private View view;//定义view用来设置fragment的layout
    public RecyclerView mCollectRecyclerView;//定义RecyclerView
    //定义以goodsentity实体类为对象的数据集合
    private ArrayList<GoodsEntity> goodsEntityList = new ArrayList<GoodsEntity>();
    //自定义recyclerveiw的适配器
    private CollectRecycleAdapter mCollectRecyclerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取fragment的layout
        view = inflater.inflate(R.layout.item_content, container, false);
        //对recycleview进行配置
        initRecyclerView();
        //模拟数据
        initData();
        return view;
    }

    /**
     * TODO 模拟数据
     */
    private void initData() {
        for (int i=0;i<10;i++){
            GoodsEntity goodsEntity=new GoodsEntity();
            goodsEntity.setGoodsName("模拟数据"+i);
            goodsEntity.setGoodsPrice("100"+i);
            goodsEntityList.add(goodsEntity);
        }
    }

    /**
     * TODO 对recycleview进行配置
     */

    private void initRecyclerView() {
        //获取RecyclerView
        mCollectRecyclerView=view.findViewById(R.id.collect_recyclerView);
        //创建adapter
        mCollectRecyclerAdapter = new CollectRecycleAdapter(getActivity(), goodsEntityList);
        //给RecyclerView设置adapter
        mCollectRecyclerView.setAdapter(mCollectRecyclerAdapter);
        //设置layoutManager,可以设置显示效果,是线性布局、grid布局,还是瀑布流布局
        //参数是:上下文、列表方向(横向还是纵向)、是否倒叙
        mCollectRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        //设置item的分割线
        mCollectRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件,需要自己在适配器中写一个监听事件的接口。参数根据自定义
        mCollectRecyclerAdapter.setOnItemClickListener(new CollectRecycleAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, GoodsEntity data) {
                //此处进行监听事件的业务处理
                Toast.makeText(getActivity(),"我是item",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

查看全部
qq_34424362
qq_34424362
2019/03/12 03:11
  • android-studio
  • xml
  • android
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复