我如何为 Android 实施这个特定的 "AnimatedBottomNavigationBar"?方法错误
How can I implement this particular "AnimatedBottomNavigationBar" for Android? Errors with methods
我从这个库中实现了 BottomBar https://github.com/Droppers/AnimatedBottomBar
我创建了 XML 选项卡
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/tab_home"
android:icon="@drawable/ic_action_home"
android:title="@string/Home" />
<item
android:id="@+id/tab_publish"
android:icon="@drawable/ic_action_publish"
android:title="@string/Publish" />
<item
android:id="@+id/tab_map"
android:icon="@drawable/ic_action_map"
android:title="@string/Map" />
<item
android:id="@+id/tab_profile"
android:icon="@drawable/ic_action_profile"
android:title="@string/Profile" />
</menu>
并把它放在我的 activity 的 XML 中作为
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content"
>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_bar"></androidx.fragment.app.FragmentContainerView>
<nl.joery.animatedbottombar.AnimatedBottomBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="@color/button_black"
app:abb_indicatorColor="@color/instagramPink"
app:abb_indicatorAppearance="round"
app:abb_indicatorHeight="4dp"
app:abb_indicatorMargin="16dp"
app:abb_selectedIndex="1"
app:abb_selectedTabType="text"
app:abb_tabs="@menu/tabs"
app:abb_badgeTextColor="@color/instagramPink"
app:abb_tabColor="@color/instagramPink"
app:abb_tabColorSelected="@color/white"
/>
</RelativeLayout>
那么我的 activity 的代码是
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
switch (item.getItemId()) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
binding.bottomBar.setSelectedItemId(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}
但是,那两条注释行给我一个错误,说这些方法不存在。这就是我通常使用导航栏的方式,但尽管浏览了文档,但我似乎无法找到为这个特定导航栏设置它的方法。请帮忙!
谢谢
试试这个:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
//REPLACEMENT
switch (item.id) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
//REPLACEMENT
binding.bottomBar.selectTabById(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}
我从这个库中实现了 BottomBar https://github.com/Droppers/AnimatedBottomBar
我创建了 XML 选项卡
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/tab_home"
android:icon="@drawable/ic_action_home"
android:title="@string/Home" />
<item
android:id="@+id/tab_publish"
android:icon="@drawable/ic_action_publish"
android:title="@string/Publish" />
<item
android:id="@+id/tab_map"
android:icon="@drawable/ic_action_map"
android:title="@string/Map" />
<item
android:id="@+id/tab_profile"
android:icon="@drawable/ic_action_profile"
android:title="@string/Profile" />
</menu>
并把它放在我的 activity 的 XML 中作为
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content"
>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_bar"></androidx.fragment.app.FragmentContainerView>
<nl.joery.animatedbottombar.AnimatedBottomBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="@color/button_black"
app:abb_indicatorColor="@color/instagramPink"
app:abb_indicatorAppearance="round"
app:abb_indicatorHeight="4dp"
app:abb_indicatorMargin="16dp"
app:abb_selectedIndex="1"
app:abb_selectedTabType="text"
app:abb_tabs="@menu/tabs"
app:abb_badgeTextColor="@color/instagramPink"
app:abb_tabColor="@color/instagramPink"
app:abb_tabColorSelected="@color/white"
/>
</RelativeLayout>
那么我的 activity 的代码是
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
switch (item.getItemId()) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
binding.bottomBar.setSelectedItemId(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}
但是,那两条注释行给我一个错误,说这些方法不存在。这就是我通常使用导航栏的方式,但尽管浏览了文档,但我似乎无法找到为这个特定导航栏设置它的方法。请帮忙!
谢谢
试试这个:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment1 = new HomeFeed();
final Fragment fragment2 = new CreateEvent();
final MapsActivity fragment3 = new MapsActivity();
final Fragment fragment4 = new ProfileActivity();
binding.bottomBar.setOnTabSelectListener(item -> {
Fragment fragment;
//REPLACEMENT
switch (item.id) { //Error saying there is such no method as "getItemId"
case R.id.tab_home:
fragment = fragment1;
break;
case R.id.tab_publish:
fragment = fragment2;
break;
case R.id.tab_map:
fragment = fragment3;
break;
case R.id.tab_profile:
default:
Fragment = fragment4;
break;
}
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
return true;
});
//REPLACEMENT
binding.bottomBar.selectTabById(R.id.tab_home); //Error saying there is such no method as "setSelectedItemId"
}
}