简体   繁体   English

如何禁用 CalendarView 的滚动 - Android Dev

[英]How to disable scrolling for CalendarView - Android Dev

I am currently refreshing my android developement skills and came across the CalendarView component.我目前正在刷新我的 android 开发技能并遇到了CalendarView组件。 I wanted to disable the scrolling of the calendar and instead enable the arrows to browse through the calendar.我想禁用日历的滚动,而是启用箭头来浏览日历。

What I want to know is the following: How can I disable the scrolling from the CalendarView?我想知道的是:如何禁用 CalendarView 的滚动? And why does Android Studio show me the arrow browse-version in the Designer-Mode but compiles and runs the scroll-version of the calendar?为什么 Android Studio 在设计器模式中显示箭头浏览版本,但编译并运行日历的滚动版本?

I am running an Android VM, a "Galaxy Nexus" with API 22. I also posted the "build.gradle" further below.我正在运行一个 Android VM,一个带有 API 22 的“Galaxy Nexus”。我还在下面进一步发布了“build.gradle”。

What Android shows me/What I want: Android 向我展示的/我想要的:

在此处输入图像描述

What Android Studio compiles & runs: Android Studio 编译和运行的内容:

在此处输入图像描述


Here some code:这里有一些代码:

// activity_main.java: // activity_main.java:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="0dp">

        <CalendarView
            android:id="@+id/calendarView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="false" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

// build.gradle // build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.kalender003_emptyactivity"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Use MaterialComponents Theme, it will most probably solve the issue.使用 MaterialComponents 主题,它很可能会解决问题。

In your values/themes file replace the parent them with this,在你的价值观/主题文件中用这个替换父级,

parent="Theme.MaterialComponents.DayNight.DarkActionBar"

If you don't want Action Bar then,如果你不想要 Action Bar,

parent="Theme.MaterialComponents.DayNight.NoActionBar"

And if you only want to show Light Coloured Calendar view regardless of Devices's theme use,如果您只想显示浅色日历视图而不考虑设备的主题使用,

parent="Theme.MaterialComponents.Light.NoActionBar"

Thanks to Vaibhav Goyal i came across the solution.感谢Vaibhav Goyal ,我找到了解决方案。

The API level I used was to low (API 22, Android 5.1).我使用的API 级别太低(API 22、Android 5.1)。 Changing that to API 23 (Android 6.0) solved my problem.将其更改为API 23 (Android 6.0) 解决了我的问题。 Now it shows me the CalendarView with arrow-navigation instead of the scroll version!现在它向我展示了带有箭头导航而不是滚动版本的 CalendarView!

yourCalenenderId.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
            return if (e.action == MotionEvent.ACTION_MOVE) {
                true
            } else {
                super.onInterceptTouchEvent(rv, e)
            }
        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM