0

Помогите, пожалуйста. У меня есть список ListView, нужно чтобы при нажатии на отдельный его элемент текст из этого элемента показывался во фрагменте. И что нужно указать в слушателе кнопки close во фрагменте, чтобы фрагмент закрылся без сохранения переданных ему данных? onStop()?


class MainActivity : AppCompatActivity() { 
 lateinit var mainBindingClass: ActivityMainBinding 
 var list = ArrayList<String>() 
 
 
 override fun onCreate(savedInstanceState: Bundle?) { 
 super.onCreate(savedInstanceState) 
 mainBindingClass = ActivityMainBinding.inflate(layoutInflater) 
 setContentView(mainBindingClass.root) 
 list.add("Оля") 
 list.add("Вася") 
 list.add("Юля") 
 } 
 
 
 private fun initListeners() { 
 mainBindingClass.lvListOfNotes.setOnItemClickListener { parent, view, position, id -> 
 val detailFragment = DetailFragment() 
 val manager = supportFragmentManager 
 val transaction = manager.beginTransaction() 
 transaction.replace(R.id.fl_container, detailFragment) 
 transaction.addToBackStack(null) 
 transaction.commit() 
 var note : String 
// не знаю как взять для note взять текст из отдельного элемента списка и передать в фрагмент
 } 
 
 }



class DetailFragment : Fragment() { 
 lateinit var detailBindingClass: FragmentDetailBinding 
 
 override fun onCreate(savedInstanceState: Bundle?) { 
 super.onCreate(savedInstanceState) 
 detailBindingClass = FragmentDetailBinding.inflate(layoutInflater) 
 setContentView(detailBindingClass.root) 
 
 } 
 
 
 override fun onCreateView( 
 inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? 
 ): View? { 
 val view = inflater!!.inflate(R.layout.fragment_detail, container, false) 
 val tv = view.findViewById<TextView>(R.id.tvNote) 
 return view 
 
 
 var note = // здесь должно приниматься значение из MainActivity
 } 
 
 private fun initListeners() { 
 detailBindingClass.bClose.setOnClickListener { 
 onStop(); 
 } 
 
 }
override fun onPause() { 
 super.onPause() 
 } 
 
 
 override fun onAttach(context: Context) { 
 super.onAttach(context) 
 } 
 
 override fun onDetach() { 
 super.onDetach() 
 } 
 
 override fun onDestroy() { 
 super.onDestroy() 
 } 
 
 override fun onStart() { 
 super.onStart() 
 } 
 
 override fun onStop() { 
 super.onStop() 
 } 
}

1 ответ 1

0

надеюсь проблем перевести код из java в kotlin не возникнет

внутри DetailFragment() добавьте метод

public static DetailFragment newInstance(String s) {
    DetailFragment fragment = new FragmentSettingsNavigationRemote();
    Bundle args = new Bundle();
    args.putString("param", s);
    fragment.setArguments(args);
    return fragment;
}

что бы достать определенную строку из элемента при нажатии

list.get(position)

таким образом нужно объявить фрагмент вот так

val detailFragment = DetailFragment.newInstance(list.get(position))

а внутри самого фрагмента вы можете достать строку по "param"

getArguments().getString(ActivitySettings.PARAM_BREADCRUMB)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.