从RecyclerView适配器到主脚本的getSelectedButtonID的未更新值

问题描述 投票:0回答:1

我有一个显示两个视图的recyclerView。我希望第二个视图的文本根据从第一个视图选择的“材质按钮”而改变。有点像标签系统,但没有标签和片段。到目前为止,我觉得recyclerView适配器通过接口发送与应用程序相同的ID,该ID仅在我触摸切换组的第一个按钮而不是其他按钮时才能够检测到。它不会更新所选按钮的ID。现在,我真的看不到失败的地方。

是因为未更新适配器?我试图在getTabs()函数中对其进行更新,但由于“适配器”属于createRecyclerView()函数,因此无法调用adapter.notifyDataChanged()。

我试图在onBindViewHolder方法中调用onClick方法,但是由于itemview不在onBindViewHolder函数的一部分,所以无法将所选按钮ID设置为togglegroupID(基于“ getSelectedButtonID”)。

我已经阅读我的代码很多次了,找不到错误...

我的RecyclerView适配器脚本:

public class act_main_recycler_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

\...
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        MaterialButtonToggleGroup Tabs;
        MaterialButton Prn_Btn;
        OnButtonListener onButtonListener;
        public ViewHolder(@NonNull View itemView, final OnButtonListener onButtonListener) {
            super(itemView);
            Tabs = itemView.findViewById(R.id.toggleGroup);
            this.onButtonListener = onButtonListener;
            int toggleGroupID = Tabs.getCheckedButtonId();
            Prn_Btn = itemView.findViewById(toggleGroupID);
            itemView.setOnClickListener(this);

            Prn_Btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onButtonListener != null){
                        int position = getAdapterPosition();
                        int ID = Prn_Btn.getId();
                        onButtonListener.ontabClick(position, ID);
                    }
                }
            });

        }

public interface OnButtonListener{
        void onButtonClick (int position);
        void ontabClick(int position, int ID);
    }

}

我的主要代码:

public class act_main extends AppCompatActivity implements act_main_recycler_adapter.OnButtonListener{


    private ArrayList<String> Texts = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lyt_act_main);
        Texts.add("+65.1");
        createRecyclerView();
    }
private void createRecyclerView(){
        RecyclerView recyclerView = findViewById(R.id.Recycler_View);
        act_main_recycler_adapter adapter = new act_main_recycler_adapter(this, Texts,this );
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
private void getTabs(int ID){
        switch (ID){
            case R.id.principale:
                Texts.set(0, "+65.1");
                Toast.makeText(this,"Principales" ,Toast.LENGTH_SHORT ).show();
                break;
            case R.id.audio:
                Texts.set(0, "+21.5");
                Toast.makeText(this,"Audio" ,Toast.LENGTH_SHORT ).show();
                break;
            case R.id.visuels:
                Texts.set(0, "39.5");
                Toast.makeText(this,"Visuels" ,Toast.LENGTH_SHORT ).show();
                break;
            default:
                Texts.set(0, "Paramètres");
                Toast.makeText(this,"Paramètres" ,Toast.LENGTH_SHORT ).show();
        }
    }
    @Override
    public void ontabClick(int position, int ID) {
        getTabs(ID);
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="5dp">

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:paddingTop="10dp"
        app:checkedButton="@+id/principale"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/principale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Principale"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:textColor="@color/textColor"
            app:cornerRadius="5dp"
            app:elevation="5dp" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/audio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Audio"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:textColor="@color/textColor"
            app:cornerRadius="5dp"
            app:elevation="5dp" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/visuels"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Visuels"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:textColor="@color/textColor"
            app:cornerRadius="5dp"
            app:elevation="5dp" />
    </com.google.android.material.button.MaterialButtonToggleGroup>

</RelativeLayout>

目前,第一种情况是在getTabs()函数中起作用,因为Toast消息“ Principales”已正确显示在应用程序上。其他情况则不同,什么也不显示。

我该如何解决?由于无法在createRecyclerView()函数之外调用adapter.notifyDataChanged(),因此如何更新第二个视图的文本。

java android android-studio android-recyclerview
1个回答
0
投票

找到了解决方案。不必使用Tabs.getCheckedId(完全可以工作)并调用一个按钮(由于无法更新,该按钮将保持不变),而必须将一个侦听器添加到切换组并从那里进行工作。修改后的代码如下所示。

将ViewHolder更改为此:

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        MaterialButtonToggleGroup Tabs;
        MaterialButton Prn_Btn;
        OnButtonListener onButtonListener;
        public ViewHolder(@NonNull View itemView, final OnButtonListener onButtonListener) {
            super(itemView);
            Tabs = itemView.findViewById(R.id.toggleGroup);
            this.onButtonListener = onButtonListener;
            int toggleGroupID = Tabs.getCheckedButtonId();
            Prn_Btn = itemView.findViewById(toggleGroupID);
            itemView.setOnClickListener(this);

            Tabs.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
                @Override
                public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                    if (onButtonListener != null){
                        if (isChecked){
                            int position = getAdapterPosition();
                            onButtonListener.ontabClick(position, checkedId);
                        }
                        if (Tabs.getCheckedButtonId() == -1){
                            int position = getAdapterPosition();
                            onButtonListener.ontabClick(position, -1);
                        }
                    }
                }
            });
        }
© www.soinside.com 2019 - 2024. All rights reserved.