powered by simpleCommunicator - 2.0.49     © 2025 Programmizd 02
Форумы / Android [игнор отключен] [закрыт для гостей] / BottomNavigationView и фрагменты
3 сообщений из 3, страница 1 из 1
BottomNavigationView и фрагменты
    #39513969
mishanches
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый день, коллеги.
Никто не поможет мне разобраться в небольшом вопросе взаимодействия фрагмента и BottomNavigationView? Дело в том, что использую стандартную Bottom Navigation Activity (с 3-мя кнопками внизу экрана). По умолчанию – при нажатии на кнопки изменяется текст. Я хочу, чтобы при нажатии на кнопку – появлялся фрагмент. Но, когда написал код, то некорректно отображается эта панель с кнопками: эта панель оказывается вверху, а не снизу, как по умолчанию.

Как сделать так, чтобы при появлении фрагмента кнопки панели оставались там же?
Вот главный код:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
Class fragmentClass = null;
int id = item.getItemId();
if (id == R.id.navigation_home) {
fragmentClass = BiografyClass.class;
} else if (id == R.id.navigation_dashboard) {
fragmentClass = PhotoFragment.class;
} else if (id == R.id.navigation_notifications) {
fragmentClass = PhotoFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

setTitle(item.getTitle());
return true;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}


Activity_main:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.butam.biografy4.MainActivity">


<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<fragment
android:id="@+id/fragment"
android:name="com.example.butam.biografy4.BiografyClass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>

</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />

</LinearLayout>


Fragment_biografy.xml:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.butam.biografy4.MainActivity">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="Тут Текст 1" />

</LinearLayout>


</LinearLayout>


BiografyClass.java:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class BiografyClass extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView =
inflater.inflate(R.layout.fragment_biografy, container, false);
return rootView;
}
}


Если переходить на другую активность с помощью интента или просто выводить текст, то все корректно отображается: панель с 3-мя кнопками снизу. С фрагментами никак пока не разберусь.

Модератор: Просьба пользоваться тэгами src и spoiler для оформления сообщений в читаемом виде.
Желательно и с сохранением форматирования... Читать-же не удобно.
...
Рейтинг: 0 / 0
BottomNavigationView и фрагменты
    #39514322
mishanches
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Все, нашел ответ
...
Рейтинг: 0 / 0
BottomNavigationView и фрагменты
    #39514371
Фотография wadman
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
mishanches,

поделиться решением - знак хорошего тона. :-)
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Android [игнор отключен] [закрыт для гостей] / BottomNavigationView и фрагменты
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]