2016-06-14 20 views
1

Ich habe es fast geschafft, eine Schachbrett-Komponente mit RecyclerView und GridLayoutManager zu bauen, wo die Koordinatenzellen eine halbe Größe haben, als die der Zellen. Aber ich weiß nicht warum, es gibt eine vertikale Lücke zwischen der linken Koordinatenzone und der ersten Zellenspalte (die "a" Spalte).Space Bug in meiner Komponente

RecyclerViewTest

Der Grund dafür ist, Zellen als Image zu verwalten, so dass ich mit einer Bibliothek wie Glide arbeiten kann.

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 Layout:

<?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 Layout

<?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

Können Sie einen besseren vorschlagen? Ich wusste nicht, wie ich meinen Fehler wieder aufnehmen sollte. – loloof64

+0

Bereits gegeben: siehe das letzte Code-Snippet – loloof64

Antwort

1

Problem:

wurde, weil für die Grenz geschieht Sie nehmen die Hälfte der Höhe und Breite Da diese Parameter halbiert wurden, ist der halbe Platz ungefüllt, also warum Sie GAP sehen.

Lösung:

Sie können Randbreite und die Lücke gleich Ihre Zelle links verlassen wird .. weg !! Aber nur für die erste Spalte, da Sie nur in der ersten Spalte Probleme haben .. !!

Ich habe Ihren Code versucht. Just Änderung in Ihrer BoardComponentAdapter.java gemacht und funktioniert gut für mich. Siehe die Änderung unten:

@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

Bitte markieren ist korrekt wenn es das Problem gelöst hat .. !! –

+0

Sorry für die Verzögerung: Ich musste nur Android Studio und Emulator starten, um das Ergebnis zu sehen. Vielen Dank: Das Ergebnis ist perfekt. – loloof64

+0

Es ist in Ordnung .. Ich hoffe, das hat geholfen .. !! –