我可以减少离开当前活动并开始新活动所需的时间吗? (机器人)

时间:2014-10-28 13:35:46

标签: android android-activity android-fragments android-listview android-transitions

我有一个分层应用,其中根活动有ListView,根据点击的项目加载活动。该活动反过来根据ListView选择加载活动,依此类推。

我的过渡非常快,活动B在过渡动画结束后立即加载。最慢的部分是开始过渡到下一个活动。

我点击了一个项目,它在过渡动画开始前半秒钟。是什么导致这种延迟?使用onBackPressed转到父活动(single_instance_top,而不是新实例或重新创建)时,存在相同的延迟。这是内置的延迟,以便用户在下一个屏幕加载之前可以看到他们选择的任何视觉效果吗?

这是我的一项活动。从我的onItemClick适配器调用ListView方法,其onClick方法除了调用所看到的方法之外什么都不做。有什么建议?

public class Activity_Course extends ActionBarActivity {

    static final String COURSE_ID = "my.COURSE_ID";
    public int courseId;

    public static boolean needsRefresh = false;
    public static boolean isRecreated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        setContentView(R.layout.activity_course);


        if (savedInstanceState == null) {
            Handler h = new Handler();
            h.post(new Runnable(){
                public void run(){
                    // do your heave tasks
                    Intent intent = getIntent();
                    courseId = intent.getIntExtra(MainFragment.COURSE_ID, 0);
                    getSupportFragmentManager().beginTransaction()
                    .add(R.id.course_activity_frame, new CourseActivityFragment()).commit();
                }
            });
        } else {
            courseId = savedInstanceState.getInt(COURSE_ID);
            Activity_Course.isRecreated = true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.course, menu);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {                   
             menu.findItem(R.id.menu_overflow).setIcon(R.drawable.ic_menu_moreoverflow_normal_holo_light);
         }
        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();
        if (id == R.id.edit_course_menu_item) {
            Intent i = new Intent(this, Activity_EditCourse.class);
            i.putExtra(COURSE_ID, courseId);
            startActivity(i);
        } else if(id == R.id.edit_course_gpa_menu_item){
            Intent i = new Intent(this, Activity_EditCourseGPA.class);
            i.putExtra(COURSE_ID, courseId);
            startActivity(i);
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class CourseActivityFragment extends Fragment {

        public final static String GRADE_ID = "my.GRADE_ID";
        public final static String CAT_ID = "my.CAT_ID";
        public final static String COURSE_ID = "my.COURSE_ID";
        public final static String CAT_STRING = "my.CAT_STRING";
        public final static String COURSE_STRING = "my.COURSE_STRING";
        public final static String SEMESTER_STRING = "my.SEMESTER_STRING";

        View rootView;
        int courseId;
        DBAdapter db;
        ScaledGradeHolder gholder;
        ProgressBar pbar;
        ListView categoryListView;
        TextView percentAvg_tv;
        TextView gpa_tv;
        TextView letterGrade_tv;
        Activity curActivity;
        CourseActivityFragment curFrag;
        int gradeColor;
        Course course;
        Adapter_CategoryList catAdapter;
        public final static String CATEGORY_ID = "my.CATEGORY_ID";

        public ArrayList<Category> catList = new ArrayList<Category>();

        public CourseActivityFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.fragment_course,
                    container, false);

            db = new DBAdapter(this.getActivity());
            curActivity = this.getActivity();
            curFrag = this;

            if (savedInstanceState != null) {
                courseId = savedInstanceState.getInt(COURSE_ID);

                if(catAdapter != null){
                    catAdapter.clear();
                    catList.clear();
                }

            } else {
                courseId = ((Activity_Course) this.getActivity()).courseId;
            }


            if(Activity_Course.isRecreated){
                if(catAdapter != null)
                    catAdapter.clear();
                Activity_Course.isRecreated = false;
            }


            db.open();
            course = db.getCourse(courseId);
            gholder = db.getScaledCourseGradeHolder(courseId);
            db.close();

            setViews();

            setListData();

            Resources res = getResources();
            categoryListView = (ListView) rootView.findViewById(R.id.cat_list_view);

//                  Applies the adapter to populate the ListView
            catAdapter = new Adapter_CategoryList(curActivity, catList, res, curFrag);
            categoryListView.setAdapter(catAdapter);
            pbar.setVisibility(View.GONE);







            return rootView;
        }

