问题描述
回收站视图内的视图未正确对齐。 recyclerview中的项目在添加新项目时具有不同的高度。
我尝试使用display metrics
,但无法实现。可以通过添加或删除项目来更改我的项目列表,但视图填充,宽度和高度必须不受干扰。
categoryRecyclerView = findViewById(R.id.categoriesRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
categoryRecyclerView.setLayoutManager(gridLayoutManager);
categoryRecyclerView.addItemdecoration(new ItemDecorator(getResources().getDimensionPixelSize(R.dimen.item_spacing),3));
CategoryItemAdapter categoryItemAdapter = new CategoryItemAdapter(this,categoryList);
categoryRecyclerView.setAdapter(categoryItemAdapter);
适配器代码
public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.MyViewHolder> {
private Context context;
private List<Category> categoryList;
private onItemClickListener mItemClickListener;
public CategoryItemAdapter(Context context,List<Category> categoryList) {
this.context = context;
this.categoryList = categoryList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CategoryItemAdapter.MyViewHolder holder,int position) {
Category category = (Category) categoryList.get(position);
holder.textView.setText(category.getCategoryName());
}
@Override
public int getItemCount() {
return categoryList.size();
}
public void setonItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view,int position,Category category);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardview;
TextView textView;
ImageView selectItem;
RelativeLayout selectItemLayout;
LinearLayout linearLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
cardview = itemView.findViewById(R.id.cardview);
textView = itemView.findViewById(R.id.categoryName);
linearLayout = itemView.findViewById(R.id.llbackground);
selectItemLayout = itemView.findViewById(R.id.selectItemLayout);
selectItem = itemView.findViewById(R.id.selectItem);
selectItemLayout.setVisibility(authenticationHelper.isAdmin() ? View.VISIBLE : View.GONE);
itemView.setonClickListener(this);
Util.setRandomGradientColor(linearLayout,GradientDrawable.Orientation.LEFT_RIGHT);
}
@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view,getAdapterPosition(),categoryList.get(getAdapterPosition()));
}
}
}
}
查看代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="150dp"
android:layout_height="100dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp">
<LinearLayout
android:id="@+id/llbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/category_gradient"
android:gravity="center">
<TextView
android:id="@+id/categoryName"
android:text="Men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/selectItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
android:padding="5dp">
<ImageView
android:id="@+id/selectItem"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="false"
android:tag="@string/unchecked"
android:enabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/circle"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
活动中的回收者视图
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/categoriesRecyclerView"
android:layout_below="@+id/categoriesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
感谢帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)