Android RecyclerView.ViewHolder.itemView getWidth()getHeight()返回0
我试图在RecyclerView中列出一行图像 下面是一些代码 JavaAndroid RecyclerView.ViewHolder.itemView getWidth()getHeight()返回0,android,mvvm,android-recyclerview,android-databinding,android-viewholder,Android,Mvvm,Android Recyclerview,Android Databinding,Android Viewholder,我试图在RecyclerView中列出一行图像 下面是一些代码 Java public class ItemSelectFragment extends Fragment { private ArrayList<Item> mItems; private RecyclerView mRecyclerView; public ItemAdapter mAdapter; private String TAG ="Item_Select_Fragment"; @Ov
public class ItemSelectFragment extends Fragment {
private ArrayList<Item> mItems;
private RecyclerView mRecyclerView;
public ItemAdapter mAdapter;
private String TAG ="Item_Select_Fragment";
@Override
public void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRetainInstance(true);
mItems=new ArrayList<>();
for(int i=1;i<9;i++) {
assert mItems != null;
mItems.add(new Item(i));
}
mAdapter = new ItemAdapter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_select_fragment, container, false);
mRecyclerView = view.findViewById(R.id.item_select_recycler_view);
mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d(TAG,"Recycler View Width: "+ mRecyclerView.getWidth()+" Height: "+ mRecyclerView.getHeight());
LinearLayoutManager manager=new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
return view;
}
private class ItemHolder extends RecyclerView.ViewHolder {
private ItemBinding itemBinding;
private ItemHolder(ItemBinding binding) {
super(binding.getRoot());
itemBinding = binding;
itemBinding.setItemViewModel(new ItemViewModel(itemBinding.getRoot(),getContext()));
}
public void bind(Item item) {
itemBinding.getItemViewModel().setItem(item);
itemBinding.executePendingBindings();
}
}
public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> {
public ItemAdapter(){}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
ItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.item, parent, false);
return new ItemHolder(binding);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
Log.d(TAG,"Binding Position: "+position);
Item item = mItems.get(position);
holder.bind(item);
}
@Override
public int getItemCount() {
return mItems.size();
}
public void resetAdapter(){this.notifyDataSetChanged();}
}
@Override
public void onResume(){
super.onResume();
mAdapter.resetAdapter();
}
@Override
public void onDestroy(){
super.onDestroy();
}
}
item.xml
<?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="ItemViewModel"
type="com.example.mytestapplication.ItemViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="120dp"
android:layout_height="100dp">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="80dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@{ItemViewModel.thumbnail}" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="@{ItemViewModel.title}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton" />
</android.support.constraint.ConstraintLayout>
</layout>
如您所见,按钮7和8处的视图模型没有展开,宽度和高度为0,这导致了问题
有人知道如何解决这个问题吗?您找到解决方案了吗?我也在想同样的问题。我想我有过,但记不得你找到了解决办法吗?我也在想同样的事情。我想我有过,但记不得了
<?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="ItemViewModel"
type="com.example.mytestapplication.ItemViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="120dp"
android:layout_height="100dp">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="80dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@{ItemViewModel.thumbnail}" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="@{ItemViewModel.title}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton" />
</android.support.constraint.ConstraintLayout>
</layout>
D/Item_View_Model: The Image Button Tile is Image Button 5
D/Item_View_Model: Getting Thumbnail
D/Item_View_Model: The Image File Path is /storage/emulated/0/DCIM/Icons/5.jpg
D/Item_View_Model: View Model Width :240 Height :200
D/Item_View_Model: The Image Button Tile is Image Button 6
D/Item_View_Model: Getting Thumbnail
D/Item_View_Model: The Image File Path is /storage/emulated/0/DCIM/Icons/6.jpg
D/Item_View_Model: View Model Width :240 Height :200
D/Item_Select_Fragment: Binding Position: 6
D/Item_View_Model: The Image Button Tile is Image Button 7
D/Item_View_Model: Getting Thumbnail
D/Item_View_Model: The Image File Path is /storage/emulated/0/DCIM/Icons/7.jpg
D/Item_View_Model: View Model Width :0 Height :0
D/Item_Select_Fragment: Binding Position: 7
D/Item_View_Model: The Image Button Tile is Image Button 8
D/Item_View_Model: Getting Thumbnail
D/Item_View_Model: The Image File Path is /storage/emulated/0/DCIM/Icons/8.jpg
D/Item_View_Model: View Model Width :0 Height :0