- 分类主要布局的部分的代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#F3F5F7" tools:context="com.example.com.jingdong.view.MainActivity"> <include layout="@layout/title_layout" /> <LinearLayout android:layout_marginTop="2dp" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <ListView android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" android:id="@+id/fr02_lv" android:background="@drawable/edtext_shape" > </ListView> <FrameLayout android:layout_width="0dp" android:layout_weight="8" android:layout_height="match_parent" android:id="@+id/fr02_layout"> </FrameLayout> </LinearLayout> </LinearLayout>
//加载上面布局的activity的类代码
package com.example.com.jingdong.fragment; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.FrameLayout; import android.widget.ListView; import com.example.com.jingdong.R; import com.example.com.jingdong.adapter.MyAdapter; import com.example.com.jingdong.mobile.pojo.FenLeiBean; import com.example.com.jingdong.mobile.pojo.ProductBean; import com.example.com.jingdong.mobile.pojo.UserBean; import com.example.com.jingdong.presenter.MyDataPresenter; import com.example.com.jingdong.view.DataView; import java.util.List; /** * Created by Administrator on 2018/3/17. */ public class Fragment02 extends Fragment implements DataView{ private ListView listView; private String url = "https://www.zhaoapi.cn/product/getCatagory"; private List<ProductBean.DataBean> list; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); list = (List<ProductBean.DataBean>) msg.obj; listView.setAdapter(new MyAdapter(getActivity(),null, list)); } }; private FrameLayout fr02_layout; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment02,container,false); //获取控件 listView = view.findViewById(R.id.fr02_lv); fr02_layout = view.findViewById(R.id.fr02_layout); MyDataPresenter myDataPresenter = new MyDataPresenter(this); myDataPresenter.NewHttp(url,2); //listview设置条目点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); //实例化要接受数据的Fragment类 Fragment02_layout layout = new Fragment02_layout(); //调用他的方法 layout.getIdData(list.get(i).getCid()); //replace 进行切换 fragmentTransaction.replace(R.id.fr02_layout,layout); //提交 fragmentTransaction.commit(); } }); return view; } @Override public void toData(List<UserBean.DataBean> list, List<ProductBean.DataBean> plist, List<FenLeiBean.DataBean> flist) { Message msg = Message.obtain(); msg.obj = plist; handler.sendMessage(msg); } }
//条目点击被切换的Fragment 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:src="@mipmap/ic_launcher" android:id="@+id/fr02_img"/> <!-- 这个布局可以是ListView,可以使Recyclerview进行展示数据 --> <ExpandableListView android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:groupIndicator="@null" android:id="@+id/fr02_ex"> </ExpandableListView> </LinearLayout>
//加载上方布局的activity类
package com.example.com.jingdong.fragment; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.com.jingdong.Myutis.OkhttpUtils; import com.example.com.jingdong.R; import com.example.com.jingdong.adapter.Expandable_adapter; import com.example.com.jingdong.mobile.pojo.FenLeiBean; import com.example.com.jingdong.mobile.pojo.ProductBean; import com.example.com.jingdong.mobile.pojo.UserBean; import com.example.com.jingdong.presenter.MyDataPresenter; import com.example.com.jingdong.view.DataView; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2018/4/2. */ public class Fragment02_layout extends Fragment implements DataView{ private ExpandableListView fr02_ex; private ImageView img; private String url = "https://www.zhaoapi.cn/product/getProductCatagory?cid="; private List<List<FenLeiBean.DataBean.ListBean>> listbean = new ArrayList<>(); private List<FenLeiBean.DataBean> list; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); list = (List<FenLeiBean.DataBean>) msg.obj; //Toast.makeText(getActivity(), "list.size():" + list.size(), Toast.LENGTH_SHORT).show(); for (int i = 0; i < list.size(); i++) { listbean.add(list.get(i).getList()); } Expandable_adapter expandable_adapter = new Expandable_adapter(getActivity(), list, listbean); fr02_ex.setAdapter(expandable_adapter); for (int i = 0; i< list.size(); i++) { fr02_ex.expandGroup(i); }; } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fr02_layout,container,false); img = view.findViewById(R.id.fr02_img); fr02_ex = view.findViewById(R.id.fr02_ex); //实例化P层进行控制m层进行访问数据 MyDataPresenter myDataPresenter = new MyDataPresenter(Fragment02_layout.this); //传入地址进行访问 myDataPresenter.NewHttp(url,3); return view; } //定义一个接受那个listview 点击item传入过来的cid; public void getIdData(int id){ //String url地址进行拼接字符串 url +=id; } //view层返回的数据 @Override public void toData(List<UserBean.DataBean> list, List<ProductBean.DataBean> plist, List<FenLeiBean.DataBean> flist) { //Message进行传入数据 Message msg = Message.obtain(); msg.obj = flist; //使用handler发送数据 handler.sendMessage(msg); } }