現在、androidアプリ開発中で、下記のサイトを参考にしながらScrollViewのカスタマイズを行っています。
しかし、実行すると型変換が上手くいかずエラーになってしまいます。
https://qiita.com/solmin719/items/afa5d5c160547ddd7721
https://qiita.com/solmin719/items/a7f519f4f4534fae8aa7
http://buchi.hatenablog.com/entry/2015/06/09/191145
どのように修正したら良いか、分かる方いたら教えていただきたいです。
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 android:id="@+id/linear"> 10 11 <!-- <ScrollView--> 12 <ScrollView 13 android:id="@+id/scrollview" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent"> 16 17 <LinearLayout 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" 20 android:orientation="horizontal" 21 android:weightSum="3"> 22 23 <FrameLayout 24 android:id="@+id/frame1" 25 android:layout_width="0dp" 26 android:layout_height="match_parent" 27 android:layout_weight="1"> 28 29 <ImageView 30 android:id="@+id/imageView1" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:scaleType="fitCenter" /> 34 </FrameLayout> 35 36 <FrameLayout 37 android:id="@+id/frame2" 38 android:layout_width="0dp" 39 android:layout_height="match_parent" 40 android:layout_weight="1"> 41 42 <ImageView 43 android:id="@+id/imageView2" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:scaleType="fitCenter" /> 47 </FrameLayout> 48 49 <FrameLayout 50 android:id="@+id/frame3" 51 android:layout_width="0dp" 52 android:layout_height="match_parent" 53 android:layout_weight="1"> 54 55 <ImageView 56 android:id="@+id/imageView3" 57 android:layout_width="match_parent" 58 android:layout_height="wrap_content" 59 android:scaleType="fitCenter" /> 60 </FrameLayout> 61 </LinearLayout> 62 </ScrollView> 63 64</LinearLayout>
kotlin
1class TopFragment : Fragment() { 2 3 var title_arr = ArrayList<String>() 4 var discription_arr = ArrayList<String>() 5 6 7 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 8 super.onCreateView(inflater, container, savedInstanceState) 9 val v = inflater.inflate(R.layout.fragment_top, container, false) 10 11 val scrollview = v.findViewById<ScrollViewWithListener>(R.id.scrollview) // ←ここでエラー 12 13 val image1 = v.findViewById<ImageView>(R.id.imageView1) 14 image1.setImageResource(R.drawable.image) 15 image1.setLayoutParams(layoutParams) 16 17 val image2 = v.findViewById<ImageView>(R.id.imageView2) 18 image2.setImageResource(R.drawable.image) 19 image2.setLayoutParams(layoutParams) 20 21 val image3 = v.findViewById<ImageView>(R.id.imageView3) 22 image3.setImageResource(R.drawable.image) 23 image3.setLayoutParams(layoutParams) 24 25 26 //スクロールした時の処理 27 scrollview.setOnScrollListener(object : ScrollViewWithListener.OnScrollChangedListener { 28 override fun onScrollChanged(x: Int, y: Int, oldx: Int, oldy: Int) { 29 Log.d(TAG, "oldy -> y : $oldy -> $y") 30 } 31 }) 32 33 return v 34 35 } 36} 37 38//スクロールビューに関するクラス 39class ScrollViewWithListener @JvmOverloads constructor( 40 context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 41) : ScrollView(context, attrs, defStyleAttr) { 42 43 private var mOnScrollChangedListener: OnScrollChangedListener? = null 44 45 interface OnScrollChangedListener { 46 fun onScrollChanged(x: Int, y: Int, oldx: Int, oldy: Int) 47 } 48 49 fun setOnScrollListener(listenerOn: OnScrollChangedListener) { 50 this.mOnScrollChangedListener = listenerOn 51 } 52 53 override fun onScrollChanged(x: Int, y: Int, oldx: Int, oldy: Int) { 54 super.onScrollChanged(x, y, oldx, oldy) 55 mOnScrollChangedListener?.onScrollChanged(x, y, oldx, oldy) 56 } 57}
エラー内容
java.lang.ClassCastException: android.widget.ScrollView cannot be cast to jp.android.ScrollViewWithListener
at jp.android.TopFragment.onCreateView(TopFragment.kt:77) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620) at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1392) at android.app.Activity.performStart(Activity.java:7157) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2943) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:280) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/19 00:42