gpt4 book ai didi

java - 如何从适配器更改 fragment 中按钮的颜色?

转载 作者:行者123 更新时间:2023-12-02 11:39:52 26 4
gpt4 key购买 nike

我有一个 fragment ,ButtonSharingFragment,其布局称为sharing_buttons.xml,它由3个按钮组成。

我将其嵌入到我的 NewContact Activity 中,在 NewContactlayout 中:

 <fragment
android:name="com.example.chris.tutorialspoint.ButtonSharingFragment"
android:id = "@+id/myFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

我的应用程序加载正常,我看到了它应该在的位置。但是,当我的 recyclerView 中的所有复选框均未选中时,我试图更改 fragment 中其中一个按钮的颜色。 recyclerView 位于我的 Activity NewContact 中。你能告诉我该怎么做吗?

这是我的 ButtonSharingFragment 代码:

public class ButtonSharingFragment extends Fragment{

Button phoneContacts;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment

View buttonView = inflater.inflate(R.layout.sharing_buttons, parent, false);
//return inflater.inflate(R.layout.sharing_buttons, parent, false);

phoneContacts = (Button) buttonView.findViewById(R.id.btnPhoneContacts);

// Defines the xml file for the fragment
return buttonView;
}

}

以及我的适配器OnBindViewHolder,当单击复选框时。 if(count==0) { } 包含什么内容?

 @Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {


//The number of rows will match the number of phone contacts
final SelectPhoneContact selectPhoneContact = theContactsList.get(position);


//in the title textbox in the row, put the corresponding name etc...
((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).getSelected());
((MatchingContact) viewHolder).check.setTag(position);

((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {


//pos is the row number that the clicked checkbox exists in
Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();


//NEED THIS TO PRESERVE CHECKBOX STATE
if (theContactsList.get(pos).getSelected()) {
theContactsList.get(pos).setSelected(false);


} else {


theContactsList.get(pos).setSelected(true);



}


//we want to keep track of checked boxes, so when it is '0'
//'Phone Contacts' button in ButtonSharingFragment will change

//HOW TO DO THIS?

int count;
count = 0;
int size = theContactsList.size();
for (int i = 0; i < size; i++) {
if (theContactsList.get(i).isSelected) {
count++;


}
}
Log.i("MyMessage","The count is " + count);


//if 'count' is 0, then change button colour in ButtonSharingFragment fragment

if (count==0){

//CHANGE COLOUR OF phoneContacts button in ButtonSharingFragment
//have tried interface/callback etc, must be doing it wrong

}

}

});

}

编辑:将我的代码修改为如下所示,但收到 NullPointerException。

我创建了一个名为 UpdateColorCallback 的 java 类:

public interface UpdateColorCallback {
void onUpdateColorCallback();
}

在我的适配器中,我已包含:

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


private UpdateColorCallback updateColorCallback;


public void setUpdateLisenter(UpdateColorCallback updateColorCallback) {
this.updateColorCallback = updateColorCallback;
}

在我的 ButtonSharingFragment 中:

public class ButtonSharingFragment extends Fragment implements UpdateColorCallback {

RecyclerView recyclerView;

Button publicContacts;
Button phoneContacts;
Button justMeContacts;

// ArrayList called selectPhoneContacts that will contain SelectPhoneContact info
ArrayList<SelectPhoneContact> selectPhoneContacts;

// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

PopulistoContactsAdapter adapter = new PopulistoContactsAdapter(selectPhoneContacts, getActivity());

adapter.setUpdateListener(this);

View buttonView = inflater.inflate(R.layout.sharing_buttons, parent, false);
//return inflater.inflate(R.layout.sharing_buttons, parent, false);


//for the Public, phoneContacts, justMe, save and cancel buttons
publicContacts = (Button) buttonView.findViewById(R.id.btnPublic);
phoneContacts = (Button) buttonView.findViewById(R.id.btnPhoneContacts);
justMeContacts = (Button) buttonView.findViewById(R.id.btnJustMe);



// Defines the xml file for the fragment
return buttonView;
}




@Override
public void onUpdateColorCallback() {
// TODO: Implement this
//Toast.makeText(getActivity(), "yes, this is working now"", Toast.LENGTH_SHORT).show();
Log.i("MyMessage","yes, this is working now");
}
}

最佳答案

好吧,您需要首先创建接口(interface),然后将引用从 fragment 传递到适配器。现在,当您想要更改颜色或符合您的条件时,您需要调用。

制作界面:-

public interface UpdateColorCallback {
void onUpdateColorCallback();
}

现在您需要在 fragment 中实现

public class ButtonSharingFragment extends Fragment implement UpdateColorCallback{

Button phoneContacts;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment

View buttonView = inflater.inflate(R.layout.sharing_buttons, parent, false);
//return inflater.inflate(R.layout.sharing_buttons, parent, false);


// pass this from here to adapter
adapter.setUpdateLisenter(this);

phoneContacts = (Button) buttonView.findViewById(R.id.btnPhoneContacts);



// Defines the xml file for the fragment
return buttonView;
}
@Override
public void onUpdateColorCallback() {
//change button color
}


}

适配器代码:-

private UpdateColorCallback updateColorCallback;
public void setUpdateLisenter(UpdateColorCallback updateColorCallback) {
this.updateColorCallback = updateColorCallback;
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
// when ur condition meet then
if(updateColorCallback != null) {
// this call back call fragment
updateColorCallback. onUpdateColorCallback();
}
}

关于java - 如何从适配器更改 fragment 中按钮的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48664149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com