Android大gridview滞后,移动缓慢

标签 android memory gridview android-arrayadapter lag

我有一个日历,所以我必须将 365 天显示在 7 行 52 行中。每一天都有不同的文字。生成一个时正在执行大量操作。所以滚动它们是如此缓慢和滞后。如何解决?

我的网格类父类:

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 帮助了我!谢谢!

最佳答案

在显示列表和网格时轻松提高性能的最佳方法和更简单的方法是使用新的 RecyclerView,在您的情况下,使用 GridLayoutManager。这将在不修改大量代码的情况下提高性能。查看一些有关如何实现它的文档 here .

除此之外,您遇到的滞后是因为在您的 getView 方法中使用了 findViewById,这是一个重复多次的缓慢操作。避免一直搜索 View 的最佳做法是使用 ViewHolder 模式。查看此 link 中的第二部分.

关于Android大gridview滞后,移动缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934676/

相关文章:

java - 缺少 Android 网络连接状态。

安卓工作室 : Gradle - build fails -- Execution failed for task ':dexDebug'

c++ - 动态二维数组非连续内存 C++

c# - 在 gridview 中分页时试图了解错误的来源

c# - ASP.NET Gridview 的编辑文本框

android - 使用 Android Studio 的补丁支持库

java - 我丢失了调试 keystore 文件

java - "Unknown error"对所有关系进行密码查询

memory - 不可变可以是内存 pig 吗?

c# - 绝对 gridview 标题不随页面操作移动