qq_34424362
qq_34424362
采纳率50%
2019-03-12 03:11 阅读 1.0k

fragment跳转另一个fragment的问题!

40

项目地址:
链接: 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();
            }
        });
    }

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    vinesbaby vinesbaby 2019-03-13 08:37

    问题已解决,这只是一个思路,对照代码自己看一下,
    链接:https://pan.baidu.com/s/1mu_GOwwcvzVI1u1l1mK55Q
    提取码:ce12

    点赞 1 评论 复制链接分享
  • menwaiqingshan SemiraChen 2019-03-12 11:09

    MsgFragment中把 switch (str) 改成 switch (((TextView)v).getText().toString())
    CollectFragment中把 view = inflater.inflate(R.layout.item_content, container, false); 改成view = inflater.inflate(R.layout.recyclerview, container, false);_

    ——————分割线——————

    新建一个ContainerFragment作为第一页的容器

    package com.example.qw.dualnavigation.fragment;
    
    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.example.qw.dualnavigation.R;
    
    /**
     * A simple {@link Fragment} subclass.
     * Activities that contain this fragment must implement the
     * {@link ContainerFragment.OnFragmentInteractionListener} interface
     * to handle interaction events.
     * Use the {@link ContainerFragment#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class ContainerFragment extends Fragment {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_container, container, false);
    
            //初始化fragment
            MsgFragment msgFragment = new MsgFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fly_fragment, msgFragment).show(msgFragment).commit();
    
            return view;
    
        }
    
    }
    

    fragment_container.xml

        <?xml version="1.0" encoding="utf-8"?>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                xmlns:tools="http://schemas.android.com/tools"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                tools:context=".fragment.ContainerFragment">
    
                <FrameLayout
                        android:id="@+id/fly_fragment"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                </FrameLayout>
    
        </FrameLayout>
    
    

    然后 MsgFragment中

    @SuppressLint("ResourceType")
        private void changeToAnotherFragment() {
            CollectFragment collectFragment = new CollectFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fly_fragment, collectFragment).show(collectFragment).commit();
        }
    

    MainFragmentAdapter中

    fragment = new CollectFragment();
    改成
    fragment = new ContainerFragment();

    大概是这样,不知道最后是做成什么效果的,目前数据是显示出来的,但是应该还有有改的空间

    点赞 1 评论 复制链接分享

相关推荐