简体   繁体   中英

Best way to set a big Amount of text to Textview

I faced a new problem. In my app, there is a part we called it X. The X part have 5 tab ( using Viewpager and ...) When user click on a button to start X activity, because, I must set text for all of the tabs (and there is 5 of them),there is a delay, based on the phone it's different, on low level phone its about, 4-7 second!!! so, what can i do to solve the problem? my way: 1- change my UI design and split the 5 tab into, 5 single activity. 2- use an progress bar( or something like progress bar) and don't change my design.( the problem is i don't know how to do this, i mean i don't know when all of the text are set)

what is the other solutions? So, if i want to use AsynckTask, the code would be like this right? Main:

public class Law_main extends AppCompatActivity {
    FrameLayout fl_TEMP;
    TextView btn_ASD;
    ViewPager viewPager;
    PagerAdapter adapter;
    TabLayout tabLayout;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.law_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("TAB 1"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 2"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 3"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 4"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


        fl_TEMP = (FrameLayout) findViewById(R.id.TEMP);
        btn_ASD = (TextView) findViewById(R.id.ASD);


        new TxtTimer().execute();


        final View appbar = findViewById(R.id.appbar);

        viewPager = (ViewPager) findViewById(R.id.pager);



        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }




    private class TxtTimer extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {


            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
            viewPager.setAdapter(adapter);

            viewPager.setCurrentItem(1, true);
            progressBar.setVisibility(View.GONE);
        }
    }
}

this code act like this: When user Click on to open X activity, user stay in the activity and then after a while, when all text got set, the x activity is open, so it's not helping at all. Were am i wrong?

Depending on how you are retrieving your text, consider using an AsyncTask . You can also put a progress circle on the fragment whilst it is loading so that the app doesn't appear to have frozen.

基于 RobVoisey 所说的内容,并且不确切知道您的用例,我会首先加载第一个选项卡的文本,以便用户查看并同时使用AsyncTask加载其他选项卡的文本,同时在适当的位置和/或显示加载图形必要的。

在选项卡中使用片段并使用片段生命周期按需加载内容,您将在每个片段上加载 1/5 文本的起点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM