Добавление CardView в ScrollView - PullRequest
0 голосов
/ 05 июня 2018

В моем приложении у меня есть класс tasti.class, где у меня есть простой layout с ScrollView и некоторыми CardView's.

У него также есть кнопка, и когда я нажимаюесли он открывает пользовательский AlertDialog, поэтому мой вопрос заключается в том, как я могу добавить другие CardViews после того, как я вставлю в свой пользовательский AlertDialog имя для размещения в TextView CardView и "Prezzo", что этодолжно быть описание textView я имею в виду, нажав кнопку «AGGIUNGI», как я могу создать новый CardView и добавить его в нижней части ScrollView?enter image description here

Вот мой XML-код части макета:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:background="@color/blueLight"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".tasti">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/rellay_LOAD"
        android:paddingBottom="2dp">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alignmentMode="alignMargins"
            android:columnCount="2"
            android:columnOrderPreserved="false"
            android:rowCount="4">


            <!-- Row 1 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="4dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CAFFE"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="4dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CAPUCCINO"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 2 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="HAMBURGER"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="BIRRA 50Cl"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 3 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="PROFITTEROL"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="BIRRA 1l"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 4 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LIQUORI VARI"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="4dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LIMONCINO"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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


        </GridLayout>


    </ScrollView>




    <RelativeLayout
        android:id="@+id/rellay_LOAD"
        android:layout_width="match_parent"
        android:layout_height="97dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"
        android:layout_weight="0.65"
        android:background="@color/colorWhite"
        android:clickable="true"
        android:gravity="center"
        android:focusable="true"
        android:foreground="?attr/selectableItemBackground"
        tools:ignore="NestedWeights,ObsoleteLayoutParam">




        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="CREATE"
            android:textSize="14sp"
            tools:ignore="HardcodedText" />


    </RelativeLayout>

</RelativeLayout>

В то время как вот код из моего tasti.class

public class tasti extends AppCompatActivity {

    RelativeLayout rellay_LOAD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasti);

        rellay_LOAD = findViewById(R.id.rellay_LOAD);



        rellay_LOAD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder mBuilder = new AlertDialog.Builder(tasti.this);

                @SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.custom_dialog, null);

                final EditText bottone = mView.findViewById(R.id.etBottone);

                final EditText prezzo = mView.findViewById(R.id.etPrezzo);

                Button mLogin = mView.findViewById(R.id.btnADD);

                mBuilder.setView(mView);

                final AlertDialog dialog = mBuilder.create();

                dialog.show();

                mLogin.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View view) {

                        if (!bottone.getText().toString().isEmpty() && !prezzo.getText().toString().isEmpty()) {

                            Toast.makeText(tasti.this,

                                    "Tasto aggiunto con successo",

                                    Toast.LENGTH_SHORT).show();

                            dialog.dismiss();

                        } else {

                            Toast.makeText(tasti.this,

                                    "Non lasciare dei campi vuoti!",

                                    Toast.LENGTH_SHORT).show();

                        }


                    }
                });
            }
        });
    }
}

1 Ответ

0 голосов
/ 05 июня 2018

В Kotlin вы можете сделать что-то вроде этого:

val mainLinearLayout = findViewById(R.id.constraintLayout) as LinearLayout
val cardLinearLayout = LinearLayout(this)
cardLinearLayout.orientation = LinearLayout.VERTICAL
val params = RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT)
params.setMargins(16,16,16,16)
val cardView = CardView(this)
cardView.radius = 15f
cardView.setCardBackgroundColor(Color.parseColor("#002388"))
cardView.setContentPadding(36,36,36,36)
cardView.layoutParams = params
cardView.cardElevation = 30f
val quote = TextView(this)
quote.text = "\"My cardview.\n";
quote.textSize = 24f
quote.setTextColor(Color.WHITE)
quote.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL)
cardLinearLayout.addView(quote)
cardLinearLayout.addView(name)
cardView.addView(cardLinearLayout)
mainLinearLayout.addView(cardView)
...