经过多次努力,我在这里发布我的问题并希望我能得到解决方案。 我正在尝试在recyclerview中填充一个gridView但是在每个位置我在网格视图中都有不同的项目,我面临的问题假设我有2个 位置0处的项目和位置1处的6项目我的回收站视图每次占用六个项目的空间,它不会通过在0位置仅显示两个项目来填充, 我如何实现这一目标:
我的RecyclerView适配器代码是: -
public class RecyclerViewDashItemAdapter extends RecyclerView.Adapter<RecyclerViewDashItemAdapter.ViewHolder> {
Context mContext;
WacsDashboardItemResponse wacsDashboardItemResponse;
GridViewItemBaseAdapter gridViewItemBaseAdapter;
public RecyclerViewDashItemAdapter(Context mContext, WacsDashboardItemResponse wacsDashboardItemResponse) {
this.mContext = mContext;
this.wacsDashboardItemResponse = wacsDashboardItemResponse;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_dash_item_adapter, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
gridViewItemBaseAdapter = new GridViewItemBaseAdapter(mContext,
wacsDashboardItemResponse.subGroup.get(position).itemArray);
holder.gridViewDashItem.setAdapter(gridViewItemBaseAdapter);
}
@Override
public int getItemCount() {
return wacsDashboardItemResponse.subGroup.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
GridView gridViewDashItem;
public ViewHolder(View itemView) {
super(itemView);
gridViewDashItem = (GridView) itemView.findViewById(R.id.gridViewDashItem);
}
}
}
我的RecyclerView适配器的xml是: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/edit_text"
android:orientation="vertical">
<GridView
android:id="@+id/gridViewDashItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:columnWidth="300dp"
android:horizontalSpacing="2dp"
android:numColumns="2"
android:padding="2dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp" />
</LinearLayout>
我的GridView代码是: -
public class GridViewItemBaseAdapter extends BaseAdapter {
Context mContext;
List<WacsDashboardItemResponse.SubGroup.ItemArray> itemArray;
public GridViewItemBaseAdapter(Context mContext, List<WacsDashboardItemResponse.SubGroup.ItemArray> itemArray) {
this.mContext = mContext;
this.itemArray = itemArray;
}
@Override
public int getCount() {
return itemArray.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView = null;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.grid_view_dash_item, null);
TextView itemName = (TextView) gridView.findViewById(R.id.itemName);
itemName.setText(itemArray.get(position).itmeDesc);
}
return gridView;
}
}
网格视图的我的Xml是: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.orbitsysindia.repairordermanagement.servicereception.util.custom.TextViewMedium
android:id="@+id/itemName"
style="@style/textColorBlack_14sp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Check Engine" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:src="@drawable/wiperblade" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SwitchTheme" />
</LinearLayout>
我在每个位置都有不同的项目,如何解决每个部分的高度问题,先谢谢。
答案 0 :(得分:1)
你必须修复gridview项目的高度,因为它目前是matchparent,所以它采取最大高度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="320dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.orbitsysindia.repairordermanagement.servicereception.util.custom.TextViewMedium
android:id="@+id/itemName"
style="@style/textColorBlack_14sp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Check Engine" />
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:src="@drawable/wiperblade" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SwitchTheme" />