我想在对通过数据绑定绑定到布局的实体进行更改后以编程方式更新工具栏的标题。
我的布局摘录(数据绑定在最初绑定时工作正常):
androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@{entity.title}"/>
Run Code Online (Sandbox Code Playgroud)
我的活动onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
binding = DataBindingUtil.setContentView(this, R.layout.my_layout);
setSupportActionBar(binding.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
MyEntity entity = (MyEntity) getIntent().getSerializableExtra(MyEntity.class.getName());
binding.setEntity(entity);
}
Run Code Online (Sandbox Code Playgroud)
编辑实体时,我想更新 UI,我在活动中尝试了以下方式:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
binding.setEntity(editedEntity); // this seems not to be sufficient and getSupportActionBar().getTitle() still returns the old value
binding.toolbar.setTitle(editedEntity.getTitle()); // getSupportActionBar().getTitle() returns the updated value but the UI does not show it
setSupportActionBar(binding.toolbar); // does not help …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是我的片段的 id 和标签未设置,尽管我在我的中指定了它们mobile_navigation.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/mobile_navigation"
app:startDestination="@+id/nav_frag1">
<fragment
android:id="@+id/nav_frag1"
android:tag="abcdef"
android:name="org.example.myproject.MyFragment"
android:label="@string/menu_frag1"
tools:layout="@layout/fragment_frag1" />
...
</navigation>
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的片段中通过编程方式访问 id 和标记时,this.getId()或者所有片段的this.getTag()id 始终是相同的非常高的整数(比 max int 稍小)并且标记为null。
导航是放在我的主要活动中的。该代码基本上是Android Studio 创建的示例抽屉导航项目。我没有改变任何事情。
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.frag1, R.id.frag2, …Run Code Online (Sandbox Code Playgroud) 我正在使用ViewPager2并希望检测用户何时在第一页上向左滑动或在最后一页上向右滑动。
我知道这可以使用此处ViewPager讨论的旧方法来实现,但这种方法不可行,因为它是最终的并且不能子类化。ViewPager2
ViewPager2提供了OnPageChangeCallback但这也不能使用,因为滑动越界时没有页面事件。
我错过了什么吗?