        public void setListData()
        {
            db.open();
            Category[] gt = db.getCategoriesInCourse(courseId);
            db.close();
            if(gt!=null){
                for(int i = 0; i < gt.length; i++) {
                    catList.add(gt[i]);
                }
            }
        }

        public void setViews(){
            // Set header info
            pbar = (ProgressBar)rootView.findViewById(R.id.progressBar1);
            TextView header = (TextView)rootView.findViewById(R.id.course_header);
            header.setText(course.getTitle());
            ((TextView) rootView.findViewById(R.id.semester_header)).setText(course.getSemesterString());

            setGradeSummaryViews();

//          Set column headers of the gradeType ListView
            LinearLayout catHeader = (LinearLayout) rootView.findViewById(R.id.linLayCatheader);
            TextView tv = (TextView)catHeader.findViewById(R.id.category_column);
            TextView tv2 = (TextView)catHeader.findViewById(R.id.weight_column);
            TextView tv3 = (TextView)catHeader.findViewById(R.id.gradecount_column);
            TextView tv4 = (TextView)catHeader.findViewById(R.id.percentage_column);
            tv.setText(R.string.cat_header);
            tv2.setText(R.string.weight_header);
            tv3.setText(R.string.count_header);
            tv4.setText(R.string.percent_header);
        }

        public void setGradeSummaryViews(){
            db.open();
            gholder = db.getScaledCourseGradeHolder(courseId);
            this.getActivity().setTitle(db.getCourse(courseId).getTitle());
            db.close();

            gradeColor = gholder.getColorId();

            // Set gpa
            gpa_tv = (TextView) rootView.findViewById(R.id.gpaGradeTextView);
            gpa_tv.setText(gholder.getGpaGradeString());
            gpa_tv.setTextColor(gradeColor);

            // Set percent average
            percentAvg_tv = (TextView) rootView.findViewById(R.id.percentTextView);
            percentAvg_tv.setText(gholder.getNumberGradeStringPercent());
            percentAvg_tv.setTextColor(gradeColor);

            // Set letter grade
            letterGrade_tv = (TextView) rootView.findViewById(R.id.letterGradeTextView);
            letterGrade_tv.setText(gholder.getLetterGrade());
            letterGrade_tv.setTextColor(gradeColor);
        }

         public void onItemClick(int position){

                Intent i = new Intent(this.getActivity(), Activity_Category.class);
                i.putExtra(CATEGORY_ID, catAdapter.getItem(position).getCategoryId());
                i.putExtra(MainFragment.COURSE_ID, courseId);
                Activity_Category.catId = catAdapter.getItem(position).getCategoryId();
                startActivity(i);


          }

         @Override
            public void onResume(){
                super.onResume();
                if(categoryListView == null)
                    return;
                int cnt = categoryListView.getChildCount();
                for(int i = 0; i < cnt; i++){
                    categoryListView.getChildAt(i).setBackgroundColor(0x00000000);
                }
                //coursesListView.setAdapter(courseAdapter);
            }

         @Override
         public void onStart(){
             super.onStart();
             if(Activity_Course.needsRefresh){
                    catList.clear();
                    setListData();
                    catAdapter.notifyDataSetChanged();
                    setGradeSummaryViews();
                    Activity_Course.needsRefresh = false;
              }
         }

         @Override
            public void onSaveInstanceState(Bundle outState){
                super.onSaveInstanceState(outState);
                outState.putInt(COURSE_ID, courseId);
            }

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        // Save current CourseId
        savedInstanceState.putInt(COURSE_ID, courseId);

        // Call superclass to save view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }

    @Override
    public boolean onNavigateUp() { 
        boolean x = super.onSupportNavigateUp();
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        return x;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以更改活动之间的“默认”转换,并使用一些非常快的转换。请参阅:example

此外,您可以在onCreateonResume中执行更少的操作,因此加载时间将非常短,并在使用处理程序短暂延迟(50 - 100 ms)后开始加载和填充数据。