메인에서 프래그먼트로 문자를 넘기고싶어요
조회수 1057회
코딩 초보입니다. 탭레이아웃을 adpter를 사용해 만들었는데요 버튼 onclick을 하면 Edittext에 있는 문자를 각각 탭 페이지에 넘겨 주고 싶은데 방법을 모르겠어요
제가 나름 방법을 찾아서 시도를 해봤는데
1.bundle 을 이용해서 argument 를 사용하면 텍스트가 넘어가질 않고요
2.public Fragment findFragmentByPosition(int position) {
return getSupportFragmentManager().findFragmentByTag(
"android:switcher:" + viewPager.getId() + ":"
+ ((FragmentAdapter) viewPager.getAdapter()).getItemId(position));
} 부분을 사용해봤는데 (제대로 쓴건지도 모르겠네요)텍스트를 쓰고 버튼을 누르면 앱이 꺼져요
계속구글링을 해서 찾아봐도 모르겠네요 저것만 2주째 붙들고 있어요 제발 답변부탁드립니다ㅜㅜ 아그리고 main에서 onclick이 빠져있네요 onclick 있다고 생각해주세요
MAIN
ADAPTER
Fragment
layout
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
bundle
은 보통FragmentTransaction
과정에서 값을 전달하기 위해 사용 됩니다.Fragment 를 찾는 방법 중 대표적인 방법으로는,
findFragmentById()
findFragmentByTag()
- Fragment backStack 으로 직접 index 로 가져오기
- 관리 중인 fragment 를 직접 지역 변수 등으로 관리하기
현재
findFragmentByTag()
를 사용중이신데 올려주신 코드로는 fragment 에 tag 를 추가하신 부분이 보이지 않고 해당 메소드의 파라미터에 잘못 된 값을 넣고 계신걸로 보입니다. 따라서 추측이지만 fragment 를 찾을 수 없다는 에러 로그가 발생하면서 앱이 강제종료 되었던 것 같네요.fragment 에 id 가 할당 되어서
findFragmentById
를 사용하여 Activity <-> Fragment 간 이벤트를 전달하는 간단한 예제를 참고해보시고 응용해보시길 바랍니다 (코틀린으로 작성 되었습니다)MainActivity.kt
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn.setOnClickListener { dispatchEventToFragment() } } private fun dispatchEventToFragment() { val fragment = supportFragmentManager.findFragmentById(R.id.testFragment) as TestFragment fragment.updateValue() } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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"> <android.support.constraint.Guideline android:id="@+id/tGL" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent=".03" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hit Button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="@id/tGL" /> <fragment android:id="@+id/testFragment" android:name="com.pistolcaffe.hashcode.TestFragment" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/btn" /> </android.support.constraint.ConstraintLayout>
TestFragment.kt
class TestFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_test, container, false) } fun updateValue() { value.text = Random().nextInt(1000).toString() } }
fragment_test.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_dark" tools:context=".TestFragment"> <TextView android:id="@+id/value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="22sp" android:textStyle="bold" /> </FrameLayout>
이 방법 외에도 custom listener 를 사용할 수도 있고 방법은 다양하게 있으니 천천히 해보시길 바랍니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력