[英]Android Recycler View inside a custom Dialog doesn't show
I'm creating an Android app that shows a simple skin color picker using a custom Dialog
and a (horizontal) Recycler View
.我正在创建一个 Android 应用程序,该应用程序使用自定义Dialog
和(horizontal) Recycler View
显示一个简单的肤色选择器。
But when I show the Dialog
(by clicking an Edit Text
), the Dialog
appeared but the Recycler View
didn't like this image below.但是当我显示Dialog
(通过单击Edit Text
)时, Dialog
出现了,但Recycler View
不喜欢下面的这张图片。 The Dialog
only showed the Button
. Dialog
只显示Button
。
Did I miss something in my codes?我是否遗漏了代码中的某些内容? Maybe in the Recycler View Adapter
.也许在Recycler View Adapter
。
Here are the codes:以下是代码:
arrays.xml:数组.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="skinColors">
<item>#f26ab8</item>
<item>#531216</item>
<item>#ee8c5c</item>
<item>#8b1929</item>
<item>#f5a20f</item>
<item>#2b6c99</item>
<item>#73def6</item>
<item>#f89e0a</item>
<item>#6e938a</item>
<item>#132855</item>
</string-array>
</resources>
item_card_view_color.xml item_card_view_color.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
android:id="@+id/cardViewColor">
</androidx.cardview.widget.CardView>
dialog_color_list_picker.xml dialog_color_list_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewColorList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="@+id/buttonPilih"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_card_view_color"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/buttonPilih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="10dp"
android:text="@string/pilih"
android:textAllCaps="false"/>
</androidx.constraintlayout.widget.ConstraintLayout>
SkinColorHelper.java SkinColorHelper.java
public class SkinColorHelper {
public static List<Integer> getSkinColorList(Context context) {
String[] skinColorList = context.getResources().getStringArray(R.array.skinColors);
List<Integer> output = new ArrayList<>();
for(int i = 0; i < skinColorList.length; i++) {
int color = Color.parseColor(skinColorList[i]);
output.add(color);
}
return output;
}
}
SkinColorPickerAdapter.java SkinColorPickerAdapter.java
public class SkinColorPickerAdapter extends RecyclerView.Adapter<SkinColorPickerAdapter.CardViewHolder> {
private List<Integer> skinColorList = new ArrayList<>();
public SkinColorPickerAdapter() {
}
void setSkinColorList(List<Integer> skinColorList) {
this.skinColorList = skinColorList;
}
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemCardViewColorBinding binding = ItemCardViewColorBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new CardViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull SkinColorPickerAdapter.CardViewHolder holder, int position) {
int color = skinColorList.get(position);
holder.bind(color);
}
@Override
public int getItemCount() {
return skinColorList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder{
private ItemCardViewColorBinding binding;
public CardViewHolder(ItemCardViewColorBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(int color) {
binding.cardViewColor.setBackgroundColor(color);
}
}
}
activity_input_measurement.xml activity_input_measurement.xml
<?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=".ui.patient.inputmeasurement.InputMeasurementActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginHorizontal="50dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextWarnaKulitJari"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:cursorVisible="false"
android:drawableStart="@drawable/ic_baseline_circle_24"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:hint="skin color picker"
android:inputType="text"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonSimpan"
android:text="@string/simpan"
android:textAllCaps="false"
android:textSize="14sp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
InputMeasurementActivity.java输入测量活动.java
public class InputMeasurementActivity extends AppCompatActivity implements View.OnClickListener {
// GENERAL
ActivityInputMeasurementBinding binding;
String TAG = getClass().getSimpleName();
ArrayList<HashMap<String, Object>> patientList = new ArrayList<>();
Dialog dialog;
DialogColorListPickerBinding dialogBinding;
// KEYS FOR PATIENT DATA
String keyPatientId = "id";
String keyPatientName = "name";
String keyPatientGender = "gender";
String keyPatientNik = "nik";
String keyPatientBirthDate = "birthDate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityInputMeasurementBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
dialog = new Dialog(this);
dialogBinding = DialogColorListPickerBinding.inflate(getLayoutInflater());
initializeColorListPickerDialog();
hideProgressBar();
binding.editTextWarnaKulitJari.setOnClickListener(this);
}
// ON CLICK LISTENER
@Override
public void onClick(View v) {
if(v.getId() == R.id.editTextWarnaKulitJari) {
dialog.show();
}
}
private void showProgressBar() {
binding.linearLayout.setVisibility(View.GONE);
binding.progressBar.setVisibility(View.VISIBLE);
}
private void hideProgressBar() {
binding.linearLayout.setVisibility(View.VISIBLE);
binding.progressBar.setVisibility(View.GONE);
}
private void addDataToSkinColorRecylcerView() {
List<Integer> colorList = SkinColorHelper.getSkinColorList(this);
Log.d(TAG, "colorListSize: " + colorList.size() + " colorList: " + colorList.toString());
SkinColorPickerAdapter adapter = new SkinColorPickerAdapter();
adapter.setSkinColorList(colorList);
adapter.notifyDataSetChanged();
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
dialogBinding.recyclerViewColorList.setLayoutManager(layoutManager);
dialogBinding.recyclerViewColorList.setHasFixedSize(true);
dialogBinding.recyclerViewColorList.setAdapter(adapter);
}
private void initializeColorListPickerDialog() {
addDataToSkinColorRecylcerView();
dialog.setContentView(R.layout.dialog_color_list_picker);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
UPDATE更新
I saw that your adapter was set up totally wrong.我看到您的适配器设置完全错误。 I think you got confused at the end of the closely same names in your app.我认为您在应用程序中完全相同的名称末尾感到困惑。 But the good news are, that I fixed that for you.但好消息是,我为你解决了这个问题。 Here is your SkinColorPickerAdapter.class
:这是您的SkinColorPickerAdapter.class
:
public class SkinColorPickerAdapter extends RecyclerView.Adapter<SkinColorPickerAdapter.CardViewHolder> {
private List<Integer> skinColorList = new ArrayList<>();
public SkinColorPickerAdapter() {
}
@NonNull
@Override
public SkinColorPickerAdapter.CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_view_color,parent,false);
return new SkinColorPickerAdapter.CardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SkinColorPickerAdapter.CardViewHolder holder, int position) {
int color = skinColorList.get(position);
holder.cardView.setCardBackgroundColor(color);
}
@Override
public int getItemCount() {
return skinColorList.size();
}
public void setSkinColorList(List<Integer> skinColorList) {
this.skinColorList = skinColorList;
notifyDataSetChanged();
}
public class CardViewHolder extends RecyclerView.ViewHolder{
private CardView cardView;
public CardViewHolder(View view) {
super(view);
this.cardView = view.findViewById(R.id.cardViewColor);
}
}
}
And here your InputMeasurementActivity.class:这里是您的 InputMeasurementActivity.class:
public class InputMeasurementActivity extends AppCompatActivity implements View.OnClickListener {
// GENERAL
ActivityInputMeasurementBinding binding;
String TAG = getClass().getSimpleName();
ArrayList<HashMap<String, Object>> patientList = new ArrayList<>();
AlertDialog dialog;
AlertDialog.Builder dialogBuilder;
// KEYS FOR PATIENT DATA
String keyPatientId = "id";
String keyPatientName = "name";
String keyPatientGender = "gender";
String keyPatientNik = "nik";
String keyPatientBirthDate = "birthDate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityInputMeasurementBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
hideProgressBar();
binding.editTextWarnaKulitJari.setOnClickListener(this);
}
// ON CLICK LISTENER
@Override
public void onClick(View v) {
if(v.getId() == R.id.editTextWarnaKulitJari) {
initializeColorListPickerDialog();
}
}
private void showProgressBar() {
binding.linearLayout.setVisibility(View.GONE);
binding.progressBar.setVisibility(View.VISIBLE);
}
private void hideProgressBar() {
binding.linearLayout.setVisibility(View.VISIBLE);
binding.progressBar.setVisibility(View.GONE);
}
private void initializeColorListPickerDialog() {
List<Integer> colorList = SkinColorHelper.getSkinColorList(this);
SkinColorPickerAdapter adapter = new SkinColorPickerAdapter();
adapter.setSkinColorList(colorList);
dialogBuilder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.dialog_color_list_picker,null);
view.setClipToOutline(true);
dialogBuilder.setView(view);
RecyclerView recyclerView = view.findViewById(R.id.recyclerViewColorList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
SkinColorPickerAdapter skinColorPickerAdapter = new SkinColorPickerAdapter();
skinColorPickerAdapter.setSkinColorList(colorList);
recyclerView.setAdapter(skinColorPickerAdapter);
dialog = dialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
}
And I changed the size of your recyclerView
in your dialog_color_list_picker.xml
:我在你的dialog_color_list_picker.xml
更改了recyclerView
的大小:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewColorList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="@+id/buttonPilih"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_card_view_color"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/buttonPilih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="10dp"
android:text="@string/pilih"
android:textAllCaps="false"/>
</androidx.constraintlayout.widget.ConstraintLayout>
FINAL RESULT最后结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.