片段转换并不像应该的那样平滑

时间:2018-05-13 08:51:11

标签: android android-fragments animation bottomnavigationview

我正在使用底部导航和片段在我的应用程序中显示数据但是在我开始在我的应用程序中加载数据之后,片段转换的动画开始卡住并且应用程序的整体性能看起来很难看我不知道这是怎么回事,没有数据片段转换工作就好了。

这是我如何加载数据和调用片段。

  

MainActivity.java

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        android.app.FragmentManager manager = getFragmentManager();
        switch (item.getItemId()) {
            case R.id.navigation_schedule:
                Fragment scheduleFragment = new ScheduleFragment();
                    manager.beginTransaction().replace(R.id.main, scheduleFragment).commit();
                return true;
            case R.id.navigation_teams:
                Fragment teamFragment = new TeamFragment();
                manager.beginTransaction().replace(R.id.main, teamFragment).commit();
                return true;
            case R.id.navigation_champions:
                Fragment championsFragment = new PointTableFragment();
                manager.beginTransaction().replace(R.id.main, championsFragment).commit();
                return true;
            case R.id.navigation_live:
                Fragment liveFragment = new LiveFragment();
                manager.beginTransaction().replace(R.id.main, liveFragment).commit();
                return true;
            case R.id.navigation_records:
                Fragment recordsFragment = new RecordsFragment();
                manager.beginTransaction().replace(R.id.main, recordsFragment).commit();
                return true;
        }
        return false;
    }
};
  

Fragment.java

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_team, null);
    lvTeam = (ListView)view.findViewById(R.id.lvTeam);

    DatabaseHandler db = new DatabaseHandler(this.getContext());
    listTeam = db.displayTeam();


    adapterTeam = new CustomAdapterTeam(this.getContext(),listTeam);
    lvTeam.setAdapter(adapterTeam);
    adapterTeam.notifyDataSetChanged();


    return view;
}

任何人都可以解释是什么让过渡看起来像是有毛刺,在过渡期间它会有点棒。

1 个答案:

答案 0 :(得分:1)

  

片段转换并不像应该的那样平滑

首先,Fragment事务是顺利的,但是你在UI线程中做了一些长时间的操作,所以UI卡住了。并且你觉得片段UI并不像我应该的那样流畅。

  • 我认为您的列表包含大量数据,您只需在UI thread中填写一次。您可以使用paging来加载大数据,而不是这个。
  • listTeam = db.displayTeam();,其他的可能是,你可能是在UI线程中从数据库中获取数据。如果你在AsyncTask中这样做。你将克服这个问题。

或者您可以延迟长时间操作,以便在片段打开后加载数据。 (不推荐

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_team, null);
    lvTeam = (ListView)view.findViewById(R.id.lvTeam);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DatabaseHandler db = new DatabaseHandler(getContext());
                    listTeam = db.displayTeam();
                    adapterTeam = new CustomAdapterTeam(getContext(),listTeam);
                    lvTeam.setAdapter(adapterTeam);
                    adapterTeam.notifyDataSetChanged();
                }
            });
        }
    },100);
    return view;
}
  • 请注意,您无法在worker thread
  • 中执行UI操作

修改

this.getContext()更改为getContext()。因为这是指Runnable