【问题标题】:Android custom view in cell单元格中的Android自定义视图
【发布时间】:2017-09-14 14:09:55
【问题描述】:

我的活动中有一个RecyclerView,其中显示了 4 张不同类型的不同卡片。其中一张卡片显示了来自该库 WeekView 的日历自定义视图。一切正常,直到我开始滚动视图并且日历卡开始超出屏幕范围。自定义周视图似乎被绘制在工具栏层上,导致图像显示上述效果

如您所见,卡片视图滚动正确,但自定义周视图位于工具栏层上方。

在我的Adapter 中,我在我的ViewHolder 中初始化WeekView

calendarView = (WeekView) itemView.findViewById(R.id.todayView);
if (calendarView != null) {
            initializeCalendarView(calendarView);
        }


private void initializeCalendarView(WeekView mWeekView) {
    mWeekView.setOnEventClickListener(this);
    mWeekView.setMonthChangeListener(this);
    mWeekView.setEventLongPressListener(this);
    mWeekView.setEmptyViewLongPressListener(this);
    setupDateTimeInterpreter(false, mWeekView);
    mWeekView.goToToday();

    mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources().getDisplayMetrics()));
    mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics()));
    mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics()));
}

这是我的xml 我的CardView

   <android.support.v7.widget.CardView
    android:id="@+id/doctorDashboardCalendar"
    android:layout_width="match_parent"
    android:layout_height="800dp"
    android:layout_margin="@dimen/margin10"
    app:cardCornerRadius="3dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:background="#e0e0e0">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/notes_icon_dark"
                android:layout_margin="10dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text18"
                android:fontFamily="@string/font_name_2"
                android:text="Calendar"/>

        </LinearLayout>

        <*************************.WeekView
            android:id="@+id/todayView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="true"
            app:eventTextColor="@android:color/white"
            app:textSize="12sp"
            app:hourHeight="30dp"
            app:headerColumnPadding="4dp"
            app:headerColumnTextColor="@color/toolbar_text"
            app:headerRowPadding="6dp"
            app:columnGap="4dp"
            app:noOfVisibleDays="1"
            app:headerRowBackgroundColor="@color/toolbar"
            app:dayBackgroundColor="#05000000"
            app:todayBackgroundColor="@color/white"
            app:headerColumnBackground="#ffffffff"
            app:todayHeaderTextColor="@color/accent"
            app:showNowLine="true"
            app:nowLineColor="@color/colorAccent"
            app:enableScrolling="false"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 你有没有关闭clip children?这是我通常看到类似情况的唯一方式。

标签: android android-recyclerview android-arrayadapter android-custom-view


【解决方案1】:

将此添加到您的自定义视图中:

android:margin_top="?attr/actionBarSize"

这将在操作栏/工具栏的大小处设置一个边缘顶部,这将使滚动固定到正确的大小。

【讨论】:

    【解决方案2】:

    我找到了解决方案。如果您在WeekView 中注释上述格式的代码,它可以正常工作

    canvas.clipRect(left, top, width, heigh, Region.Op.REPLACE);
    

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多