Сделай так чтобы при нажатии на левую иконку срабатывалась функция : package…
- User
Сделай так чтобы при нажатии на левую иконку срабатывалась функция : package com.example.cinema_provider_app.main_Fragments.Home_Fragment
import android.os.Bundle import android.os.Handler import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.PagerSnapHelper import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.SnapHelper import com.example.cinema_provider_app.R import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Adapters.CustomRecycleAdapter import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Adapters.FirstTypeAdapter import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Adapters.SliderAdapter import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Data_Classes.CustomDataClass import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Data_Classes.FirstDataType import com.example.cinema_provider_app.main_Fragments.Home_Fragment.Data_Classes.SlideItem
class HomeFragment : Fragment() {
private val scrollHandler = Handler() private lateinit var scrollRunnable: Runnable private lateinit var sliderRecyclerView: RecyclerView private lateinit var firstTypeRecyclerView: RecyclerView private lateinit var secondTypeRecyclerView: RecyclerView private lateinit var customRecyclerView: RecyclerView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_home, container, false) return view } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //FIRST SLIDER ON THE TOP //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sliderRecyclerView = view.findViewById(R.id.sliderRecyclerView) sliderRecyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) val snapHelper: SnapHelper = PagerSnapHelper() snapHelper.attachToRecyclerView(sliderRecyclerView) val slideItems = listOf( SlideItem(" Slide 1", "Description 1", R.drawable._54282_admin), SlideItem(" Slide 2", "Description 2", R.drawable._54282_admin), SlideItem(" Slide 3", "Description 3", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 4", "Description 4", R.drawable._54282_admin), SlideItem(" Slide 5", "Description 5", R.drawable._54282_admin) ) val sliderAdapter = SliderAdapter(slideItems) sliderRecyclerView.adapter = sliderAdapter scrollRunnable = Runnable { val layoutManager = sliderRecyclerView.layoutManager as LinearLayoutManager val currentPosition = layoutManager.findFirstVisibleItemPosition() val nextPosition = if (currentPosition < sliderAdapter.itemCount - 1) currentPosition + 1 else 0 sliderRecyclerView.smoothScrollToPosition(nextPosition) scrollHandler.postDelayed(scrollRunnable, AUTO_SCROLL_DELAY) } scrollHandler.postDelayed(scrollRunnable, AUTO_SCROLL_DELAY) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //SECOND TYPE SLIDER's //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// firstTypeRecyclerView = view.findViewById(R.id.first_type_recycleView) val secondSlideItem = listOf<FirstDataType>( FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin) ) firstTypeRecyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) val firstTypeAdapter = FirstTypeAdapter(secondSlideItem) firstTypeRecyclerView.adapter = firstTypeAdapter val snappHelper: SnapHelper = LinearSnapHelper() snappHelper.attachToRecyclerView(firstTypeRecyclerView) secondTypeRecyclerView = view.findViewById(R.id.first_type_recycleView_2) val secondTypeSlideItem = listOf<FirstDataType>( FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin), FirstDataType(R.drawable._54282_admin) ) secondTypeRecyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) val secondTypeAdapter = FirstTypeAdapter(secondTypeSlideItem) secondTypeRecyclerView.adapter = secondTypeAdapter val snappHelperr: SnapHelper = LinearSnapHelper() snappHelperr.attachToRecyclerView(secondTypeRecyclerView) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //CUSTOM TYPE SLIDER's //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// customRecyclerView = view.findViewById(R.id.CustomRecycleView) customRecyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) val customList = listOf( CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game) ) val customAdapter = CustomRecycleAdapter(customList) customRecyclerView.adapter = customAdapter val customSnapHelper : SnapHelper = PagerSnapHelper() customSnapHelper.attachToRecyclerView(customRecyclerView) } override fun onDestroy() { scrollHandler.removeCallbacks(scrollRunnable) super.onDestroy() } fun placeHolder(){ } companion object { private const val AUTO_SCROLL_DELAY = 10000L // Задержка в миллисекундах (4 секунды) @JvmStatic fun newInstance() = HomeFragment() } - Assistant
To make the left icon clickable and trigger a function, you need to add a click listener to the icon in the
onViewCreatedmethod of theHomeFragmentclass.First, add an
idattribute to the left iconImageViewin the XML layout file:<!-- Первая иконка --> <ImageView android:id="@+id/icon1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.2" android:src="@drawable/icon1" />Then, in the
onViewCreatedmethod, find the iconImageViewusing itsid, and set a click listener to it:override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // ... val icon1 = view.findViewById<ImageView>(R.id.icon1) icon1.setOnClickListener { // Call your function here placeHolder() } // ... }Now, when the left icon is clicked, the
placeHolder()function will be called. Adjust the function implementation as needed. - User
FATAL EXCEPTION: main Process: com.example.cinema_provider_app, PID: 10515 java.lang.NullPointerException: view.findViewById(R.id.icon1) must not be null at com.example.cinema_provider_app.main_Fragments.Home_Fragment.HomeFragment.onViewCreated(HomeFragment.kt:159) at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
- Assistant
The
NullPointerExceptionoccurs because theicon1ImageView with the idR.id.icon1is not found in the XML layout.Make sure that you have added the
idattribute to the left icon ImageView in the XML layout file like this:<!-- Первая иконка --> <ImageView android:id="@+id/icon1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.2" android:src="@drawable/icon1" />If the
icon1ImageView is still not found, please check that you are using the correct layout file (the one that contains the ImageView with the idicon1) in yourHomeFragmentclass.If you have multiple layout files for different configurations (e.g., landscape and portrait), make sure that you have added the
icon1ImageView in the correct layout file that is being used when the Fragment is created. - User
Может я где то ошибся ? customRecyclerView = view.findViewById(R.id.CustomRecycleView) customRecyclerView.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) val customList = listOf( CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game), CustomDataClass(R.drawable.bg_game)
) val customAdapter = CustomRecycleAdapter(customList) customRecyclerView.adapter = customAdapter val customSnapHelper: SnapHelper = PagerSnapHelper() customSnapHelper.attachToRecyclerView(customRecyclerView) val icon1: ImageView = view.findViewById(R.id.icon1) icon1.setOnClickListener { // Call your function here placeHolder() } } - Assistant
Based on your updated code, it seems like you have correctly added the
idattribute to the left iconImageViewin the XML layout file.However, ensure that you are inflating the correct XML layout file in the
onCreateViewmethod of yourHomeFragment. Make sure that the layout file you are inflating contains theicon1ImageView.For example, if the layout file is named
fragment_home.xml, the code in theonCreateViewmethod should be as follows:override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_home, container, false) // Find the icon1 ImageView val icon1: ImageView = view.findViewById(R.id.icon1) icon1.setOnClickListener { // Call your function here placeHolder() } // Rest of your code... return view }Ensure that
R.layout.fragment_homecorresponds to the layout file that contains theicon1ImageView.If the issue persists, please double-check that the ImageView exists in the XML layout file and that the attribute is spelled correctly.