关闭

往fragment传值参考代码

166人阅读 评论(0) 收藏 举报
分类:

BaseFragment

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.Gson;

import android.os.Bundle;
import android.os.Handler;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public abstract class BaseFragment extends Fragment {
    protected static final String TAG = "BaseFragment";
    protected static final int SUCCESS = 0;
    private View view;
    private ListView listView;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == SUCCESS) {
                String json = (String) msg.obj;
                Gson gson = new Gson();
                final WeiChatInfo fromJson = gson.fromJson(json,
                        WeiChatInfo.class);
                listView.setAdapter(new BaseAdapter() {
                    @Override
                    public View getView(int position, View convertView,
                            ViewGroup parent) {
                        View view2 = View.inflate(getActivity(),
                                R.layout.lv_item, null);
                        ImageView lv_image = (ImageView) view2
                                .findViewById(R.id.lv_image);
                        TextView lv_title = (TextView) view2
                                .findViewById(R.id.lv_title);
                        lv_title.setText(fromJson.result.list.get(position).title);

                        ImageUtils imageUtils = new ImageUtils();
                        imageUtils.getImage(
                                fromJson.result.list.get(position).firstImg,
                                lv_image);

                        return view2;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        return fromJson.result.list.size();
                    }
                });

            }
        };
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.list_layout, container, false);
        listView = (ListView) view.findViewById(R.id.listView);
        return view;
    }

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

        // 请求网络数据
        getData();
    }

    private void getData() {
        new Thread() {
            public void run() {
                int pno = getPno();
                String path = "http://v.juhe.cn/weixin/query?key=0917c812f187b92e025c9b7c2d4e59b5&pno="
                        + pno;
                try {
                    URL url = new URL(path);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url
                            .openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setReadTimeout(5000);
                    httpURLConnection.setRequestMethod("GET");
                    int responseCode = httpURLConnection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = httpURLConnection
                                .getInputStream();
                        // 读出来---String
                        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = inputStream.read(buffer)) != -1) {
                            arrayOutputStream.write(buffer, 0, len);
                        }
                        String string = arrayOutputStream.toString();
                        // 解析json
                        Log.i(TAG, "string:" + string);
                        handler.obtainMessage(SUCCESS, string).sendToTarget();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }

    public abstract int getPno();
}

fragment1

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class Fragment0 extends BaseFragment {

    private int pno;

    @Override
    public int getPno() {
        // 返回
        return pno;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取参数
        Bundle arguments = getArguments();
        pno = (int) arguments.getInt("pno");
    }
    // 先获取Fragment实例,顺便传值
    public static Fragment getInstance(int pno) {
        // 自己创建Fragmnt
        Fragment0 fragment0 = new Fragment0();
        // 存储参数
        Bundle bundle = new Bundle();
        bundle.putInt("pno", pno);
        // 设置进去
        fragment0.setArguments(bundle);
        return fragment0;
    }

}``

调用

viewPager.setAdapter(new FragmentPagerAdapter(
                getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return 5;
            }

            @Override
            public Fragment getItem(int position) {
                Fragment fragment0 = Fragment0.getInstance(position + 1);
                return fragment0;
            }
        });
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1061次
    • 积分:51
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:4篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论