在使用 FragmentGridPagerAdapter 创建的自定义片段中更新 CircularButton

Updating a CircularButton in a custom Fragment created with FragmentGridPagerAdapter

我有一个可穿戴应用程序,其中有几个使用 FragmentGridPagerAdapter 创建的片段。其中一个片段有几个 CircularButtons,我想在从手持设备 phone 收到消息时更新按钮的背景色。我在接收消息方面没有问题。但是,按钮的颜色(或 UI 中的任何颜色)不会更新。你知道我该如何解决这个问题吗?

public class UIPageAdapter extends FragmentGridPagerAdapter {
private final Context mContext;
MainControlFragment[] mainControlFragments;
private List mRows;
uiChangeListener mUIChangeListener = new uiChangeListener();


public UIPageAdapter(Context ctx, FragmentManager fm) {


    super(fm);

    Log.i("pageAdapter", "constructor");
    mContext = ctx;
    mainControlFragments = new MainControlFragment[2];
    mainControlFragments[0] = new MainControlFragment();
    mainControlFragments[1] = new MainControlFragment();
    LocalBroadcastManager.getInstance(ctx).registerReceiver(mUIChangeListener,new IntentFilter(Constants.BROADCAST_CONTROL_HOME));
}

@Override
public Fragment getFragment(int row, int col) {

Log.i("PageAdapter","Fragment #" + col +"is asked");

    return mainControlFragments[col];

}

public void changeStatus(int button, boolean status) {
    mainControlFragments[0].setStatus(button,status);

 //   notifyDataSetChanged();

}


public class uiChangeListener extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

            String act = intent.getAction();

            if (act == Constants.BROADCAST_CONTROL_HOME) {

                int key =  intent.getIntExtra(Constants.CONTROL_HOME_KEY,-1);
                String command = intent.getStringExtra(Constants.CONTROL_HOME_COMMAND);
                changeStatus(key,command.equals("on"));
            }
    }
}


@Override
public int getRowCount() {
    return 1;
}

@Override
public int getColumnCount(int i) {
    return 2;
}

    }

基本上,当从手持设备接收到消息时,WearableListener class 会向 UIPageAdapter 广播更新消息 这是监听器class

public class ListenerService extends WearableListenerService
 {

    String tag = "ListenerService";

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {


            final String message = (new String(messageEvent.getData()));
        Log.i(tag,message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.BROADCAST_CONTROL_HOME)
                    .putExtra(Constants.CONTROL_HOME_KEY, messageEvent.getPath())
                    .putExtra(Constants.CONTROL_HOME_COMMAND,Integer.parseInt(message.substring(1)))
                    .putExtra("caller",tag));

        }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(tag, "onCreate");
    }

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="smartstuff.com.tr.myautomationtool" >

    <uses-feature android:name="android.hardware.type.watch" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <uses-library
            android:name="com.google.android.wearable"
            android:required="false" />

        <service android:name=".ListenerService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
            </intent-filter>
        </service>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.DeviceDefault.Light" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最后是自定义片段

public class MainControlFragment extends Fragment{
  ViewGroup container;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("controlFragment","create");
        this.container = container;
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.main_control, container, false);

    }

    public void setStatus(int button, boolean status) {
        Log.i("controlFragment",button + " "+ status);
        CircularButton[] btns = new CircularButton[4];
        btns[0] = (CircularButton) container.findViewById(R.id.cbtnFront);
        btns[1] = (CircularButton) container.findViewById(R.id.cbtnBack);
        btns[2] = (CircularButton) container.findViewById(R.id.cbtnBed);
        btns[3] = (CircularButton) container.findViewById(R.id.cbtnCoffee);
        btns[button].setColor(status?Color.BLACK:Color.RED);


    }
}

我也试过 notifyDataSetChanged(); UIPageAdapter 中的方法,但是它只调用片段中的 onCreateView 方法。感谢任何帮助

我假设你已经解决了这个问题,但我必须在调用 setColor():

之后在 CircularButton 上添加对 invalidate() 的调用
_circularButton.setColor(ContextCompat.getColor(getActivity(), buttonColor));
_circularButton.invalidate();

没有调用无效 UI 只更新了一些时间。