2015-10-04 44 views
1

我有一个日历,所以我必须按照7个连续的52行显示365天。每一天都有不同的文字。很多操作正在执行,同时生成一个。所以滚动他们是如此缓慢和迟缓。如何解决它?Android的大网格视图比较滞后,移动速度很慢

我的网格类的父:

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     int nr = calendar.get(Calendar.DAY_OF_YEAR); 
     setContentView(R.layout.activity_main); 
     calendar.setTime(new Date()); 
     int year = calendar.get(Calendar.YEAR); 
     boolean p = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); 
     calendar.set(Calendar.DAY_OF_YEAR, 1); 
     int before = calendar.get(Calendar.DAY_OF_WEEK)-2; 
     calendar.set(Calendar.DAY_OF_YEAR, 1); 
     calendar.add(Calendar.YEAR, 1); 
     calendar.add(Calendar.DAY_OF_YEAR, -1); 
     int after = calendar.get(Calendar.DAY_OF_WEEK)-2; 
     final GridView gridView = (GridView) findViewById(R.id.main_grid_calendar); 
     gridView.setAdapter(new DayAdapter(this, new String[(p?366:365)], before, after)); 

    } 
} 

我的适配器类:

public class DayAdapter extends ArrayAdapter<String> { 
    private Context context; 
    private Calendar calendar = Calendar.getInstance(); 
    private final int today = calendar.get(Calendar.DAY_OF_YEAR), before, after, count; 

    public DayAdapter(Context context, String[] values, int before, int after) { 
     super(context, R.layout.calendar_day, new String[before + values.length + after]); 
     this.context = context; 
     this.before = before; 
     this.after = after; 
     this.count = before + values.length + after; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View day = inflater.inflate(R.layout.calendar_day, parent, false); 
     final TextView date = (TextView) day.findViewById(R.id.dayitem_text_number); 
     final TextView holiday = (TextView) day.findViewById(R.id.dayitem_text_holiday); 
     final TextView click = (TextView) day.findViewById(R.id.dayitem_text_more); 

     SharedPreferences theme = Data.getPreferences(context, Data.Prefs.THEME); 
     Data.AppColorSet color = Data.getColors(Integer.parseInt(theme.getString(Data.appThemeSetings, "1"))); 

     date.setTextColor(color.foreground); 
     holiday.setTextColor(color.foreground); 
     click.setTextColor(color.foreground); 
     day.setBackgroundColor(color.background); 

     calendar.set(Calendar.DAY_OF_YEAR, position+1-before); 
     if (position<before) { 
      day.setBackgroundColor(Color.GRAY); 
     } else if (position>=count-after) { 
      day.setBackgroundColor(Color.GRAY); 
     } else { 
      day.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(context, DayActivity.class); 
        intent.putExtra("id", position-3); 
        intent.putExtra("from", "calendar"); 
        context.startActivity(intent); 
       } 
      }); 
     } 
     String d = "" + calendar.get(Calendar.DAY_OF_MONTH); 
     String m = "" + (1 + calendar.get(Calendar.MONTH)); 
     if (Integer.parseInt(d)<10) { 
      d = "0" + d; 
     } 
     if (Integer.parseInt(m)<10) { 
      m = "0" + m; 
     } 
     if (this.today==position-2) { 
      RelativeLayout layout = (RelativeLayout) day.findViewById(R.id.dayitem_relative_main); 
      layout.setBackgroundColor(color.dark?Color.rgb(55, 0, 0):Color.rgb(255, 200, 200)); 
     } 
     final String today = d + "." + m; 
     date.setText(today); 
     String text = HolidayCalendar.getInstance(context).getTexts(today).get(0); 
     String[] arr = text.split(" "); 
     String result = ""; 
     final int words = 4; 
     boolean full = false; 
     if (arr.length<=words) { 
      result = text; 
      full = true; 
     } else { 
      for (int i = 0; i<words; i++) { 
       result += " " + arr[i]; 
      } 
      result += "..."; 
     } 
     int number = HolidayCalendar.getInstance(context).getTexts(today).size() - (full?1:0); 
     if (number != 0) { 
      click.setText(number + " " + context.getResources().getString(R.string.see_more)); 
     } 
     holiday.setText(result); 
     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int width = size.x; 
     day.setLayoutParams(new AbsListView.LayoutParams(width/7, width/4)); 
     return day; 
    } 
} 

我的网格布局的父:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/main_relative_main" 
    tools:context="${relativePackage}.${activityClass}" > 

    <GridView 
     android:id="@+id/main_grid_calendar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/main_adview_bottom_dark" 
     android:background="#000000" 
     android:horizontalSpacing="1dp" 
     android:numColumns="7" 
     android:verticalSpacing="1dp" > 

    </GridView> 

</RelativeLayout> 

我的适配器布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dayitem_relative_main" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="#FFFFFF" 
    android:gravity="center" > 

    <TextView 
     android:id="@+id/dayitem_text_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="8sp" /> 

    <TextView 
     android:id="@+id/dayitem_text_more" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="2dp" 
     android:gravity="center_horizontal" 
     android:textSize="8sp" /> 

    <TextView 
     android:id="@+id/dayitem_text_holiday" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginBottom="15dp" 
     android:gravity="center|center" 
     android:textSize="10sp" /> 

</RelativeLayout> 

编辑: ViewHolder帮助了我!谢谢!

回答

1

显示列表和网格时,最容易和最容易提高性能的方法是使用新的RecyclerView,在您的情况下,使用GridLayoutManager。这可以在不修改大量代码的情况下提高性能。看看如何实现它的一些文档here

除此之外,您的痛苦来自在您的getView方法中使用findViewById的事实,这是一种重复多次的缓慢操作。避免始终搜索视图的最佳做法是使用ViewHolder模式。看看这link的第二部分。

+0

非常感谢!我已经完成了! – Andret