0

我有一個分級應用程序,根活動有一個ListView,它根據哪個項目被點擊加載一個活動。該活動依次加載基於ListView選擇等的活動。我可以減少離開當前活動並開始新活動所需的時間嗎? (Android)

我的轉換非常快,活動B在轉換動畫結束後立即加載。最慢的部分是開始轉換到下一個活動。

我點擊一個項目,在過渡動畫開始前半秒。是什麼造成這種延誤?使用onBackPressed轉到父活動(single_instance_top,而不是新實例或重新創建)時存在相同的延遲。這是否是內置的延遲,以便用戶在下一個屏幕加載之前可以看到其選擇的任何視覺效果?

這是我的一項活動。我的ListView適配器調用了onItemClick方法,該適配器的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; 
    } 

} 
+0

你試過跟蹤視圖,看看時間在哪裏? – Panther 2014-10-28 13:37:32

+0

我還沒有使用過那個功能呢...它是如何使用的? – NSouth 2014-10-28 13:38:22

+0

你應該學會在'traceview'中使用'timeline'。然而,就一開始而言,您可以考慮在方法開始和結束時的日誌時間,您認爲需要花費時間並查看哪種方法實際上會消耗更多時間 – Panther 2014-10-28 13:40:03

回答

1

您可以更改活動之間的「默認」轉換,並使用非常快的轉換。請參閱:example

您也可以在onCreateonResume少做事情,所以加載時間會很短,很短的延遲後開始加載和數據的填充(50 - 100毫秒),與處理程序。

相關問題