Java 我的组件中的空间错误

Java 我的组件中的空间错误,java,android,android-recyclerview,chess,gridlayoutmanager,Java,Android,Android Recyclerview,Chess,Gridlayoutmanager,我几乎成功地用RecyclerView和GridLayoutManager构建了一个棋盘组件,其中坐标单元格的大小是单元格大小的一半。但我不知道为什么,在左坐标区域和第一个单元格列(“a”列)之间有一个垂直间隙 这样做的原因是将单元格管理为ImageView,这样我就可以使用Glide这样的库 MainActivity.java: package com.loloof64.recycler_view_test; import android.support.v7.app.AppCompatA

我几乎成功地用RecyclerView和GridLayoutManager构建了一个棋盘组件,其中坐标单元格的大小是单元格大小的一半。但我不知道为什么,在左坐标区域和第一个单元格列(“a”列)之间有一个垂直间隙

这样做的原因是将单元格管理为ImageView,这样我就可以使用Glide这样的库

MainActivity.java:

package com.loloof64.recycler_view_test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
activity_main.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.loloof64.recycler_view_test.MainActivity">

    <com.loloof64.recycler_view_test.BoardComponent
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>
BoardComponentAdapter.java:

package com.loloof64.recycler_view_test;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class BoardComponentAdapter extends    RecyclerView.Adapter<BoardComponentViewHolder> {

    public final static int ITEMS_PER_ROW = 10;
    public final static int TOTAL_ITEMS = ITEMS_PER_ROW * ITEMS_PER_ROW;
    private final static int CELL_SIZE_DP = 20;

    private int cellSizePx;

    public BoardComponentAdapter(Context context){
        cellSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CELL_SIZE_DP,  context.getResources().getDisplayMetrics());
    }


    @Override
    public BoardComponentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.board_component, parent, false);
        return new BoardComponentViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(BoardComponentViewHolder holder, int  position) {
        int rowPosition = position / ITEMS_PER_ROW;
        int colPosition = position % ITEMS_PER_ROW;

        boolean isACellRow = rowPosition > 0 && rowPosition < (ITEMS_PER_ROW-1);
        boolean isACellCol = colPosition > 0 && colPosition < (ITEMS_PER_ROW-1);

        if (isACellCol && isACellRow){
            int color = (rowPosition+colPosition) % 2 == 0 ? Color.rgb(255,180, 35) : Color.rgb(128,41,21);
            holder.imageView.setBackgroundColor(color);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellSizePx, cellSizePx);
            holder.imageView.setLayoutParams(layoutParams);
        }
        else {
            int cellWidth = isACellCol ? cellSizePx : cellSizePx/2;
            int cellHeight = isACellRow ? cellSizePx : cellSizePx/2;
              holder.imageView.setBackgroundColor(Color.rgb(191,96,123));
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellWidth, cellHeight);
            holder.imageView.setLayoutParams(layoutParams);
        }
    }

    @Override
    public int getItemCount() {
        return TOTAL_ITEMS;
    }
}
board_component.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/cell_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

问题:

这是因为你的边界占据了一半的高度 当这些参数变为一半时,宽度就变为一半 未填充,即您为什么会看到缺口

解决方案:

您可以使左边距等于单元格宽度和间距 会消失的。。!!但只针对第一列,因为您仅在第一列遇到问题

我试过你的代码。刚刚在您的
BoardComponentAdapter.java中进行了更改,对我来说工作正常。请参见以下更改:

 @Override
public void onBindViewHolder(BoardComponentViewHolder holder, int  position) {
    int rowPosition = position / ITEMS_PER_ROW;
    int colPosition = position % ITEMS_PER_ROW;

    boolean isACellRow = rowPosition > 0 && rowPosition < (ITEMS_PER_ROW-1);
    boolean isACellCol = colPosition > 0 && colPosition < (ITEMS_PER_ROW-1);

    if (isACellCol && isACellRow){
        int color = (rowPosition+colPosition) % 2 == 0 ? Color.rgb(255,180, 35) : Color.rgb(128,41,21);
        holder.imageView.setBackgroundColor(color);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellSizePx, cellSizePx);
        holder.imageView.setLayoutParams(layoutParams);
    }
    else
    {
        int cellWidth = isACellCol ? cellSizePx : cellSizePx / 2;
        int cellHeight = isACellRow ? cellSizePx : cellSizePx / 2;
        holder.imageView.setBackgroundColor(Color.rgb(191, 96, 123));
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellWidth, cellHeight);

        //Just Added three lines here..!!

        if(colPosition==0) {
            layoutParams.setMargins(cellWidth,0,0,0);
        }
        holder.imageView.setLayoutParams(layoutParams);
    }
}
@覆盖
BindViewHolder上的公共无效(BoardComponentViewHolder,内部位置){
int rowPosition=每行的位置/项目;
int colPosition=每行的位置%ITEMS;
布尔值isACellRow=rowPosition>0&&rowPosition<(每行1个项目);
布尔值isACellCol=colPosition>0&&colPosition<(每行1项);
如果(isACellCol&&isACellRow){
int color=(rowPosition+colPosition)%2==0?color.rgb(255180,35):color.rgb(128,41,21);
holder.imageView.setBackgroundColor(颜色);
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(cellSizePx,cellSizePx);
holder.imageView.setLayoutParams(layoutParams);
}
其他的
{
int cellWidth=isACellCol?cellSizePx:cellSizePx/2;
int cellHeight=isACellRow?cellSizePx:cellSizePx/2;
holder.imageView.setBackgroundColor(Color.rgb(19196123));
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(单元格宽度、单元格高度);
//刚刚在这里添加了三行。。!!
if(colPosition==0){
layoutParams.setMargins(单元格宽度,0,0,0);
}
holder.imageView.setLayoutParams(layoutParams);
}
}

您能推荐一个更好的吗?我不知道如何恢复我的错误。已经给出:查看最后一个代码段,如果它解决了问题,请将其标记为正确。。!!抱歉耽搁了:我只需要启动Android Studio和Emulator来查看结果。非常感谢,结果很好,很好。。我希望这有帮助。。!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/cell_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
 @Override
public void onBindViewHolder(BoardComponentViewHolder holder, int  position) {
    int rowPosition = position / ITEMS_PER_ROW;
    int colPosition = position % ITEMS_PER_ROW;

    boolean isACellRow = rowPosition > 0 && rowPosition < (ITEMS_PER_ROW-1);
    boolean isACellCol = colPosition > 0 && colPosition < (ITEMS_PER_ROW-1);

    if (isACellCol && isACellRow){
        int color = (rowPosition+colPosition) % 2 == 0 ? Color.rgb(255,180, 35) : Color.rgb(128,41,21);
        holder.imageView.setBackgroundColor(color);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellSizePx, cellSizePx);
        holder.imageView.setLayoutParams(layoutParams);
    }
    else
    {
        int cellWidth = isACellCol ? cellSizePx : cellSizePx / 2;
        int cellHeight = isACellRow ? cellSizePx : cellSizePx / 2;
        holder.imageView.setBackgroundColor(Color.rgb(191, 96, 123));
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(cellWidth, cellHeight);

        //Just Added three lines here..!!

        if(colPosition==0) {
            layoutParams.setMargins(cellWidth,0,0,0);
        }
        holder.imageView.setLayoutParams(layoutParams);
    }
}