用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - ios代码库

fragment动态传值

2017-03-01 作者: 老四和老三的老二都老大了举报

[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;
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...