我需要创建一个Recyclerview。为此,我创建了cardview模型以显示在recyclerview中。即使我已将匹配父级赋予布局,cardview的宽度也不适合父级。它的宽度大约占屏幕的一半(不匹配父屏幕),任何人都可以支持吗?请建议采取更好的方法。
在下面附加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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="15dp"
android:layout_height="wrap_content"
android:foreground="@drawable/border_creatives"
android:layout_marginHorizontal="6dp"
android:layout_marginVertical="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/dficon"
app:layout_constraintBottom_toTopOf="@id/guideline19"
app:layout_constraintStart_toStartOf="@id/guideline23"
app:layout_constraintTop_toBottomOf="@id/guideline18"
app:layout_constraintEnd_toStartOf="@id/guideline50"/>
<TextView
android:id="@+id/dname"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="@font/amiko_semibold"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline24"
app:layout_constraintEnd_toStartOf="@+id/ddownbutton"
app:layout_constraintStart_toStartOf="@+id/guideline50"
app:layout_constraintTop_toTopOf="@+id/guideline18" />
<TextView
android:id="@+id/ddate"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="@font/amiko_semibold"
android:textColor="@color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="@+id/guideline19"
app:layout_constraintEnd_toStartOf="@id/barrier7"
app:layout_constraintStart_toStartOf="@id/guideline50"
app:layout_constraintTop_toTopOf="@+id/guideline24" />
<TextView
android:id="@+id/dfileextension"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:fontFamily="@font/amiko_semibold"
android:textColor="@color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="@+id/guideline19"
app:layout_constraintStart_toEndOf="@id/barrier7"
app:layout_constraintTop_toTopOf="@+id/guideline24" />
<ImageButton
android:id="@+id/ddownbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/mins_customselector"
android:clickable="true"
android:src="@drawable/filedownload_icon"
app:layout_constraintBottom_toTopOf="@+id/guideline19"
app:layout_constraintEnd_toStartOf="@+id/guideline22"
app:layout_constraintTop_toTopOf="@+id/guideline18"
android:focusable="true" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.90" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8275" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.045" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="ddate" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
具有recyclerview的主布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Download_Activity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintStart_toStartOf="@+id/guideline49"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="@id/toolbar">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="start"
android:text="@string/downloads"
android:textColor="#FCFFFD"
android:textSize="30sp"
app:fontFamily="@font/carter_one"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/drecyclerdownloads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
适配器类
public class DownloadsAdapter extends RecyclerView.Adapter<Downloadsviewholder> {
Download_Activity download_activity;
ArrayList<Downloadsmodel> downloadsmodels;
public DownloadsAdapter(Download_Activity download_activity, ArrayList<Downloadsmodel> downloadsmodels) {
this.download_activity = download_activity;
this.downloadsmodels = downloadsmodels;
}
@NonNull
@Override
public Downloadsviewholder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(download_activity.getBaseContext());
View view = layoutInflater.inflate(R.layout.downloads_elements,null,false);
return new Downloadsviewholder(view);
}
@Override
public void onBindViewHolder(@NonNull final Downloadsviewholder downloadsviewholder, final int i) {
downloadsviewholder.dName.setText(downloadsmodels.get(i).getName());
downloadsviewholder.dUploaddate.setText(downloadsmodels.get(i).getUploaddate());
downloadsviewholder.dExtension.setText(downloadsmodels.get(i).getFileextension());
Picasso.get().load(downloadsmodels.get(i).getIcon()).into(downloadsviewholder.dIcon);
downloadsviewholder.dButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ConnectionCheck.checkconnetion(download_activity.getBaseContext())) {
dowloadFile(downloadsviewholder.dName.getContext(), downloadsmodels.get(i).getName(), downloadsmodels.get(i).getFileextension()
, DIRECTORY_DOWNLOADS, downloadsmodels.get(i).getLink());
} else {
Toast.makeText(download_activity.getBaseContext(),"Check your internet connection", Toast.LENGTH_SHORT).show();
}
}
});
}
public void dowloadFile (Context context,String filename, String fileextension, String destinationdirectory, String url){
DownloadManager downloadManager=(DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse(url);
DownloadManager.Request request=new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationdirectory, filename+"."+fileextension);
downloadManager.enqueue(request);
}
@Override
public int getItemCount() {
return downloadsmodels.size();
}
}
观看者
public class Downloadsviewholder extends RecyclerView.ViewHolder {
TextView dName;TextView dLink;TextView dUploaddate; TextView dExtension;
ImageView dIcon;
ImageButton dButton;
public Downloadsviewholder(@NonNull View itemView) {
super(itemView);
dName = itemView.findViewById(R.id.dname);
dUploaddate=itemView.findViewById(R.id.ddate);
dExtension=itemView.findViewById(R.id.dfileextension);
dButton=itemView.findViewById(R.id.ddownbutton);
dIcon=itemView.findViewById(R.id.dficon);
}
}