我几乎设法使用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>
BoardComponent.java:
package com.loloof64.recycler_view_test;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class BoardComponent extends RecyclerView {
public BoardComponent(Context context) {
super(context);
initialize(context);
}
public BoardComponent(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(context);
}
public BoardComponent(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
private void initialize(Context context){
setLayoutManager(new GridLayoutManager(context, BoardComponentAdapter.ITEMS_PER_ROW));
setAdapter(new BoardComponentAdapter(context));
}
}
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;
}
}
BoardComponentViewHolder.java
package com.loloof64.recycler_view_test;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
public class BoardComponentViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public BoardComponentViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.cell_view);
}
}
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>
答案 0 :(得分:1)
<强>问题:强>
这种情况正在发生,因为对于边境,你的身高只有一半 和这些参数的宽度减半,半个地方就在那里 没有填补,即你为何看到GAP。
解决方案:
您可以保留左边距等于您的单元格宽度和间隙 将会消失.. !!但仅针对第一栏,因为您在第一栏中遇到问题.. !!
尝试了你的代码。刚刚对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);
}
}