Java 从RecyclerView适配器到主脚本的getSelectedButtonID的未更新值
我有一个显示两个视图的recyclerView。我希望第二个视图的文本根据第一个视图中的“选定材质”按钮进行更改。有点像标签系统,但没有标签和碎片。到目前为止,recyclerView适配器通过一个接口发送,我觉得,与应用程序相同的ID只能在我触摸togglegroup的第一个按钮时检测,而不能检测其他按钮。它不会更新所选按钮的ID。现在,我真的不知道我在哪里失败了 是不是因为适配器没有更新?我试图在getTabs()函数中更新它,但我无法调用adapter.notifyDataChanged(),因为“adapter”属于createRecyclerView()函数 我试图在onBindViewHolder方法中调用onClick方法,但无法将所选按钮ID设置为togglegroupID(基于“getSelectedButtonID”),因为itemview不是onBindViewHolder函数的一部分 我已经阅读了我的代码很多次了,现在找不到错误 我的RecyclerView适配器脚本:Java 从RecyclerView适配器到主脚本的getSelectedButtonID的未更新值,java,android,android-studio,android-recyclerview,Java,Android,Android Studio,Android Recyclerview,我有一个显示两个视图的recyclerView。我希望第二个视图的文本根据第一个视图中的“选定材质”按钮进行更改。有点像标签系统,但没有标签和碎片。到目前为止,recyclerView适配器通过一个接口发送,我觉得,与应用程序相同的ID只能在我触摸togglegroup的第一个按钮时检测,而不能检测其他按钮。它不会更新所选按钮的ID。现在,我真的不知道我在哪里失败了 是不是因为适配器没有更新?我试图在getTabs()函数中更新它,但我无法调用adapter.notifyDataChanged(
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\u main\u recycler\u适配器扩展了RecyclerView.adapter{
\...
公共类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
“材料”按钮切换到“组”选项卡;
材料按钮Prn_Btn;
OnButtonListener OnButtonListener;
公共视图持有者(@NonNull视图项视图,最终OnButtonListener OnButtonListener){
超级(项目视图);
Tabs=itemviewbyd(R.id.toggleGroup);
this.onButtonListener=onButtonListener;
int-toggleGroupID=Tabs.getCheckedButtonId();
Prn_Btn=itemviewbyd(toggleGroupID);
setOnClickListener(这个);
Prn_Btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(onButtonListener!=null){
int position=getAdapterPosition();
int ID=Prn_Btn.getId();
onButtonListener.ontabClick(位置,ID);
}
}
});
}
公共接口OnButtonListener{
void onButtonClick(内部位置);
void ontabClick(int位置,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);
}
}
公共类act\u main扩展AppCompative活动实现act\u main\u recycler\u adapter.OnButtonListener{
私有ArrayList text=新ArrayList();
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt_act_main);
正文。添加(“+65.1”);
createRecyclerView();
}
私有void createRecyclerView(){
RecyclerView RecyclerView=findViewById(R.id.Recycler\u视图);
act_main_recycler_adapter adapter=新act_main_recycler_adapter(这个,文本,这个);
recyclerView.setAdapter(适配器);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
}
私有void getTabs(int-ID){
开关(ID){
案例R.id.原则:
文本集(0,“+65.1”);
Toast.makeText(这是“Principales”,Toast.LENGTH_SHORT).show();
打破
案例R.id.audio:
文本集(0,“+21.5”);
Toast.makeText(此为“音频”,Toast.LENGTH_SHORT).show();
打破
案例R.id.visuels:
文本集(0,“39.5”);
Toast.makeText(这是“Visuels”,Toast.LENGTH_SHORT).show();
打破
违约:
文本集(0,“参数”);
Toast.makeText(这是“Paramètres”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
public void ontabClick(整数位置,整数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()。找到了解决方案。而不是使用Tabs.getCheckedId(完全有效)并调用按钮(由于无法更新,因此按钮将保持不变),您必须向togglegroup添加一个侦听器,然后从那里开始工作 将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);
}
}
}
});
}