我正在尝试制作简单的纸牌游戏,我需要展示桌子上的一副纸牌水平地用户可以通过单击选择任意数量的卡片。理想情况下,当使用单击任何卡时,卡应比其他卡稍微抬高一点。另外卡片不是完全可见的,它们是部分可见的,例如this https://d2gg9evh47fn9z.cloudfront.net/thumb_COLOURBOX1642430.jpg。我创建了自定义 Recyclerview 并使用项目装饰器来更改卡片的偏移量。然而,每当我改变偏移量时,牌组的第一张或最后一张牌就会与其他牌完全重叠。就像下图一样,
如何正确展现以上效果,不先输(或最后一张)卡?还如何实现高程当我们点击其中任何一张卡片时?我尝试使用“setElevation()”但没有效果。如果我能实现这个目标就好了没有任何第三方图书馆(不过我愿意接受建议。)。我使用了以下代码,
游戏.java
public class Game extends AppCompatActivity {
private List<Card> cards = new ArrayList<>();
private RecyclerView.ItemDecoration decoration = new OverlapDecoration();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
RecyclerView marketDeck = (RecyclerView) findViewById(R.id.market_deck);
Card item1 = new Card();
item1.setImage(R.color.colorPrimary);
Card item2 = new Card();
item2.setImage(R.color.colorAccent);
cards.add(item1);
cards.add(item2);
cards.add(item1);
cards.add(item2);
cards.add(item1);
CardHolder cardHolder = new CardHolder(cards);
marketDeck.setAdapter(cardHolder);
marketDeck.setLayoutManager(new LinearLayoutManager(getBaseContext(), LinearLayoutManager.HORIZONTAL, false));
marketDeck.addItemDecoration(decoration);
cardHolder.setOnCardClick(new CardHolder.onCardClick() {
@Override
public void onClick(View v) {
v.setElevation(10);
}
});
}
private class OverlapDecoration extends RecyclerView.ItemDecoration {
//Following code from : http://stackoverflow.com/questions/27633454/how-to-overlap-items-in-linearlayoutmanager-recyclerview-like-stacking-cards
private final static int overlap = -100;
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
final int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION) {
return;
}
outRect.set(overlap, 0, 0, 0);
}
}
}
CardHolder.java
public class CardHolder extends RecyclerView.Adapter<CardHolder.CardView> {
private List<Card> cards;
private onCardClick cardClick;
public CardHolder(List<Card> cards) {
this.cards = cards;
}
@Override
public CardView onCreateViewHolder(ViewGroup parent, int viewType) {
return new CardView(LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false));
}
@Override
public void onBindViewHolder(CardView holder, int position) {
Card card = cards.get(position);
holder.icon.setImageResource(card.getImage());
}
@Override
public int getItemCount() {
return cards.size();
}
class CardView extends RecyclerView.ViewHolder {
ImageView icon;
CardView(final View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.card_icon);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cardClick.onClick(v);
}
});
}
}
void setOnCardClick(onCardClick click) {
cardClick = click;
}
public interface onCardClick {
void onClick(View v);
}
}
Card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5sp">
<ImageView
android:id="@+id/card_icon"
style="@style/CardTheme"
android:contentDescription="@null"
app:srcCompat="@color/colorPrimary" />
</LinearLayout>
game.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.someapp.Game">
<android.support.v7.widget.RecyclerView
android:id="@+id/market_deck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Note: 我将 recyclerview 的宽度保留为“wrap_content”,因为卡片的数量是可变的。