如何在回收站视图中构建自动调整大小的图像视图

时间:2016-12-10 19:32:45

标签: android android-recyclerview

大家好我还是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();
    }
}
}

1 个答案:

答案 0 :(得分: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()并传递给你可以根据给定的获取图像计算的比率。

在布局文件中,您只需将ImageView替换为DynamicHeightImageView,如下所示:

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

图像现在将根据任何图像的高度进行调整。