Fragment 循环替换利用

public class MainActivity extends AppCompatActivity {
    String img="https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg";
    String a = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    private Handler handler = new Handler() {
        private Bitmap bitmap;
        private String infoText;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            switch (what) {
                case 1:
                    infoText = (String) msg.obj;
                    Log.d("TAG",infoText);
                    break;
                case 2:
                    bitmap = (Bitmap) msg.obj;

                    break;
            }
            if(infoText!=null&&bitmap!=null){
                mainFrag.aa(infoText,bitmap);
               transaction.add(R.id.fl,list.get(0)).commit();
                rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                        switch (i){
                            case R.id.rb1:
                                mainFrag.aa(infoText,bitmap);
                                manager.beginTransaction().replace(R.id.fl,list.get(0)).commit();
                                break;
                            case R.id.rb2:
                                mainFrag.aa("正在加載",null);
                              // mainFrag.ww("正在加載");
                                manager.beginTransaction().replace(R.id.fl,list.get(1)).commit();
                                break;
                            case R.id.rb3:
                               mainFrag.aa("正在加載",null);
                               //mainFrag.ww("正在加載");
                                manager.beginTransaction().replace(R.id.fl,list.get(2)).commit();
                                break;
                        }
                    }
                });
            }

        }
    };
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private mainFrag frag;
    private RadioGroup rg;
    List list = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg = (RadioGroup) findViewById(R.id.rg);
        indata();
        frag = new mainFrag();
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        onclick();
    }

    private void indata() {
        for (int i=0;i<3;i++){
            mainFrag frag = new mainFrag();
            list.add(frag);
        }
    }

    public void onclick(){

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        String netJson = Util.getNetJson(a);
                        //Log.e("wzq", "run: " + netJson);
                        Gson gson = new Gson();
                       // Log.d("TAG","infoText");
                        News product = gson.fromJson(netJson, News.class);
                       // List data = product.getData();
                        String infoText = product.getInfo();
                        Bitmap bitmap = Util.getBitmap(img);
                        Message message = new Message();
                        message.obj = infoText;
                        message.what = 1;
                        handler.sendMessage(message);

                        Message message1 = new Message();
                        message1.obj = bitmap;
                        message1.what = 2;
                        handler.sendMessage(message1);

                    }
                }.start();
    }
}

你可能感兴趣的:(Fragment 循环替换利用)