Handler延时跳转(Fragment显/隐)

MianActivity

package com.example.lenovo.mnyk01;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final int FLAG = 123;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

           if(msg.what==FLAG){
               Intent intent = new Intent(MainActivity.this,ShowActivity.class);
               startActivity(intent);
           }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     TextView txt = findViewById(R.id.text);

    handler.sendEmptyMessageDelayed(FLAG,3000);


    }
}

ShowActivity

package com.example.lenovo.mnyk01;



import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.TextView;

import com.example.lenovo.mnyk01.fragment.FinishFragment;
import com.example.lenovo.mnyk01.fragment.ShowFragment;




public class ShowActivity extends AppCompatActivity {

    private static final String url ="http://172.17.8.100/mobile/exam/findNewList";
    private ShowFragment showFragment;
    private FinishFragment finishFragment;
    private FragmentManager manager;
    private FragmentTransaction begin;
    private TextView tiao;

      private int page = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        tiao = findViewById(R.id.tiao);

        showFragment = new ShowFragment();
        finishFragment = new FinishFragment();

        manager = getSupportFragmentManager();
        begin = manager.beginTransaction();
         begin.add(R.id.content,showFragment)
                 .add(R.id.content,finishFragment)
                 .hide(finishFragment).commit();


       tiao.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                     if(page ==0){
                         page = 1;
                         manager.beginTransaction()
                                 .hide(showFragment).show(finishFragment).commit();

                     }else{
                         page = 0;
                         manager.beginTransaction()
                                 .show(showFragment).hide(finishFragment).commit();
                     }
           }
       });
    }
}

//showLayout






 

  

  

 

 


 

FirstFragment

package com.example.lenovo.mnyk01.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.lenovo.mnyk01.HttpUtils;
import com.example.lenovo.mnyk01.Newss;
import com.example.lenovo.mnyk01.NewssAdapter;
import com.example.lenovo.mnyk01.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */

public class FinishFragment extends Fragment{

    private ListView xlvNews;
 private    List list;

    private static String url = "https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
    private NewssAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.finishfragment,null,false);
        xlvNews = v.findViewById(R.id.xlv);
        return v;
    }

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

        list = new ArrayList<>();
        adapter = new NewssAdapter(getActivity(),list);
        xlvNews.setAdapter(adapter);


        new AsyncTask(){

            @Override
            protected String doInBackground(String... strings) {
                String result = HttpUtils.getFromHttpUrlConnection(strings[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                Newss newss = gson.fromJson(s, Newss.class);
                list.clear();
                list.addAll(newss.getBody().getResult());
                adapter.notifyDataSetChanged();


            }
        }.execute(url);

    }
}

 

ShowFragment

package com.example.lenovo.mnyk01.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lenovo.mnyk01.HttpUtils;
import com.example.lenovo.mnyk01.News;
import com.example.lenovo.mnyk01.NewsAdapter;
import com.example.lenovo.mnyk01.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 */

public class ShowFragment extends Fragment{
    private static final String url ="http://172.17.8.100/mobile/exam/findNewList";
    private TextView tiao;
    private ListView lvNews;
    private List list;
    private NewsAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.showfragment,null,false);

        lvNews = v.findViewById(R.id.lv_news);
        return v;
    }

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

       list= new ArrayList<>();
        adapter = new NewsAdapter(getContext(),list);
        lvNews.setAdapter(adapter);

        new AsyncTask(){

            @Override
            protected String doInBackground(String... strings) {
                String result = HttpUtils.getFromHttpUrlConnection(strings[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                News news = gson.fromJson(s, News.class);
                list.clear();
                list.addAll(news.getResult().getData());
                adapter.notifyDataSetChanged();

            }
        }.execute(url);
    }
}

 

 

 

 

 

 

你可能感兴趣的:(Handler延时跳转(Fragment显/隐))