2016-12-10 22 views
0

嗨,大家好,我對Android還是一個新手,我正在嘗試構建一個小型項目進行測試。我想要做的是建立一個紙牌遊戲。對於底層玩家,我想用他手中的所有牌顯示回收者視圖。我希望卡片圖像彼此重疊,並且在播放卡片時重新計算重疊以匹配回收站視圖的父寬度。當卡片寬度的總和小於recycler視圖的寬度時,我希望它們在重疊視圖中不重疊並居中顯示。如何在回收站視圖內創建自動調整大小的圖像視圖

我只是不能找出使用什麼方法。我使用ItemDecoration創建重疊,但是當我添加重疊的match_parent不起作用。

這是我的回收遊戲視圖。

<RelativeLayout> 
    <!-- Bottom Player --> 
    <LinearLayout android:id="@+id/bottom" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@+id/middle_section" 
     android:orientation="vertical" 
     android:paddingLeft="2dip" 
     android:paddingRight="2dip"> 

     <TextView android:id="@+id/player1_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/player1_cards_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" /> 
    </LinearLayout> 
</RelativeLayout> 

這是玩家的手佈局:

<android.support.v7.widget.CardView 
android:id="@+id/player_hand" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/player_card" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:adjustViewBounds="true" /> 

</RelativeLayout> 
</android.support.v7.widget.CardView> 

然後,我有一個回收視圖適配器:

public class PlayerHandRecyclerViewAdapter extends  RecyclerView.Adapter<PlayerHandRecyclerViewHolder> { 
private ArrayList<Card> _cards = new ArrayList<Card>(); 
private Context _context; 

public PlayerHandRecyclerViewAdapter(Context context, ArrayList<Card> cards) { 
    this._context = context; 
    this._cards = cards; 
} 

@Override 
public PlayerHandRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    //Inflate the layout, initialize the View Holder 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_hand, parent, false); 
    PlayerHandRecyclerViewHolder holder = new PlayerHandRecyclerViewHolder(v, parent); 
    return holder; 

} 

@Override 
public void onBindViewHolder(PlayerHandRecyclerViewHolder holder, int position) { 
    int width = holder.playerHandView.getWidth(); 
    int height = holder.playerHandView.getHeight(); 
    //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView 
    holder.imageView.setImageResource(_cards.get(position).getImageResourceId()); 
} 

@Override 
public int getItemCount() { 
    //returns the number of elements the RecyclerView will display 
    return _cards.size(); 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

// Insert a new item to the RecyclerView on a predefined position 
public void insert(int position, Card card) { 
    _cards.add(position, card); 
    notifyItemInserted(position); 
} 

// Remove a RecyclerView item containing a specified Data object 
public void remove(Card card) { 
    int position = _cards.indexOf(card); 
    _cards.remove(position); 
    notifyItemRemoved(position); 
} 

} 

然後我有一個持有人:

public class PlayerHandRecyclerViewHolder extends RecyclerView.ViewHolder { 

RecyclerView playerHandView; 
CardView cv; 
ImageView imageView; 

PlayerHandRecyclerViewHolder(View itemView, ViewGroup parent) { 
    super(itemView); 

    String parentId = String.valueOf(parent.getId()); 
    int resId = parent.getContext().getResources().getIdentifier(parentId, "id", parent.getContext().getPackageName()); 
    playerHandView = (RecyclerView) parent.findViewById(resId); 
    cv = (CardView) itemView.findViewById(R.id.player_hand); 
    imageView = (ImageView) itemView.findViewById(R.id.player_card); 
} 
} 

而且這是我的物品裝飾類

public class PlayerHandRecyclerViewDecoration extends RecyclerView.ItemDecoration { 
private final static int vertOverlap = -20; 

@Override 
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
    int position = parent.getChildAdapterPosition(view); 
    if (position != 0) { 
     outRect.right = vertOverlap; 
     view.invalidate(); 
    } 
} 
} 
+0

我想你實際上想要的是構建一個LayoutManager,而不是一個ItemDocoration。 – Karakuri

+0

@Karakuri是的,看來我做錯了,我轉向擴展LinearLayoutManager,現在我遇到的問題是,當我使用包裝內容的圖像視圖和重疊,我看到只有5個圖像,因爲它們的初始寬度超過父寬度。 –

+0

public void onLayoutChildren(RecyclerView.Recycler recycler,RecyclerView.State state){0} {0} {0}超級.onLayoutChildren(回收者,狀態); int childCount = getChildCount();如果(childCount> 0){int i = 1; i

回答

0

您可以創建自定義的ImageView如:

public class DynamicHeightImageView extends ImageView { 

    private double mHeightRatio; 

    public DynamicHeightImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public DynamicHeightImageView(Context context) { 
     super(context); 
    } 

    public void setHeightRatio(double ratio) { 
     if (ratio != mHeightRatio) { 
      mHeightRatio = ratio; 
      requestLayout(); 
     } 
    } 

    public double getHeightRatio() { 
     return mHeightRatio; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if (mHeightRatio > 0.0) { 
      // set the image views size 
      int width = MeasureSpec.getSize(widthMeasureSpec); 
      int height = (int) (width * mHeightRatio); 
      setMeasuredDimension(width, height); 
     } 
     else { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 
} 

你應該叫setHeightRatio(),並通過它,你可以從給定獲取的圖像計算出的比率。

在佈局文件,你可以只是DynamicHeightImageView如下替換ImageView

<com.......DynamicHeightImageView 
     /> 

圖像將現在調整基礎上的任何圖像的高度。

相關問題