gpt4 book ai didi

android - 无法在 RecyclerView 上显示图像

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:56 25 4
gpt4 key购买 nike

我想创建一个包含背景图像和 TextView 的项目列表。尽管它会正常创建 RecyclerView 并设置文本,但不会设置背景图像。

这是我的适配器类

public class Casino_Adapter extends       RecyclerView.Adapter<Casino_Adapter.ViewHolder> {

private Data[] mDataset;
private ClickListener clickListener;
public Context context;

// Provide a suitable constructor (depends on the kind of dataset)
public Casino_Adapter(Data[] myDataset) {
this.mDataset = myDataset;
this.context = context;

}

@Override
public Casino_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.casino_card_row, parent, false);
// set the view's size, margins, paddings and layout parameters

ViewHolder vh = new ViewHolder(v);
return vh;
}


public static class ViewHolder extends RecyclerView.ViewHolder {
private final Context context = null;
// each data item is just a string in this case

public TextView mTextView;
public ImageView mImageView;
public CardView cardView;
//Typeface tf;


public ViewHolder(View v) {
super(v);


mTextView = (TextView) v.findViewById(R.id.text);
mImageView = (ImageView) v.findViewById(R.id.image);
cardView = (CardView) v.findViewById(R.id.card_view);
//cardView.setPreventCornerOverlap(false);


}


}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getText());
holder.mImageView.setImageResource(mDataset[position].getImage());
}

public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}


// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}

public interface ClickListener {
public void itemClicked(View view, int pos);
}
}

我有一个 fragment 类,我希望 RecyclerView 位于其中。

public class CasinoFragment extends Fragment {

protected RecyclerView recyclerView;
protected RecyclerView.Adapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;


public CasinoFragment() {
// Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

initial();

}

private void initial() {
final Data datas[] =
{
new Data("This is an item", R.drawable.ic_home_white_36dp),
new Data("This is an item 2", R.drawable.car),
new Data("asdasd`sdads", R.drawable.car),
new Data("This is an item 3", R.drawable.car)
};
mAdapter = new Casino_Adapter(datas);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_casino, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);

return view;
}
}

这些是我使用的Setter

public class Data {
int image;
String text;

public Data(String title, int backimage)
{
this.text = title;
this.image = backimage;

}

public String getText()
{
return text;
}

public int getImage()
{
return image;
}



public void setText()
{
this.text=text;
}

public void setImageID()
{
this.image = image;
}
}

赌场行 XML

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
android:layout_margin="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
>

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="@drawable/image_round"
android:src="@drawable/car" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/image"
android:id="@+id/rel_color"
android:background="#4c4c4c">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Μπύρα"
android:textColor="#fcfcfc"
android:textSize="30sp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:id="@+id/text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

</RelativeLayout>
</android.support.v7.widget.CardView>

我得到的结果如下。 Final Result

最佳答案

您看不到图像的原因是它实际上不可见。您的 rel_color 布局与您的 ImageView 具有相同的尺寸属性。

如果要在 ImageView 上方显示布局,只需要移除它的背景,否则 ImageView 将被隐藏。

关于android - 无法在 RecyclerView 上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37527986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com