我正在尝试制作简单的纸牌游戏,我需要在桌面上显示卡片水平,用户可以通过点击选择任意数量的卡片。理想情况下,当使用任何卡片时,卡片应该从其他卡片上提升一点。加卡并非完全可见,它们部分可见,如this。我创建了自定义Recyclerview并使用了项目装饰器来更改卡片的偏移量。然而,每当我改变偏移时,甲板的第一张或最后一张卡完全与其他卡重叠。如下图所示,
如何正确显示上述效果,不丢失第一张(或最后一张)卡?当我们点击它们时,如何实现任何这些卡的提升?我尝试使用" setElevation()"但它没有效果。如果我能够在没有任何第三方库的情况下实现,那将是很好的(但我愿意接受建议)。我使用了以下代码,
Game.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>
注意:我保持recyclerview的宽度是&#34; wrap_content&#34;因为卡的数量是可变的。
答案 0 :(得分:2)
通过调整第一个元素的边距来实现它,如下所示,
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 == 0) {
outRect.set(0, 0, 0, 0);
} else {
outRect.set(overlap, 0, 0, 0);
}
}
}