[英]How do i extend cardview and pass it as the only item inside Recyclerview?
嗨,我是Android新手,我试图使用itemtouchhelper.callback
进行刷卡以关闭cardview
和Recyclerview
功能,因此我需要通过cardview
作为RecyclerView
的唯一项目,因为提到了此答案,但我不知道如何扩展cardView
使customview?
cardStack
import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
public class CardStack extends CardView {
private int image;
public CardStack(Context context) {
super(context);
initialize(context);
}
public CardStack(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public CardStack(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void initialize(Context context){
LayoutInflater.from(context).inflate(R.layout.card,this);
//What to do here?
}
card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.stocks.android.cardviewobjectswipefeaturestocks.CardStack
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/card1" />
</com.stocks.android.cardviewobjectswipefeaturestocks.CardStack>
</LinearLayout>
简单。
制作一个布局文件,其父文件为CardView。 我做了一个row_list_item.xml,看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:fillViewport="true"
app:cardBackgroundColor="@color/OFF_WHITE"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
.........
</LinearLayout>
</CardView>
然后在适配器内部以这种方式将其传递给适配器:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.row_list_item, parent, false));
}
这样就完成了。 奖励!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.