我有一个回收器视图,我需要使用颜色选择器之一(具体取决于数据绑定中的值)来更改 TextView 颜色。
我有两个选择器:
颜色/selector_item_text.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/white" android:state_pressed="true" />
<item android:color="@color/white" android:state_activated="true" />
<item android:color="@color/black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
颜色/selector_item_textwithspecial.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/pink" android:state_checked="true" />
<item android:color="@color/pink" android:state_pressed="true" />
<item android:color="@color/pink" android:state_activated="true" />
<item android:color="@color/orange" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我将它绑定到我的 TextView 上,如下所示:
<TextView android:text="@{data.displayPrice}"
android:textColor="@{data.isSpecial ? @color/selector_item_textwithspecial : @color/selector_item_text}"
style="@style/ProductPrice"/>
Run Code Online (Sandbox Code Playgroud)
问题是 TextView 颜色始终是橙色(如果有特殊)或黑色。选择永远不会改变颜色。但是,如果我删除数据绑定,它就会按预期工作。
例如,下面的代码将使 TextView 变成粉色(选中时)和橙色(未选中时)
<TextView android:text="@{data.displayPrice}"
android:textColor="@color/selector_item_textwithspecial"
style="@style/ProductPrice"/>
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
谢谢...
android textcolor android-selector android-databinding android-text-color
我目前正在尝试使用数据绑定来设置“切换”按钮的检查状态,但由于某种原因我的状态永远不会改变?
在我的设置页面中,我执行以下操作:
文件- SettingsFragment - SettingsViewModel - SettingsLayout.XML
设置视图模型
private val _checked = MutableLiveData<Boolean>()
val checked : LiveData<Boolean> = _checked
Run Code Online (Sandbox Code Playgroud)
设置布局.XML
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="@dimen/margin_big
android:checked="@{viewModel.checked}"/>
Run Code Online (Sandbox Code Playgroud)
我已经仔细检查以确保在我的SettingsFragment中我也执行了DataBindingUtil.inflate并传递了我的 SettingsViewModel。
我在我的 android 项目中使用 Observer 和 viewLifecycleOwner 时遇到错误。它抛出“未解析的引用:viewLifecycleOwner”错误,我不知道为什么。我确认我有进口
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
Run Code Online (Sandbox Code Playgroud)
以及依赖关系:
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
Run Code Online (Sandbox Code Playgroud)
我尝试过使缓存无效/重新启动...在 GitHub 中找到的解决方案。但错误仍然存在。感谢任何可以帮助我的人。
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: DrinkViewModel
// Contains all the views
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
// Use Data Binding to get reference to the views
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.drinkButton.setOnClickListener {
onDrinkClicked()
}
viewModel.revenue.observe(viewLifecycleOwner, Observer { newRevenue ->
binding.revenueText.text = newRevenue.toString()
})
viewModel.drinksSold.observe(viewLifecycleOwner, Observer { newAmount -> …Run Code Online (Sandbox Code Playgroud) 我正在 DataBindings 中尝试 StateFlow,在我能找到的所有示例中,大多数看起来都像这个的副本,ViewModel 中使用两个变量来进行一个数据绑定。一个私有 MutableStateFlow 和一个公共 StateFlow。StateFlow 几乎只是从 MutableStateFlow 读取值。为什么是这样?仅使用一个变量(MutableStateFlow)并跳过 StateFlow 变量不是更容易吗?
我试图在包含BottomNavigationView三个片段的应用程序中实现启动屏幕,并且我使用了最著名的方法,例如在不创建新活动或片段的情况下执行此操作的答案,但在直接启动启动屏幕后出现问题,它得到“RuntimeException”和空指针异常”
E/AndroidRuntime: FATAL EXCEPTION: main\n Process: com.mml.foody, PID: 6868\n java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mml.foody/com.mml.foody.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method \'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)\' on a null object reference\n at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)\n at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)\n at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)\n at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)\n at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)\n at android.os.Handler.dispatchMessage(Handler.java:106)\n at android.os.Looper.loop(Looper.java:223)\n at android.app.ActivityThread.main(ActivityThread.java:7656)\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)\n Caused by: java.lang.NullPointerException: Attempt to invoke virtual method \'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)\' on a null object reference\n at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)\n at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)\n at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)\n …Run Code Online (Sandbox Code Playgroud) android splash-screen android-theme kotlin android-databinding
嗨,我尝试用数据绑定更新imageview,但我想知道如何正确地做到这一点.
我的变种
@BindingAdapter({"bind:someImage"})
public void loadIt(ImageView view, String imageUrl) {
Picasso.with(view.getContext())
.load(getSomeImage())
.placeholder(android.R.drawable.alert_dark_frame)
.into(view);
}
public void setSomeImage(){
notifyPropertyChanged(BR.someImage);
}
Run Code Online (Sandbox Code Playgroud)
我的布局
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagePath"
app:imageUrl="@{var.someImage}"/>
Run Code Online (Sandbox Code Playgroud)
我试图在代码中实现数据绑定功能.在build.gradle中我添加了databinding.enabled = true但是它给出错误无法解析符号启用.如何纠正?
我正在尝试对应该显示通过ViewModel中的LiveData属性公开的数据的视图进行数据绑定,但是我没有找到将LiveData中的对象绑定到视图的方法。从XML中,我只能访问LiveData实例的value属性,而不能访问其中的对象。我错过了什么吗?
我的ViewModel:
class TaskViewModel @Inject
internal constructor(private val taskInteractor: taskInteractor)
: ViewModel(), TaskContract.ViewModel {
override val selected = MutableLiveData<Task>()
val task: LiveData<Task> = Transformations.switchMap(
selected
) { item ->
taskInteractor
.getTaskLiveData(item.task.UID)
}
... left out for breivety ...
}
Run Code Online (Sandbox Code Playgroud)
我试图将任务对象的值绑定到我的视图中,但是当尝试在我的视图中设置任务的值时,我只能这样做android:text="@={viewmodel.task.value}"。我无权访问任务对象的字段。在LiveData对象中提取对象值的诀窍是什么?
我的任务课:
@Entity(tableName = "tasks")
data class Task(val id: String,
val title: String,
val description: String?,
created: Date,
updated: Date,
assigned: String?)
Run Code Online (Sandbox Code Playgroud) android android-databinding android-livedata android-viewmodel
首先,我需要承认这里非常相似但并非重复的问题。在该线程中提出的解决方案均无效。
我的应用程序文件结构如下:
app
java
[mydomain].[myapplication]
Models
DataModel.java
MainActivity.java
res
layout
activity_main.xml
content_main.xml
my_inner_layout.xml
Run Code Online (Sandbox Code Playgroud)
我的应用build.gradle包含
dataBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
在MainActivity.java我有
import [mydomain].[myapplication].Models.DataModel;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemsSelectedListener {
DataModel dataModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
... <other layout creation template code> ...
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
dataModel = new DataModel();
binding.setValues(dataModel);
}
<navigation and auto-generated methods>
}
Run Code Online (Sandbox Code Playgroud)
我的my_inner_layout.xml包含
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> …Run Code Online (Sandbox Code Playgroud) 我怎么解决这个问题。我无法使此数据绑定有效,我已经尝试了所有方法。Build.gradle(模块应用)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.fusion.alen.ask"
minSdkVersion 16
targetSdkVersion 27
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
androidExtensions {
experimental = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:animated-vector-drawable:27.1.1'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'com.android.support:customtabs:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1' …Run Code Online (Sandbox Code Playgroud)