Android View寻呼机与屏幕外片段重叠

时间:2014-02-04 05:27:21

标签: android android-fragments android-viewpager

这是我在主活动中的viewPager,地图正在运行,但在加载时,地图片段与其先前的片段重叠

    mAdapter = new MyAdapter(getSupportFragmentManager());      
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setOffscreenPageLimit(3);
    mPager.setAdapter(mAdapter);
    mPager.setPageTransformer(false, new DepthPageTransformer());
    ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
        @Override
        public void onPageSelected(int position) {
            Log.v("TAG", String.valueOf(position));
            super.onPageSelected(position);
            if(position == 1 && mAdapter.fm.findFragmentByTag("android:switcher:" + R.id.pager + ":2") != null){
                mAdapter.ft = mAdapter.fm.beginTransaction();
                mAdapter.ft.detach(mAdapter.fm.findFragmentByTag("android:switcher:" + R.id.pager + ":2"));
                mAdapter.ft.commit();
            } else if(position == 2 && mAdapter.fm.findFragmentByTag("android:switcher:" + R.id.pager + ":2") == null){
                mAdapter.ft = mAdapter.fm.beginTransaction();
                Fragment myFragment = (MapaFragment) Fragment.instantiate(MainActivity.this, MapaFragment.class.getName());
                mAdapter.ft.attach(myFragment);
                mAdapter.ft.commit();
            } else if(position == 1 && mAdapter.fm.findFragmentByTag("android:switcher:" + R.id.pager + ":2") != null){
                mAdapter.ft = mAdapter.fm.beginTransaction();
                mAdapter.ft.attach(mAdapter.fm.findFragmentByTag("android:switcher:" + R.id.pager + ":" + position + 1 ));
                mAdapter.ft.commit();
            }
        }
    };       
    mPager.setOnPageChangeListener(pageChangeListener);

我的适配器

    public static class MyAdapter extends FragmentPagerAdapter {

    public FragmentManager fm;
    public FragmentTransaction ft;

    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        this.fm = fragmentManager;

    }

    @Override
    public int getCount() {
        return 3;
    }


    @Override
    public Fragment getItem(int position) {

        switch (position) {     
        case 0:
            return new RubroFragment();         
        case 1:
            return new ListOfertaFragment();
        case 2:
            return new MapaFragment();
        default:
            return null;
        }
    }
}

和我的地图:

public class MapaFragment extends SupportMapFragment implements
    ConnectionCallbacks, OnConnectionFailedListener, LocationListener,
    OnMyLocationButtonClickListener {


private LocationManager lm;
private Location loc;
private List<Ubicacion> listPositions;
private GoogleMap mMap;
private SaltaApplication app;
private LocationClient mLocationClient;

private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000) // 5 seconds
        .setFastestInterval(16) // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    app = (SaltaApplication) getActivity().getApplication();
    listPositions = app.getUbicaciones();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
            mLocationClient = new LocationClient(getActivity().getApplicationContext(),
                    this,  // ConnectionCallbacks
                    this); // OnConnectionFailedListener
        }
}

protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onResume() {
    super.onResume();
    mMap = this.getMap();
    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationButtonClickListener(this);
    setUpMapIfNeeded();
    setUpLocationClientIfNeeded();
    centrar_plano();
    mLocationClient.connect();
}

@Override
public void onPause() {
    super.onPause();
    if (mLocationClient != null) {
        mLocationClient.disconnect();
    }
    Log.v("TAG", "onPause");
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = this.getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
               UiSettings settings = getMap().getUiSettings();

                settings.setAllGesturesEnabled(false);
                settings.setMyLocationButtonEnabled(false);
                settings.setCompassEnabled(false);
                settings.setScrollGesturesEnabled(false);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

参考How to implement google maps api v2 in SherlockMapActivity

insck of ShareLockMapActivity使用SherlockMapFragment

import android.app.Activity;
import android.support.v4.app.Watson.OnCreateOptionsMenuListener;
import android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import android.support.v4.app.Watson.OnPrepareOptionsMenuListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.SupportMapFragment;

public class SherlockMapFragment extends SupportMapFragment implements
        OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener,
        OnOptionsItemSelectedListener {
    private SherlockFragmentActivity mActivity;

    public SherlockFragmentActivity getSherlockActivity() {
        return mActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof SherlockFragmentActivity)) {
            throw new IllegalStateException(getClass().getSimpleName()
                    + " must be attached to a SherlockFragmentActivity.");
        }
        mActivity = (SherlockFragmentActivity) activity;

        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }

    @Override
    public final void onCreateOptionsMenu(android.view.Menu menu,
            android.view.MenuInflater inflater) {
        onCreateOptionsMenu(new MenuWrapper(menu),
                mActivity.getSupportMenuInflater());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Nothing to see here.
    }

    @Override
    public final void onPrepareOptionsMenu(android.view.Menu menu) {
        onPrepareOptionsMenu(new MenuWrapper(menu));
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        // Nothing to see here.
    }

    @Override
    public final boolean onOptionsItemSelected(android.view.MenuItem item) {
        return onOptionsItemSelected(new MenuItemWrapper(item));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Nothing to see here.
        return false;
    }
}

如何使用上面的课程:

public class MyMapFragment extends SherlockMapFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getMap() != null) {
            Log.e(getClass().getSimpleName(), "Map ready for use!");
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(
                    23.0333, 72.6167));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);

            getMap().moveCamera(center);
            getMap().animateCamera(zoom);

            addMarker(getMap(), Double.parseDouble(latlong[0]),
                        Double.parseDouble(latlong[1]), name, "" + sni);



        }

    }

    private void addMarker(GoogleMap map, double lat, double lon,
            String string, String string2) {
        map.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lon))
                .title(string)
                .snippet(string2)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    }
}

在以下任何活动中添加以上片段:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.dj_android.MyMapFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

答案 1 :(得分:0)

是的确的答案是对的,但我的问题依赖于此

public class DepthPageTransformer implements ViewPager.PageTransformer {
private static float MIN_SCALE = 0.75f;

public void transformPage(View view, float position) {
    int pageWidth = view.getWidth();

    if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
        view.setAlpha(0);

    } else if (position <= 0) { // [-1,0]
        // Use the default slide transition when moving to the left page
        view.setAlpha(1);
        view.setTranslationX(0);
        view.setScaleX(1);
        view.setScaleY(1);

    } else if (position <= 1) { // (0,1]
        // Fade the page out.
        view.setAlpha(1 - position);

        // Counteract the default slide transition
        view.setTranslationX(pageWidth * -position);

        // Scale the page down (between MIN_SCALE and 1)
        float scaleFactor = MIN_SCALE
                + (1 - MIN_SCALE) * (1 - Math.abs(position));
        view.setScaleX(scaleFactor);
        view.setScaleY(scaleFactor);

    } else { // (1,+Infinity]
        // This page is way off-screen to the right.
        view.setAlpha(0);
    }
}

}