嘿,我在显示CardView
中的一行时遇到问题。我想用行分隔图像和标题。我在CardView
中使用view作为行。在预览中可以,但是在运行应用程序时不显示。这是我的代码:
我在另一个项目中做到了这一点,并且有效,但是在这个项目中不起作用
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView `enter code here`xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="4dp"
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
app:cardCornerRadius="8dp"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/img_amazingRow_icon"
android:layout_alignParentRight="true"
tools:src="@mipmap/ic_launcher"
android:layout_width="130dp"
android:layout_height="170dp" />
<TextView
android:textColor="#000"
android:id="@+id/txt_amazingRow_title"
android:ellipsize="end"
android:maxLines="1"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/img_amazingRow_icon"
tools:text="سرویس چای خوری"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/line_amazingRow"
android:layout_margin="8dp"
android:layout_below="@+id/txt_amazingRow_title"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:textColor="@color/gray500"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"
android:layout_below="@id/line_amazingRow"
tools:text="24500"
android:id="@+id/txt_amazingRow_discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="8dp"
android:textColor="#000"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"
android:layout_below="@id/txt_amazingRow_discount"
tools:text="24500"
android:id="@+id/txt_amazingRow_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
getMainData();
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageView imgHamberMenu = (ImageView) findViewById(R.id.img_toolbar_hamberMenu);
imgHamberMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.openDrawer(Gravity.RIGHT);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void getMainData() {
ApiService service = ApiClient.getClient().create(ApiService.class);
Call<MainData> call = service.getMainData();
call.enqueue(new Callback<MainData>() {
@Override
public void onResponse(Call<MainData> call, Response<MainData> response) {
MainData mainData = response.body();
cats=mainData.getCats();
iamgeUrl = mainData.getSliders();
catRecycler.setAdapter(new CatAdapter(MainActivity.this,cats));
amazingList=mainData.getAmazings();
for (int i = 0; i < iamgeUrl.size(); i++) {
DefaultSliderView defaultSliderView = new DefaultSliderView(MainActivity.this);
defaultSliderView
.image(iamgeUrl.get(i).getUrl())
.setScaleType(BaseSliderView.ScaleType.CenterCrop)
.setOnSliderClickListener(MainActivity.this);
defaultSliderView.bundle(new Bundle());
slider.addSlider(defaultSliderView);
txtHour.setText(hour+"");
txtMin.setText(min+"");
txtSecond.setText(second+"");
}
amazingRecycler.setAdapter(new AmazingAdapter(MainActivity.this,amazingList));
}
@Override
public void onFailure(Call<MainData> call, Throwable t) {
Log.i("LOG", t.toString());
}
});
}
public void setupViews() {
txtHour=(TextView)findViewById(R.id.txt_amazing_hour);
txtMin=(TextView)findViewById(R.id.txt_amazing_min);
txtSecond=(TextView)findViewById(R.id.txt_amazing_second);
imgBannerB=(ImageView)findViewById(R.id.img_main_bannerBottom) ;
imgBannerR=(ImageView)findViewById(R.id.img_main_bannerRight) ;
imgBannerL=(ImageView)findViewById(R.id.img_main_bannerLeft);
Picasso.with(getApplicationContext()).load("https://dkstatics-public.digikala.com/digikala-adservice-banners/4387.jpg").into(imgBannerB);
Picasso.with(getApplicationContext()).load("https://dkstatics-public.digikala.com/digikala-adservice-banners/2774.jpg").into(imgBannerL);
Picasso.with(getApplicationContext()).load("https://dkstatics-public.digikala.com/digikala-adservice-banners/1814.jpg").into(imgBannerR);
amazingRecycler=(RecyclerView)findViewById(R.id.rv_main_amazing);
amazingRecycler.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true));
iamgeUrl = new ArrayList<>();
catRecycler=(RecyclerView)findViewById(R.id.rv_main_cats);
catRecycler.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true));
slider = (SliderLayout) findViewById(R.id.slider_main);
slider.setPresetTransformer(SliderLayout.Transformer.Default);
slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
}
适配器代码:
public class AmazingAdapter extends RecyclerView.Adapter<AmazingAdapter.AmazingViewHolder> {
public Context context;
public List<Amazing> amazingList;
public AmazingAdapter(Context context, List<Amazing> amazingList){
this.amazingList=amazingList;
this.context=context;
}
@NonNull
@Override
public AmazingViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.amazing_row,viewGroup,false);
return new AmazingViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AmazingViewHolder amazingViewHolder, int position) {
Amazing amazing=amazingList.get(position);
amazingViewHolder.txtTitle.setText(amazing.getTitle());
amazingViewHolder.txtPrice.setText(amazing.getPrice()+" تومان");
SpannableString spannableString=new SpannableString(amazing.getPrice()+"تومان ");
spannableString.setSpan(new StrikethroughSpan(),0,amazing.getPrice().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
amazingViewHolder.txtDiscount.setText(spannableString);
Picasso.with(context).load(amazing.getImg()).into(amazingViewHolder.imgIcon);
}
@Override
public int getItemCount() {
return amazingList.size();
}
public class AmazingViewHolder extends RecyclerView.ViewHolder{
ImageView imgIcon;
TextView txtTitle;
TextView txtPrice,txtDiscount;
public AmazingViewHolder(@NonNull View itemView) {
super(itemView);
txtTitle=(TextView)itemView.findViewById(R.id.txt_amazingRow_title);
txtPrice=(TextView)itemView.findViewById(R.id.txt_amazingRow_price);
txtDiscount=(TextView)itemView.findViewById(R.id.txt_amazingRow_discount);
imgIcon=(ImageView)itemView.findViewById(R.id.img_amazingRow_icon);
}
}
}
我希望在txt_amazingRow_title
和txt_amazingRow_discount
之间显示一行
但这没用
答案 0 :(得分:1)
@john,
似乎您的布局层次结构正确。只需要替换一些属性。
我刚刚将tools:src
替换为android:src
,将tools:text
替换为android:text
。现在一切正常。 Check this Screenshot
此外,您应该使用 @+id
而不是 @id
。两者都一样。 @id
不会在代码中引起任何问题,但是最好使用@+id
来避免任何意外的问题。
就是这样。