如何从我的适配器更改片段中按钮的颜色?

时间:2018-02-07 12:43:32

标签: java android fragment

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

我将其嵌入我的NewContact活动中layout NewContact

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

我的应用程序加载正常,我看到它应该是的片段。但是当我recyclerView中的所有复选框都未选中时,我正在尝试更改片段中某个按钮的颜色。 recyclerView位于我的活动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的{​​{1}}。什么进入adapter

if(count==0) { }

编辑:修改我的代码如下所示,但是我得到了NullPointerException。

我创建了一个名为 @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 } } }); } 的java类:

UpdateColorCallback

在我的适配器中,我包括:

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

2 个答案:

答案 0 :(得分:1)

好吧,你需要首先创建接口然后将引用从片段传递到适配器。现在,当您想要改变颜色或符合条件时,您需要打电话。

制作界面: -

public interface UpdateColorCallback {
    void onUpdateColorCallback();
}

现在你需要实现你的片段

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();
    }
}

答案 1 :(得分:0)

您可以从Frag传递Button实例到适配器类,并更改适配器中的按钮状态,如:

adapter =new ButtonAdapter(button);

然后在适配器中:

    Button button1;

       ButtonAdapter(Button button)
            {
              button1=button;
            }

            if (count==0){        
   button1.setBackgroundColor(getResources().getColor(R.color.white,null));
            }