ExpandableListView не отображается внутри - PullRequest
0 голосов
/ 04 апреля 2020

У меня есть простое действие с RelativeLayout, и внутри этого макета у него есть ExpandableListView. Я настроил свою активность как таковую, и в ней есть внутренний класс, который является адаптером для ExpandableListView. Я не уверен, почему при выполнении действия отображаются другие макеты, но мой ExpandableListView не отображается. Я убедился, что в адаптере нет значений 0 или null. Любые идеи, в чем может быть проблема или что может вызвать эту проблему для тех, кто реализует ExpandableListView?

package com.marinoo.familymap.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.marinoo.familymap.R;
import com.marinoo.familymap.cmodel.FamilyTree;
import com.marinoo.familymap.model.Event;
import com.marinoo.familymap.model.Person;
import java.util.ArrayList;
import java.util.List;

public class PersonActivity extends AppCompatActivity {
    private String selectedPersonID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        selectedPersonID = getIntent().getExtras().getString("selectedPersonID");
        setContentView(R.layout.activity_person);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Family Map: Person Details");
        setTextViews();

        ExpandableListView expandableListView = findViewById(R.id.expandableListView);
        List<Event> selectedPersonEvents = FamilyTree.getInstance().getPeoplesEventsMap().get(selectedPersonID);
        List<Person> selectedPersonFamilyMembers = getSelectedPersonFamilyMembers();

        expandableListView.setAdapter(new ExpandableListAdapter(selectedPersonEvents, selectedPersonFamilyMembers));
    }

    private class ExpandableListAdapter extends BaseExpandableListAdapter {

        private static final int EVENTS_GROUP_POSITION = 0;
        private static  final int FAMILY_MEMBERS_GROUP_POSITION = 1;

        private final List<Event> events;
        private final List<Person> familyMembers;

        ExpandableListAdapter(List<Event> events, List<Person> familyMembers) {
            this.events = events;
            this.familyMembers = familyMembers;
        }

        @Override
        public int getGroupCount() {
            return 2;
        }

        @Override
        public int getChildrenCount(int groupPosition) {

            switch (groupPosition) {
                case EVENTS_GROUP_POSITION:
                    return events.size();
                case FAMILY_MEMBERS_GROUP_POSITION:
                    return familyMembers.size();
                default:
                    throw new IllegalArgumentException("Unrecognized group position: " + groupPosition);
            }
        }

        @Override
        public Object getGroup(int groupPosition) {

            switch (groupPosition) {
                case EVENTS_GROUP_POSITION:
                    return getString(R.string.lifeEventsTitle);
                case FAMILY_MEMBERS_GROUP_POSITION:
                    return getString(R.string.familyTitle);
                default:
                    throw new IllegalArgumentException("Unrecognized group position: " + groupPosition);
            }
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {

            switch (groupPosition) {
                case EVENTS_GROUP_POSITION:
                    return events.get(childPosition);
                case  FAMILY_MEMBERS_GROUP_POSITION:
                    return familyMembers.get(childPosition);
                default:
                    throw new IllegalArgumentException("Unrecognized group position: " + groupPosition);
            }
        }

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

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_person_details_group, parent, false);
            }

            TextView title = convertView.findViewById(R.id.listTitle);

            switch (groupPosition) {
                case EVENTS_GROUP_POSITION:
                    title.setText(R.string.lifeEventsTitle);
                    break;
                case FAMILY_MEMBERS_GROUP_POSITION:
                    title.setText(R.string.familyTitle);
                    break;
                default:
                    throw new IllegalArgumentException("Unrecognized group position: " + groupPosition);
            }

            return convertView;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            View itemView;

            switch (groupPosition) {
                case EVENTS_GROUP_POSITION:
                    itemView = getLayoutInflater().inflate(R.layout.life_events_item, parent, false);
                    initializeLifeEventsView(itemView, childPosition);
                    break;
                case FAMILY_MEMBERS_GROUP_POSITION:
                    itemView = getLayoutInflater().inflate(R.layout.family_members_item, parent, false);
                    initializeFamilyMembersView(itemView, childPosition);
                    break;
                default:
                    throw new IllegalArgumentException("Unrecognized group position: " + groupPosition);
            }

            return itemView;
        }

        @SuppressLint("SetTextI18n")
        private void initializeLifeEventsView(View lifeEventsView, int childPosition) {

            Event event = events.get(childPosition);
            Person person = FamilyTree.getInstance().getPeopleMap().get(event.getPersonID());

            TextView eventDetails = lifeEventsView.findViewById(R.id.event);
            eventDetails.setText(event.getEventType().toUpperCase() + ": " + event.getCity() +
                    ", " + event.getCountry() + " (" + event.getYear() + ")");

            TextView personName = lifeEventsView.findViewById(R.id.eventsPersonName);
            assert person != null;
            personName.setText(person.getFirstName() + " " + person.getLastName());

            lifeEventsView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //map activity happens
                }
            });


        }

        @SuppressLint("SetTextI18n")
        private void initializeFamilyMembersView(View familyMembersView, int childPosition) {

            Person selectedPerson = FamilyTree.getInstance().getPeopleMap().get(selectedPersonID);
            Person familyMember = familyMembers.get(childPosition);

            TextView familyFullName = familyMembersView.findViewById(R.id.familyFullName);
            familyFullName.setText(familyMember.getFirstName() + " " + familyMember.getLastName());

            TextView familyMemberRelationship = familyMembersView.findViewById(R.id.familyRelationship);
            if (selectedPerson.getFatherID().equals(familyMember.getPersonID())) {
                familyMemberRelationship.setText("Father");

            } else if (selectedPerson.getMotherID().equals(familyMember.getPersonID())) {
                familyMemberRelationship.setText("Mother");

            } else if (selectedPerson.getSpouseID().equals(familyMember.getPersonID())) {
                familyMemberRelationship.setText("Spouse");

            } else {
                familyMemberRelationship.setText("Child");
            }

            familyMembersView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Start person activity
                }
            });
        }

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

    @SuppressLint("SetTextI18n")
    private void setTextViews() {

        TextView firstName = findViewById(R.id.pActivityFirstName);
        TextView lastName = findViewById(R.id.pActivityLastName);
        TextView gender = findViewById(R.id.pActivityGender);
        Person selectedPerson;
        selectedPerson = FamilyTree.getInstance().getPeopleMap().get(selectedPersonID);

        assert selectedPerson != null;
        firstName.setText(selectedPerson.getFirstName());
        lastName.setText(selectedPerson.getLastName());

        if (selectedPerson.getGender().equals("f")) {
            gender.setText("Female");

        } else {
            gender.setText("Male");
        }
    }

    private ArrayList<Person> getSelectedPersonFamilyMembers() {

        ArrayList<Person> familyMembers = new ArrayList<>();
        Person selectedPerson = FamilyTree.getInstance().getPeopleMap().get(selectedPersonID);

        if (FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getFatherID()) != null) {
            familyMembers.add(FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getFatherID()));
        }

        if (FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getMotherID()) != null) {
            familyMembers.add(FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getMotherID()));
        }

        if (FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getSpouseID()) != null) {
            familyMembers.add(FamilyTree.getInstance().getPeopleMap().get(selectedPerson.getSpouseID()));
        }

        if (FamilyTree.getInstance().getChildrenMap().get(selectedPersonID) != null) {
            ArrayList<Person> children = FamilyTree.getInstance().getChildrenMap().get(selectedPersonID);
            familyMembers.addAll(children);
        }

        return  familyMembers;
    }
}

