我可以通过编程方式从我的GridView制作可点击的项目吗?

时间:2018-06-05 15:16:29

标签: android android-studio

在我的应用中,我有一项名为tasti.class的活动。当我按下button" aggiungi tasti"时,会打开一个AlertDialog,我可以输入一个名字。然后,当我按保存时,它会在CardView内的GridView中创建自定义ScrollView,就像您在下面的屏幕截图中看到的一样。

enter image description here

现在,我想知道的是,是否有可能使这些创建CardViews中的任何一个可点击。我希望能够在我点击其中一个时更改其中的Text

这是我的tasti.class代码:

public class tasti extends AppCompatActivity {

    RelativeLayout rellay_LOAD;
    GridLayout gridLayout;


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

        rellay_LOAD = findViewById(R.id.rellay_LOAD);
        gridLayout = findViewById(R.id.gridLayout);

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

                CustomAllert();

            }
        });
    }


    public void addLayout(String textViewText){
        View card = LayoutInflater.from(this).inflate(R.layout.cardview, gridLayout, false);

        TextView textView = card.findViewById(R.id.btnName);

        textView.setText(textViewText);

        gridLayout.addView(card);
    }


    public void CustomAllert(){
        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();

                    addLayout(bottone.getText().toString());

                    dialog.dismiss();

                } else {

                    Toast.makeText(tasti.this,

                            "Non lasciare dei campi vuoti!", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }
}

有没有人对我如何做到这一点有任何建议?有没有办法在没有RecyclerView的情况下完成?

2 个答案:

答案 0 :(得分:2)

为该XML的父级布局添加一个clickListener,您在onCreateViewHolder中的RecyclerView中使用了该XML。

例如,如果您的RecyclerView项如下所示:

<?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"
    android:id="@+id/ll_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
// Your View Elements
</LinearLayout>

在您的onBindViewHolder中添加一个侦听器,例如:

parentLayout.setOnClickListener({
})

答案 1 :(得分:0)

您可以尝试:

cardView.setOnClickListener(v ->
{
    // update TextView text
});

如果您不介意使用Lambda语句,则此方法有效。如果您尝试使用上述内容,则可能会出现lambda expressions not supported at this language level错误,可以通过更新Android Studio的源兼容性和目标兼容性轻松解决,如here所示。

由于您使用XML创建CardView,因此可以将其用于程序化使用,如下所示:

CardView cardView = (CardView) findViewById(R.id.yourCardViewId);
相关问题