首页 > 解决方案 > 滚动查看可点击

问题描述

在此处输入图像描述

<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/myToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:title="Doctor Appointment"
                app:titleTextColor="#fff"
                tools:ignore="MissingConstraints" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="4dp"
                android:background="@color/blue"
                android:drawableLeft="@drawable/ic_action_logout"
                android:text="Logout"
                android:textColor="@color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@+id/myToolbar"
                tools:ignore="MissingConstraints" />


        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.appbar.AppBarLayout>


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="SpeakableTextPresentCheck,SpeakableTextPresentCheck" />
    </androidx.viewpager.widget.ViewPager>
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="411dp"
        android:layout_height="712dp"
        tools:ignore="MissingConstraints">


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

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/button"
                    android:layout_width="140dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="70dp"
                    android:background="@drawable/my_border"
                    android:text="save"
                    android:textColor="@color/blue"
                    tools:ignore="MissingConstraints" />


                <Button
                    android:id="@+id/button2"
                    android:layout_width="140dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="230dp"
                    android:layout_marginTop="70dp"
                    android:background="@drawable/my_border"
                    android:text="close"
                    android:textColor="@color/blue" />
            </RelativeLayout>

            <RadioGroup
                android:id="@+id/G1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="10dp">

                <RadioButton
                    android:id="@+id/R1"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:text="By Speciality" />

                <RadioButton
                    android:id="@+id/R2"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:text="By Doctor"/>
            </RadioGroup>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <ImageView
                    android:id="@+id/img2"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="25dp"
                    android:layout_marginEnd="25dp"
                    android:layout_marginBottom="25dp"
                    android:src="@drawable/female_doctor" />

                <TextView
                        android:id="@+id/Name1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="30dp"
                        android:layout_toEndOf="@+id/img2"
                        android:text="Dr. J. Anitha Ponmalar"
                        android:textColor="#000000"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/degree1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/Name1"
                        android:layout_marginTop="15dp"
                        android:layout_toEndOf="@+id/img2"
                        android:text="M.B.B.S., DCH, MD physiology"
                        android:textColor="#000000"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/branch1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/degree1"
                        android:layout_marginTop="15dp"
                        android:layout_toEndOf="@+id/img2"
                        android:text="Dermatology"
                        android:textColor="#000000"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/time1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="190dp"
                        android:layout_marginTop="99dp"
                        android:layout_toEndOf="@+id/img2"
                        android:text="Fee : 309" />

            </RelativeLayout>
        </LinearLayout>

    </ScrollView>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

如何实现可单击的滚动视图,在单击滚动视图时我需要导航到另一个活动。这里我附上了图像,我在滚动视图中使用了图像和文本。在 java 文件中我使用了单选按钮单击。你能帮我用java代码使滚动视图可点击?我尝试使用单击侦听器将滚动视图实现为可单击,但它不起作用。

标签: androidclick

解决方案


在线性布局中添加一个 id,如下面的代码所示:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/myToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="?attr/actionBarTheme"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:title="Doctor Appointment"
                    app:titleTextColor="#fff"
                    tools:ignore="MissingConstraints" />
    
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="4dp"
                    android:background="@color/blue"
                    android:drawableLeft="@drawable/ic_action_logout"
                    android:text="Logout"
                    android:textColor="@color/white"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="@+id/myToolbar"
                    tools:ignore="MissingConstraints" />
    
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
    
        </com.google.android.material.appbar.AppBarLayout>
    
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:ignore="SpeakableTextPresentCheck,SpeakableTextPresentCheck" />
        </androidx.viewpager.widget.ViewPager>
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="411dp"
            android:layout_height="712dp"
            tools:ignore="MissingConstraints">
    
    
            <LinearLayout
                android:id="@+id/llMyScrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                tools:ignore="MissingConstraints">
    
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <Button
                        android:id="@+id/button"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="60dp"
                        android:layout_marginTop="70dp"
                        android:background="@drawable/my_border"
                        android:text="save"
                        android:textColor="@color/blue"
                        tools:ignore="MissingConstraints" />
    
    
                    <Button
                        android:id="@+id/button2"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="230dp"
                        android:layout_marginTop="70dp"
                        android:background="@drawable/my_border"
                        android:text="close"
                        android:textColor="@color/blue" />
                </RelativeLayout>
    
                <RadioGroup
                    android:id="@+id/G1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingTop="10dp">
    
                    <RadioButton
                        android:id="@+id/R1"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:text="By Speciality" />
    
                    <RadioButton
                        android:id="@+id/R2"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:text="By Doctor"/>
                </RadioGroup>
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
    
                    <ImageView
                        android:id="@+id/img2"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="25dp"
                        android:layout_marginEnd="25dp"
                        android:layout_marginBottom="25dp"
                        android:src="@drawable/female_doctor" />
    
                    <TextView
                            android:id="@+id/Name1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="30dp"
                            android:layout_toEndOf="@+id/img2"
                            android:text="Dr. J. Anitha Ponmalar"
                            android:textColor="#000000"
                            android:textSize="14sp"
                            android:textStyle="bold" />
    
                        <TextView
                            android:id="@+id/degree1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/Name1"
                            android:layout_marginTop="15dp"
                            android:layout_toEndOf="@+id/img2"
                            android:text="M.B.B.S., DCH, MD physiology"
                            android:textColor="#000000"
                            android:textSize="14sp" />
    
                        <TextView
                            android:id="@+id/branch1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/degree1"
                            android:layout_marginTop="15dp"
                            android:layout_toEndOf="@+id/img2"
                            android:text="Dermatology"
                            android:textColor="#000000"
                            android:textSize="14sp" />
    
                        <TextView
                            android:id="@+id/time1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="190dp"
                            android:layout_marginTop="99dp"
                            android:layout_toEndOf="@+id/img2"
                            android:text="Fee : 309" />
    
                </RelativeLayout>
            </LinearLayout>
    
        </ScrollView>
    </RelativeLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

并将您的 onclick 侦听器更改为:

LinearLayout llayout = findViewById(R.id.llMyScrollView); 
llayout.setOnClickListener(new View.OnClickListener() { 
@Override public void onClick(View v) { 
  Intent intent = new Intent(Dermatology.this,Spec_Derm.class); 
  startActivity(intent); 
} 
}); 

推荐阅读