2016-06-14 31 views
1

我幾乎成功地用RecyclerView和GridLayoutManager構建了一個棋盤組件,座標單元格的尺寸是單元格尺寸的一半。但我不知道爲什麼,左邊座標區和第一個單元格列(「a」列)之間存在垂直間隙。我的組件中的空間錯誤

RecyclerViewTest

其原因是管理細胞ImageView的,這樣我可以像滑翔圖書館工作。

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

你能建議一個更好的嗎?我不知道如何恢復我的錯誤。 – loloof64

+0

已經給出:查看最後的代碼片段 – loloof64

回答

1

問題:

,這是因爲發生了邊境你正在服用的高度 和寬度的一半當這些參數變成一半時,半個地方就是 不填充,即爲什麼你看到差距。

解決方案:

可以走人留有餘量等於你的寬度和差距 將會消失..!但只是第一列,因爲你只有第一列有問題.. !!

嘗試了你的代碼。只需在您的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); 
    } 
} 
+0

如果它解決了問題,請標記爲正確。 –

+0

對不起,我只需要啓動Android Studio和Emulator來查看結果。非常感謝:結果是完美的。 – loloof64

+0

這很好..我希望幫助.. !! –