Макет:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_person"
    android:layout_margin="15dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.PersonActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:id="@+id/pActivityFirstName" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:text="@string/pActivityFirstName"
                    android:textSize="12sp" />

            </LinearLayout>

            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:background="@color/gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:id="@+id/pActivityLastName" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:text="@string/pActivityLastName"
                    android:textSize="12sp" />

            </LinearLayout>

            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:background="@color/gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:id="@+id/pActivityGender" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/gray"
                    android:text="@string/pActivityGender"
                    android:textSize="12sp" />

            </LinearLayout>

            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:background="@color/gray" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ExpandableListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/expandableListView"
                android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
                android:dividerHeight="0.5dp"
                android:divider="@color/gray"
                tools:context=".ui.PersonActivity" />

        </LinearLayout>



    </LinearLayout>

</RelativeLayout>

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

    <TextView
        android:id="@+id/listTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingTop="10dp"
        android:paddingEnd="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingBottom="10dp"
        android:textAllCaps="true"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>

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

    <TextView
        android:id="@+id/event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"
        android:textSize="18sp"
        android:layout_marginLeft="8dp"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/eventsPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/event"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@id/event" />

</RelativeLayout>

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

    <TextView
        android:id="@+id/familyFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"
        android:textSize="18sp"
        android:layout_marginLeft="8dp"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/familyRelationship"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/familyFullName"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@id/familyFullName" />

</RelativeLayout>
...