这是我第一次使用RecyclerView进行数据绑定,但不是我第一次使用RecyclerView本身.
由于某些原因,没有调用任何适配器方法 - 甚至不是getItemCount().我的RecyclerView可能是一个愚蠢的问题,与数据绑定无关,但我看不出有什么不妥.
View rootview = inflater.inflate(R.layout.fragment_profile_first, container, false);
// Initialize recycler view
RecyclerView badgesRV = (RecyclerView) rootview.findViewById(R.id.badgesRV);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
badgesRV.setLayoutManager(llm);
BadgeAdapter badgeAdapter = new BadgeAdapter(profileObject.badgesEntity.badges);
badgesRV.setAdapter(badgeAdapter);
Run Code Online (Sandbox Code Playgroud)
适配器:
public class BadgeAdapter extends RecyclerView.Adapter<BadgeAdapter.BadgeBindingHolder>{
private static final int MAX_BADGES_TO_DISPLAY = 5;
private BadgeObject[] badges;
public BadgeAdapter(BadgeObject[] badges){
this.badges = badges;
}
@Override
public BadgeBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_badge_row, parent, false);
BadgeBindingHolder holder = new BadgeBindingHolder(v);
return holder;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) android android-custom-view android-recyclerview android-databinding
我正在尝试使用自定义视图的数据绑定(George Mount 在此处显示的可能用法).
无法想象在没有<merge>标签的情况下构建复合视图.但是,在这种情况下,数据绑定失败:
MyCompoundView 类:
public class MyCompoundView extends RelativeLayout {
MyCompoundViewBinding binding;
public MyCompoundView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
binding = MyCompoundViewBinding.inflate(inflater, this, true);
}
Run Code Online (Sandbox Code Playgroud)
my_compound_view.xml:app:isGone="@{!data.isViewVisible}"我希望控制整个复合视图的可见性
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data>
<variable name="data" type="com.example.MyViewModel"/>
</data>
<merge
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:isGone="@{!data.isViewVisible}">
<ImageView
android:id="@+id/image_image"
android:layout_width="60dp"
android:layout_height="60dp"
app:imageUrl="@{data.imagePhotoUrl}"/>
<!-- tons of other views-->
</merge>
</layout>
Run Code Online (Sandbox Code Playgroud)
编译器错误:
Error:(13) No resource identifier …Run Code Online (Sandbox Code Playgroud) 我要离开这个 stackoverflow 答案/sf/answers/2437229581/将数据对象绑定到自定义视图。但是,在一切都设置好之后,我的视图没有被数据更新,而是TextView保持空白。这是我的代码(简化,为了适应这里)
activity_profile.xml::
<layout xmlns...>
<data>
<variable name="viewModel" type="com.example.ProfileViewModel"/>
</data>
<com.example.MyProfileCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:viewModel="@{viewModel}">
</layout>
Run Code Online (Sandbox Code Playgroud)
view_profile.xml:
<layout xmlns...>
<data>
<variable name="viewModel" type="com.example.ProfileViewModel"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{viewModel.name}">
</layout>
Run Code Online (Sandbox Code Playgroud)
MyProfileCustomView.kt:
class MyProfileCustomView : FrameLayout {
constructor...
private val binding: ViewProfileBinding = ViewProfileBinding.inflate(LayoutInflater.from(context), this, true)
fun setViewModel(profileViewModel: ProfileViewModel) {
binding.viewModel = profileViewModel
}
}
Run Code Online (Sandbox Code Playgroud)
ProfileViewModel.kt:
class ProfileViewModel(application: Application) : BaseViewModel(application) {
val name = MutableLiveData<String>()
init {
profileManager.profile()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(::onProfileSuccess, Timber::d)
}
fun …Run Code Online (Sandbox Code Playgroud) data-binding android android-custom-view kotlin android-viewmodel
我有一个带有数据绑定布局的CustomView类,它接受一个变量.在包含CustomView的布局中,我想将一个属性传递给CustomView,并让该CustomView将该属性传递给它自己的布局绑定.这就是我所拥有的:
public class CustomView extends LinearLayout
{
public CustomView(Context inContext, AttributeSet inAttrs)
{
super(inContext, inAttrs);
inflate(inContext, R.layout.custom_view, null);
}
@BindingAdapter({"app:variable"})
public static void SetVariable(CustomView inCustomView, VariableType inMyVariable)
{
CustomViewBinding binding = DataBindingUtil.getBinding(inCustomView);
binding.setMyVariable(inMyVariable);
}
}
Run Code Online (Sandbox Code Playgroud)
这崩溃试图从视图中提取绑定.这甚至可能吗?这是堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'void xxx.databinding.CustomViewBinding.setVariableType(xxx.VariableType)' on a null object reference
at xxx.CustomView.SetDynamicList(CustomView.java:32)
at xxx.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) …Run Code Online (Sandbox Code Playgroud) 我找到了带有自定义视图解释的android 数据绑定,但这对我不起作用,因为我不太了解这一点,而且我的情况也有点不同。
我的想法:
我需要画布,这样我才能在上面画一些东西。我创建了一个扩展 View 类的类(CustomView)。在 CustomView 类中,我创建了负责绘图的服务实例,在重写的onDraw方法中,我将画布传递给服务类,以便应用程序可以绘制。
问题:
在活动中我使用过setContentView(new CustomView());,但如果我想使用 MVVM 设计模式,这将不起作用。如何将它们分开并使其与 MVVM 数据绑定一起使用?我不明白如何以及在何处设置 CustomView 以便它可以通过具有数据绑定的视图获取/绑定?
请耐心等待,我是 android 新手,没有足够的经验。谢谢 :)