[ios]代码库
//主页面布局
<LinearLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:id="@+id/linear1"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:layout_toRightOf="@+id/linear1"
android:layout_width="260dp"
android:layout_height="match_parent"
android:id="@+id/linear2"
android:orientation="vertical"></LinearLayout>
//第一个Fragment布局
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:text="条目一"
android:textSize="20dp"/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:text="条目二"
android:textSize="20dp"/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv3"
android:text="条目三"
android:textSize="20dp"/>
//第二个Fragment布局
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
//适配器布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="TextView"
android:textSize="20dp"/>
<TextView
android:layout_marginTop="50dp"
android:layout_below="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:text="TextView"
android:textSize="20dp"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"
android:layout_alignParentRight="true"/>
//MainActivity
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.linear1,new OneFragment(),"fragment1");
transaction.add(R.id.linear2,new TwoFragment(),"fragment2");
transaction.commit();
//第一个Fragment
private TextView tv1;
private TextView tv2;
private TextView tv3;
private String result;
private JsonBean jsonBean;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view1=View.inflate(getActivity(),R.layout.fone,null);
tv1 = (TextView) view1.findViewById(R.id.tv1);
tv1.setOnClickListener(this);
tv2 = (TextView) view1.findViewById(R.id.tv2);
tv2.setOnClickListener(this);
tv3 = (TextView) view1.findViewById(R.id.tv3);
tv3.setOnClickListener(this);
new Thread(){
@Override
public void run() {
super.run();
getData();
//更新ui
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
//不要忘记start
}.start();
return view1;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv1:
List<Daa> list1=new ArrayList<Daa>();
Daa daa1=new Daa(jsonBean.datalist.get(0).course_name,jsonBean.datalist.get(0).course_price,jsonBean.datalist.get(0).course_pic);
list1.add(daa1);
Daa daa2=new Daa(jsonBean.datalist.get(1).course_name,jsonBean.datalist.get(1).course_price,jsonBean.datalist.get(1).course_pic);
list1.add(daa2);
EventBus.getDefault().post(new Message(list1));
break;
case R.id.tv2:
List<Daa> list2=new ArrayList<Daa>();
Daa daa11=new Daa(jsonBean.datalist.get(2).course_name,jsonBean.datalist.get(2).course_price,jsonBean.datalist.get(2).course_pic);
list2.add(daa11);
Daa daa22=new Daa(jsonBean.datalist.get(3).course_name,jsonBean.datalist.get(3).course_price,jsonBean.datalist.get(3).course_pic);
list2.add(daa22);
EventBus.getDefault().post(new Message(list2));
break;
case R.id.tv3:
List<Daa> list3=new ArrayList<Daa>();
Daa daa12=new Daa(jsonBean.datalist.get(4).course_name,jsonBean.datalist.get(4).course_price,jsonBean.datalist.get(4).course_pic);
list3.add(daa12);
Daa daa23=new Daa(jsonBean.datalist.get(5).course_name,jsonBean.datalist.get(5).course_price,jsonBean.datalist.get(5).course_pic);
list3.add(daa23);
EventBus.getDefault().post(new Message(list3));
break;
}
}
public void getData(){
String path="http://www.meirixue.com/api.php?c=list&a=index";
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
int code=connection.getResponseCode();
if(code==200){
InputStream inputStream=connection.getInputStream();
result = StreamUtils.parser(inputStream);
}
Gson gson=new Gson();
jsonBean = gson.fromJson(result, JsonBean.class);
} catch (Exception e) {
e.printStackTrace();
}
}
//第二个Fragment
private String b;
private TextView tv;
private ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view1=View.inflate(getActivity(),R.layout.ftwo,null);
lv = (ListView) view1.findViewById(R.id.lv);
//注册EventBus
EventBus.getDefault().register(this);
return view1;
}
@Subscribe
public void onEventMainThread(Message event) {
List<Daa> list=event.getList();
MyAdapter myAdapter = new MyAdapter(getActivity(), list);
lv.setAdapter(myAdapter);
}
@Override
public void onDestroy(){
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public class StreamUtils {
public static String parser(InputStream inputStream){
StringBuilder sbb=new StringBuilder();
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
try {
while((str=br.readLine())!=null){
sbb.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
return sbb.toString();
}
//EventBus自动义的类
public class Message {
public List<Daa> list;
public Message(List<Daa> list) {
this.list = list;
}
public List<Daa> getList() {
return list;
}
}
by: 发表于:2017-10-13 10:10:55 顶(0) | 踩(0) 回复
??
回复评论