如何知道活动何时完成加载

时间:2017-11-19 01:21:37

标签: java android android-activity screen

我有一个加载数据列表的活动,此活动是带有3个标签的TabActivity,所有标签都会加载不同的数据类型。

但数据可能真的是一堆数据,因此我的活动可能需要花费很多时间才能显示出来。

我使用

开始活动
Intent intent = new Intent(MainActivity.this, PcapAnalisys.class);
startActivity(intent);

然后,当活动加载时显示黑屏,并在几秒钟后正确显示内容。

有一种方法可以显示如下信息:“正在加载活动......请稍等”或其他什么?

这是我的TabActivity的代码:

public class PcapAnalysis extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pcap_analysis);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mViewPager = (ViewPager) findViewById(R.id.container);
    tabLayout = (TabLayout) findViewById(R.id.tabs);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    //fab.setImageResource(R.drawable.ic_attach_file_white_24dp);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FileChooserDialog dialog = new FileChooserDialog(PcapAnalysis.this);
            dialog.show();

            dialog.addListener(new FileChooserDialog.OnFileSelectedListener() {
                @Override
                public void onFileSelected(Dialog source, File file) {
                    source.hide();
                    Toast.makeText(source.getContext(),
                            "File selected: " + file.getAbsolutePath(),
                            Toast.LENGTH_LONG).show();

                    Log.i("Archivo seleccionado", file.getAbsolutePath());

                    new AsyncTask_load(PcapAnalysis.this, file.getAbsolutePath()).execute();
                }

                public void onFileSelected(Dialog source, File folder, String name) {
                    source.hide();
                    Toast.makeText(source.getContext(),
                            "File created: " + folder.getName() + "/" + name,
                            Toast.LENGTH_LONG).show();
                }
            });
        }
    });

    new Task().execute();

}

@Override
public void recreate()
{
    super.recreate();
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_pcap_analysis, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/*public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_pcap_analysis, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}*/

private class Task extends AsyncTask<Void, Integer, Void> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(PcapAnalysis.this);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage("Procesando...");
        pDialog.setCancelable(true);
        pDialog.setMax(100);
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager.setOffscreenPageLimit(3); //cuantas paginas se precargan
        mViewPager.setAdapter(mSectionsPagerAdapter);

        tabLayout.setupWithViewPager(mViewPager);

        //publishProgress(progress);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
    }

    @Override
    protected void onPostExecute(Void result) {
        pDialog.dismiss();
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private Fragment http = new HttpList();
    private Fragment dns = new DnsList();
    private Fragment ip = new IpList();
    private Fragment resume = new ResumeList();

    SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position) {
            case 0:
                return http;
            case 1:
                return dns;
            case 2:
                return ip;
            case 3:
                return resume;
        }
        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "HTTP";
            case 1:
                return "DNS";
            case 2:
                return "RESOURCE IP";
            case 3:
                return "RESUME";
        }
        return null;
    }
}
}

正如您所看到的,此活动使用片段来显示数据,并且每个片段都有一个用于加载数据的cicle(即在String数组中)。

此外,我使用AsyncTask尝试解决此问题,但无法正常工作。请帮忙!

0 个答案:

没有答案