【发布时间】:2016-12-11 04:29:05
【问题描述】:
大家好,我还是 Android 新手,我正在尝试构建一个小项目进行测试。我想做的是建立一个纸牌游戏。对于底部玩家,我想显示一个回收者视图,其中包含他手中的所有卡片。我希望卡片图像彼此重叠,并且在播放卡片时重新计算重叠以匹配回收器视图的父宽度。当卡片宽度的总和小于回收视图宽度时,我希望它们在回收视图中显示而不会重叠和居中。
我只是不知道使用什么方法。我正在使用 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();
}
}
}
【问题讨论】:
-
我认为您真正想要的是构建一个 LayoutManager,而不是 ItemDocoration。
-
@Karakuri 是的,看来我做错了我转向扩展 LinearLayoutManager,现在我遇到的问题是,当我将包装内容用于卡片图像视图并重叠时,我只看到 5 个图像因为它们的初始宽度大于父宽度。
-
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { super.onLayoutChildren(recycler, state); int childCount = getChildCount(); if (childCount > 0) { for (int i = 1; i
标签: android android-recyclerview