简体   繁体   English

将大量文本设置为 Textview 的最佳方法

[英]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!!!在我的应用程序中,有一个部分我们称之为 X。X 部分有 5 个选项卡(使用 Viewpager 和...)当用户单击按钮启动 X 活动时,因为,我必须为所有选项卡设置文本(其中有 5 个),有延迟,根据手机不同,在低级别手机上大约是 4-7 秒!!! 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.我的方式: 1- 更改我的 UI 设计并将 5 个选项卡拆分为 5 个单个活动。 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) 2- 使用进度条(或类似进度条的东西)并且不要改变我的设计。(问题是我不知道如何做到这一点,我的意思是我不知道所有文本何时设置)

what is the other solutions?其他解决方案是什么? So, if i want to use AsynckTask, the code would be like this right?那么,如果我想使用 AsynckTask,代码应该是这样的吧? 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.这段代码的行为是这样的:当用户点击打开 X 活动时,用户停留在活动中,然后一段时间后,当所有文本都设置好时,x 活动是打开的,所以它根本没有帮助。 Were am i wrong?我错了吗?

Depending on how you are retrieving your text, consider using an AsyncTask .根据您检索文本的方式,请考虑使用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 文本的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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