代码之家  ›  专栏  ›  技术社区  ›  user1209216

RecyclerView的自定义警报对话框-列表始终为空

  •  1
  • user1209216  · 技术社区  · 7 年前

    首先,我用RecyclerView创建了活动,它工作得很好。我的原始代码:

    活动\协议.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:id="@+id/linearLayout"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:paddingTop="8dp"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toTopOf="@+id/editText"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <TextView
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:ems="10"
            android:gravity="left"
            android:inputType="none|text|textPersonName"
            android:text="* required fields"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/btnGetSelected"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <Button
            android:id="@+id/btnGetSelected"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginBottom="4dp"
            android:text="I agree"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
    
    </android.support.constraint.ConstraintLayout>
    

    传递给ActivityAgreements和适配器安装程序的协议列表:

    @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_agreements);
    
            btnGetSelected = findViewById(R.id.btnGetSelected);
            list = findViewById(R.id.list);
            list.setLayoutManager(new LinearLayoutManager(this));
            list.setHasFixedSize(true);
    
    
            agreements = getIntent().getParcelableArrayListExtra("agreements");
    
    
    
            AgreementsAdapter adapter = new AgreementsAdapter(this.agreements);
            list.setAdapter(adapter);
    

    我的适配器代码:

    public class AgreementsAdapter extends RecyclerView.Adapter<AgreementsAdapter.ViewHolder>
    {
    
        ArrayList<Agreement> agreements;
    
        public AgreementsAdapter(List<Agreement> agreements)
        {
            this.agreements = new ArrayList<>(agreements);
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.agreement_list_item, parent, false);
            return new ViewHolder(v);
        }
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, int position)
        {
            holder.bindData(agreements.get(position));
            holder.checkbox.setOnCheckedChangeListener(null);
            holder.checkbox.setChecked(agreements.get(position).getAccepted());
            //holder.checkbox.setFloorUnCheckedColor(agreements.get(position).getRequired()? Color.rgb(255,0,0):Color.rgb(0,255,0) );
            holder.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> agreements.get(holder.getAdapterPosition()).setAccepted(isChecked));
        }
    
        @Override
        public int getItemCount()
        {
            return agreements.size();
        }
    
        public static class ViewHolder extends RecyclerView.ViewHolder
        {
            private TextView textAgreementName;
            private AppCompatCheckBox checkbox;
    
            public ViewHolder(View v)
            {
                super(v);
                //textAgreementName = v.findViewById(R.id.text_agreement_name);
                checkbox = v.findViewById(R.id.checkbox_agreement_accepted);
            }
    
            public void bindData(Agreement agreement)
            {
                //checkbox.setFloorUnCheckedColor(agreement.getRequired()? Color.rgb(255,0,0):Color.rgb(0,0,255) );
                /*if(agreement.getRequired())
                {
                    textAgreementName.setTypeface(null, Typeface.BOLD);
                    textAgreementName.setText(agreement.getName() + " *");
                }
                else
                    textAgreementName.setText(agreement.getName());*/
    
    
                if(agreement.getRequired())
                {
                    checkbox.setText(agreement.getName() + " *");
                    checkbox.setTypeface(null, Typeface.BOLD);
                }
                else
                {
                    checkbox.setText(agreement.getName());
                }
    
    
    
    
            }
        }
    }
    

    一切都很好,带有列表的活动显示出来没有任何问题。现在,我想使用AlertDialog,而不是Activity来显示协议列表。我正在尝试以这种方式创建AlertDialog(在主活动中):

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = inflater.inflate(R.layout.activity_agreements, null);
                alertDialog.setView(convertView);
    
    
                RecyclerView list = convertView.findViewById(R.id.list);
                list.setLayoutManager(new LinearLayoutManager(this));
                list.setHasFixedSize(true);
    
                AgreementsAdapter adapter = new AgreementsAdapter(agreements);
                list.setAdapter(adapter);
    
    
    
               alertDialog.show();
    

    agreements 变量包含协议列表(与以前传递给AgrRementsActivity的相同)。但它不起作用-对话框以自定义布局显示,但列表始终为空。

    有人能指出我遗漏了什么吗?

    具有固定大小和其他建议的新代码:

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = inflater.inflate(R.layout.activity_agreements, null);
    
    
    
                RecyclerView list = convertView.findViewById(R.id.list);
                list.setLayoutManager(new LinearLayoutManager(this));
                list.setHasFixedSize(true);
    
                AgreementsAdapter adapter = new AgreementsAdapter(agreements);
                list.setAdapter(adapter);
                alertDialog.setView(convertView);
    
                AlertDialog dialog = alertDialog.create();
                dialog.getWindow().setLayout(600, 400);
    
    
    
               dialog.show();
               adapter.notifyDataSetChanged();
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   DAA    7 年前

    RecyclerView可能正在显示列表,但其高度为0,因此看起来是空的。

    对话框大小有点复杂,检查 this answer 试着给它一个固定的高度(也许是你屏幕高度的百分之一)。

        2
  •  1
  •   Hovanes Mosoyan    7 年前

    我们不应该打电话给 通知数据更改 演出结束后?

    祝你好运