Из фрагмента я начать activty, который открывает новый фрагмент. Кроме того, я передать значение строки из первого фрагмента ( AllDishes
) за деятельность до последнего фрагмента ( DishDetailView
) через намерение. Фрагмент ( DishDetailView
) устанавливается в качестве контекста для макета ( fragment_dishdetailview
), но когда я пытаюсь установить текст , который был принят, вид не получает обновления.
Когда я отладить мое приложение, параметр строки ( "Test"
) , кажется, прошел , а также текст из , TextView
name
кажется, настроен правильно, как вы можете видеть на скриншоте ниже, но как - то UI ( fragment_dishdetailview
) не получает обновления, подмигнули просто пустой.
I'm не уверен , что обновление пользовательского интерфейса или способ передачи значения из одного фрагмента к другому , является ли проблема. Моя цель состоит в том, чтобы открыть фрагмент ( DishDetailView
) из фрагмента ( AllDishes
) и передать строку от первого до второго фрагмента. Поэтому на самом деле Я не нужна активность , но не я не смог передать строку.
AllDishes (фрагмент)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dish dish = ca.getItem(position);
Intent intent = new Intent(rootView.getContext(), DishDetailsViewActivity.class);
intent.putExtra("nameToPass" , dish.name);
startActivity(intent);
}
DishDetailView (фрагмент)
public class DishDetailView extends Fragment {
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View rootView = inflater.inflate(R.layout.fragment_dishdetailview, container, false);
String tempHolder = getActivity().getIntent().getStringExtra("nameToPass");
TextView name = rootView.findViewById(R.id.textview_view_dishName);
name.setText(tempHolder);
rootView.invalidate();
return inflater.inflate(R.layout.fragment_dishdetailview, container, false);
}
}
DishDetailsViewActivity (активность)
public class DishDetailsViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dish_details_view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
fragment_dishdetailview.xml (фрагмент макета)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@color/screen_background"
tools:context=".fragments.DishDetailView">
<TextView
android:id="@+id/textview_view_dishName"
android:layout_width="300dp"
android:layout_height="27dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="20dp"
android:textColor="@color/headercolor"
android:textSize="@dimen/header_fontsize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
** content_dish_details_view.xml (раскладка деятельность) **
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph3" />
</androidx.constraintlayout.widget.ConstraintLayout>
нав-graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph3"
app:startDestination="@id/fragment_dishdetailview">
<fragment
android:id="@+id/fragment_dishdetailview"
android:name="com.example.nutritracker.fragments.DishDetailView"
android:label="DishDetailView"
tools:layout="@layout/fragment_dishdetailview">
</fragment>
</navigation>
Я думаю , вы должны вернуть ваши rootView
на onCreateView
метод DishDetailView
.