在Android Studio中使用列表项选择地图标记

时间:2018-08-04 12:29:29

标签: android expandablelistview

我正在使用SupportMapFragment开发一个应用程序,我使用ExpandableListView来获取标记信息。它简单易用,没有任何问题。但是我想要当我单击列表项或列表标题视图时,自动将焦点集中到单击项的标记上。谁遇到过这种情况?

//“我的位置”课程

public class Location extends FragmentActivity implements OnMapReadyCallback 
{

    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        SupportMapFragment mapFragment = (SupportMapFragment) 
  getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        ExpandableListView listView = (ExpandableListView) 
 findViewById(R.id.location_view);
        initData();
        LocationListAdapter listAdapter = new LocationListAdapter(this, 
listDataHeader, listHash);
        listView.setAdapter(listAdapter);
    }

    private void initData(){
        listDataHeader = new ArrayList<>();
        listHash = new HashMap<>();

        listDataHeader.add("Bla");
        listDataHeader.add("Blabla");

        List<String> Mərkəz = new ArrayList<>();
        Mərkəz.add(getResources().getString(R.string.bla));

        List<String> Şərur = new ArrayList<>();
        Şərur.add(getResources().getString(R.string.blabla));

        listHash.put(listDataHeader.get(0),Bla);
        listHash.put(listDataHeader.get(1),Blabla);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        GoogleMap mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        LatLng Bla= new LatLng(39.212806, 45.405997);
        mMap.addMarker(new MarkerOptions().position(Bla).title("Bla"))


  .setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) 
   ;

        LatLng Blabla= new LatLng(39.553633, 44.984568);
        mMap.addMarker(new MarkerOptions().position(Blabla).title("Blabla"))
                .setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
    }}

//我的位置xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    tools:context=".Location"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="5dp"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/location_view"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">
    </ExpandableListView>
</LinearLayout>

// LocationListAdapter

public class LocationListAdapter  extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String >> listHashMap;

    public LocationListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap){
        this.context=context;
        this.listDataHeader=listDataHeader;
        this.listHashMap=listHashMap;
    }

    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return listHashMap.get(listDataHeader.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return listDataHeader.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return listHashMap.get(listDataHeader.get(i)).get(i1); // i= child item, i1= group item
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String headerTitle = (String)getGroup(i);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.locationheader,null);
        }
        TextView list1header =  (TextView)view.findViewById(R.id.locationheader);
        list1header.setTypeface(null, Typeface.BOLD);
        list1header.setText(headerTitle);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final String childText = (String)getChild(i,i1);
        if (view== null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.locationlist,null);
        }
        TextView txtListChild = (TextView) view.findViewById(R.id.location_item);
        txtListChild.setTypeface(null, Typeface.ITALIC);
        txtListChild.setText(childText);
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (observer != null) {
            super.unregisterDataSetObserver(observer);
        }
    }}

// locationheader xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp"
        android:textStyle="normal"
        android:paddingLeft="? 
        android:attr/expandableListPreferredItemPaddingLeft"
        android:id="@+id/locationheader"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"/>
</LinearLayout>

//位置列表xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:paddingLeft="? 
        android:attr/expandableListPreferredChildPaddingLeft"
        android:id="@+id/location_item"
        android:layout_gravity="center"
        android:padding="5dp"
        android:layout_marginTop="5dp"/>
</LinearLayout>

0 个答案:

没有答案