0

ActionBarに機能を備えたFragmentActivityがあり、 4つのフラグメントをホストしています。各フラグメントは、基本的にリスト内の結果を異なる方法で並べ替えます(たとえば、評価、価格、距離などに基づいて)LocateMeListView

私の質問は、場所の管理(取得、更新など)がFragmentActivityLocateMeで行われるようにコードを作成する方法ですが、フラグメントはこの場所を使用して、ボタンが押されたときにリストの作成または並べ替え時にデータ呼び出しを実行します。

正しいことを行う方法がわからない場合を除いて、ロケーションを処理するために各フラグメントにLocationManagerがあると、間違いなく間違っていると感じます。

皆さん、ありがとうございました。:)

4

2 に答える 2

2

IMHOがインターフェースと登録リスナーを介する「最もクリーンな」方法

2つのインターフェイスを作成します。

public interface LocationListener{
    public void onLocationAvailable(/* whatever data you want to pass */ );
}
public interface LocationListenersRegistry{
    public void addLocationListener(LocationListener listener);
    public void removeLocationListener(LocationListener listener);
}

次に、アクティビティを実装LocationListenersRegistryし、フラグメントを実装しますLocationListener

アクティビティでは、private ArrayList<LocationListener>add/removeメソッドに従ってリスナーを追加および削除します。アクティビティが新しいデータを受信するたびに、それを処理してから、配列上のすべてのリスナーに渡す必要があります。

フラグメントでは、次のように、アクティビティから自分自身を登録および登録解除する必要がonPauseあります。onResume

onResume(){
    super.onResume();
    ((LocationListenersRegistry)getActivity()).addLocationListener(this);
}

onPause(){
    super.onPause();
    ((LocationListenersRegistry)getActivity()).removeLocationListener(this);
}

編集:

アクティビティはLocationListenersRegistryを実装し、次のコードを持ちます。

public class MyActivity extends Activity implements LocationListenersRegistry {

private ArrayList<LocationListener> listeners = new ArrayList<LocationListener>();
public void addLocationListener(LocationListener listener){
       listeners.add(listener);
}
public void removeLocationListener(LocationListener listener){
    listeners.remove(listener);
}

そして、ユーザーがメニューボタンをクリックするたびに:

  for(LocationListener l:listeners)
         l.onLocationAvailable(/* pass here the data for the fragment */);

フラグメントはLocationListenerを実装します

  public class MyFragment extends Fragments implements LocationListener{
      public void onLocationAvailable( /* receive here the data */){
           // do stuff with your data
       }
于 2013-01-09T10:24:58.483 に答える
1

ブディウスの答えの改善を提案したいと思います。

これらのインターフェースを作成する代わりに、Javaですでに実装されており、Androidで使用可能なオブザーバーパターンを使用できます。

public class MyFragment extends Fragment implements Observer {
     @Override
     public void onResume() {
         super.onResume();
         myLocationServices.addObserver(this);
     }
    @Override
    public void onPause() {
         super.onPause();
         myLocationServices.deleteObserver(this);
    }
    @Override
    public void update(Observable observable, Object data) {
         // do stuff with your data
    }

}

データをカプセル化するために別のクラスを作成しました(Google PlaySDKの一部でGoogleIO2013で導入されたLocationClientを使用しましたが、従来のLocationManager実装を使用できます。まだ学習中です:P):

public class MyLocationServices extends Observable implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
    @Override
    public void onLocationChanged(Location location) {
         Location mBestReading;
         //Obtain the location. Do the staff
         //And notify the observers
         setChanged();
         notifyObservers(mBestReading);
    }

}

于 2014-05-08T12:14:35.340 に答える