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

标签 java android fragment

我有一个 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/

相关文章:

android - 物理设备在 Android Studio : Multiple RSA key fingerprints but only one adbkey. pub 中不工作

android - 刷新 fragment 不会调用 onCreate()。这是正常的?

java - 如果需要声明,使用 GUI 按钮缩放形状

java - 将样式表添加到 XML 文档中

java - 数组未在 Java/Android 中正确插入数据

android progress hud自动添加

java - Java中两个ResultSet的比较

android - Xamarin Android "ScrollView"奇怪的行为

android - 如何为新 fragment 重用 fragment View

android - 在哪里可以找到 FragmentTabHost 库