Java 自定义视图日历(TableLayout)速度非常慢
我从头创建了一个日历自定义视图。我不使用日历视图的原因是我想自己定制它。我需要一些Java 自定义视图日历(TableLayout)速度非常慢,java,android,Java,Android,我从头创建了一个日历自定义视图。我不使用日历视图的原因是我想自己定制它。我需要一些日历视图没有的函数。因此,我构建了一个CustomView: calendar.xml: <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="
日历视图
没有的函数。因此,我构建了一个CustomView:
calendar.xml:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:id="@+id/calender">
<TextView
android:id="@+id/calender_text_view_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/calender_button_next_month"
app:layout_constraintBottom_toBottomOf="@id/calender_button_next_month"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
/>
<ImageButton
android:id="@+id/calender_button_next_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/ic_navigate_next_colorprimary_36dp"
android:background="@android:color/transparent"
android:contentDescription="@null"/>
<ImageButton
android:id="@+id/calender_button_prev_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.2"
android:src="@drawable/ic_navigate_before_colorprimary_36dp"
android:background="@android:color/transparent"
android:contentDescription="@null"
/>
<TableLayout
android:id="@+id/calender_table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/calender_text_view_month"
android:layout_marginTop="30dp"
android:divider="@drawable/recyclerview_divider"
android:showDividers="middle"
android:background="@drawable/table_stroke_color_primary"
/>
</merge>
calendar_item.xml:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
>
<TextView
android:id="@+id/text_view_calender_item_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="4dp"
android:clickable="false"
android:textAlignment="center"
/>
<TextView
android:id="@+id/text_view_calender_item_routine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/text_view_calender_item_date"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"
android:clickable="false"
android:textAlignment="center"
/>
</merge>
此日历自定义视图有效。每一项都是它应该在的地方。但问题是它需要非常长的时间来构建,最多需要一两秒钟。每次我用日历打开片段时,应用程序都会冻结一两秒钟。我无法解释为什么会这样
如果需要,我可以编辑实现或其他代码。但我认为这才是最重要的。好的,我会自己回答这个问题,因为我设法解决了这个问题。它非常慢,因为is每次渲染超过30个
CalenderItem
s,其中包含一个LinearLayout
和两个TextView
s。我将其更改为只有一个带有“\n”的TextView
,使其看起来像两个TextView
s。因此,我还可以删除布局并扩展TextView
类。因此,现在它必须比以前少渲染30多个TextView
s和版面,这使得它的速度更快。您可以使用RecyclerView
而不是TableView
来改进您的自定义日历视图。使用RecyclerView
将使您能够定制日期单元格(选择/取消选择、选择范围等),支持滚动、滑动等
或者您可以使用,它提供高度可定制的日历视图
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
>
<TextView
android:id="@+id/text_view_calender_item_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="4dp"
android:clickable="false"
android:textAlignment="center"
/>
<TextView
android:id="@+id/text_view_calender_item_routine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/text_view_calender_item_date"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"
android:clickable="false"
android:textAlignment="center"
/>
</merge>
public class CalenderItem extends ConstraintLayout {
private final TextView tvDate;
private final TextView tvRoutine;
private IItemClicked listener = null;
public void setOnItemClickedListener(IItemClicked listener) {
this.listener = listener;
}
public CalenderItem(Context context) {
this(context, null);
}
public CalenderItem(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CalenderItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater l = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
l.inflate(R.layout.calender_item, this, true);
tvDate = findViewById(R.id.text_view_calender_item_date);
tvRoutine = findViewById(R.id.text_view_calender_item_routine);
setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1));
setBackgroundResource(R.drawable.background_calender_item);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null) {
listener.itemClicked(day, month, year);
}
}
});
}
public interface IItemClicked {
void itemClicked(int day, int month, int year);
}
}