可扩展列表项单击问题:Android 7.0

时间:2017-07-26 14:28:45

标签: android expandablelistview android-7.0-nougat

我有自定义的可扩展列表视图。

我已使用contentView.onClick在第二级列表项上获取项目点击事件。

它适用于所有设备< 7.0

但对于7.0,它不起作用。我试过模拟器,发现使用Up-Down键试图移动焦点和项目(第二级)获得点击。但在Android设备7.0中

我可以点击第一级项目,但第二级项目点击会出现问题。

第一级适配器:

public class OrgStructureExpandableAdapter extends BaseExpandableListAdapter
{
    private Context context;
    private LayoutInflater layoutInflater;

    private OnOrganizationCheckedListener groupCheckedListener;

    public Entry[] lsfirst;

    private OrgStructureSecondLevelExpandableAdapter.OnOrganizationCheckedListener teamCheckedListener;

    private ArrayList<Object> alOrgs;

    public OrgStructureExpandableAdapter(Context context, ArrayList<Object> alOrgs, OnOrganizationCheckedListener groupCheckedListener, OrgStructureSecondLevelExpandableAdapter.OnOrganizationCheckedListener teamCheckedListener)
    {
        this.context = context;
        this.alOrgs = alOrgs;
        this.groupCheckedListener = groupCheckedListener;
        this.teamCheckedListener = teamCheckedListener;
        layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        lsfirst = new Entry[alOrgs.size()];

        for (int i = 0; i < alOrgs.size(); i++)
        {
            final CustExpListview celv = new CustExpListview(context);
            OrgStructureSecondLevelExpandableAdapter adp = new OrgStructureSecondLevelExpandableAdapter(context, i, alOrgs.get(i), teamCheckedListener);
            celv.setAdapter(adp);
            celv.setGroupIndicator(null);

//            for ( int j = 0; j < adp.getGroupCount(); j++ )
//            {
//                celv.expandGroup(j);
//            }

            lsfirst[i] = new Entry(celv, adp);
        }
    }

    @Override
    public int getGroupCount()
    {
        if ( alOrgs != null )
        {
            if ( isForOrganization )
                return alOrgs.size() + 1;
            else
                return alOrgs.size();
        }
        else if ( isForOrganization )
            return 1;
        else
            return 0;
    }

    @Override
    public int getChildrenCount( int groupPosition )
    {
        return 1;
    }

    @Override
    public Object getGroup( int groupPosition )
    {
        return null;
    }

    @Override
    public Object getChild( int groupPosition, int childPosition )
    {
        return null;
    }

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

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

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

    @Override
    public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent )
    {
        if ( alOrgs != null && alOrgs.size() > groupPosition )
            convertView = layoutInflater.inflate(R.layout.row_organization_structure, null);
        else
            convertView = layoutInflater.inflate(R.layout.row_add_school_structure, null);

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                //do something onClick
            }
        });

        convertView.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick( View v )
            {
                //do something onLongClick

                return false;
            }
        });

        convertView.setTag(null);

        //Bind UI stuff here

        return convertView;
    }

    @Override
    public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent )
    {
        return lsfirst[groupPosition].cls;
    }

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

    public interface OnOrganizationCheckedListener
    {
        public void onGroupCheckChange( boolean isChecked, int groupPosition );

        public void onTeamCheckChange( boolean isChecked, int groupPosition, int childPosition );

        public void expandCollapseGroup(int groupPosition);

        public void addSchool();

        public void unLinkOrg(int groupPosition, View view);
    }

    public class Entry
    {
        public final CustExpListview cls;
        public final OrgStructureSecondLevelExpandableAdapter sadpt;

        public Entry(CustExpListview cls, OrgStructureSecondLevelExpandableAdapter sadpt) {
            this.cls = cls;
            this.sadpt = sadpt;
        }
    }

    public Entry[] getLsfirst()
    {
        return lsfirst;
    }   

}

二级适配器:

public class OrgStructureSecondLevelExpandableAdapter extends BaseExpandableListAdapter
{
    private Context context;
    private LayoutInflater layoutInflater;

    private ArrayList<Object> alTeams;

    private OnOrganizationCheckedListener groupCheckedListener;

    private int orgPosition;
    private OrganizationalStructureBean.Orgs selectedOrg;

    public OrgStructureSecondLevelExpandableAdapter( Context context, int orgPosition, OrganizationalStructureBean.Orgs selectedOrg, OnOrganizationCheckedListener groupCheckedListener )
    {
        this.context = context;
        this.orgPosition = orgPosition;
        this.selectedOrg = selectedOrg;
        this.alTeams = selectedOrg.getTeams();
        this.groupCheckedListener = groupCheckedListener;
        layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount()
    {
        if ( alTeams != null )
            return alTeams.size() + 1;
        else
            return 1;
    }

    @Override
    public int getChildrenCount( int groupPosition )
    {
        if ( alTeams != null && alTeams.size() > groupPosition && alTeams.get(groupPosition).getPositions() != null && alTeams.get(groupPosition).getPositions().size() > 0 )
            return alTeams.get(groupPosition).getPositions().size() + 1;
        else if ( alTeams != null && alTeams.size() > groupPosition )
            return 1;
        else
            return 0;

    }

    @Override
    public Object getGroup( int groupPosition )
    {
        if ( alTeams != null && alTeams.size() > groupPosition )
            return alTeams.get(groupPosition);
        else
            return null;
    }

    @Override
    public Object getChild( int groupPosition, int childPosition )
    {
        if ( alTeams != null && alTeams.size() > groupPosition && alTeams.get(groupPosition).getPositions() != null && alTeams.get(groupPosition).getPositions().size() > childPosition )
            return alTeams.get(groupPosition).getPositions().get(childPosition);
        else
            return null;
    }

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

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

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

    @Override
    public View getGroupView( final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent )
    {
        if ( alTeams != null && alTeams.size() > groupPosition )
            convertView = layoutInflater.inflate(R.layout.row_team_structure, null);
        else
            convertView = layoutInflater.inflate(R.layout.row_add_team_structure, null);

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                //do some stuff onClick
            }
        });

        convertView.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick( View v )
            {
                //do some stuff onLongClick

                return false;
            }
        });

        convertView.setTag(null);

        //UI stuff

        return convertView;
    }

    @Override
    public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent )
    {
        if ( alTeams != null && alTeams.size() > groupPosition && alTeams.get(groupPosition).getPositions() != null && alTeams.get(groupPosition).getPositions().size() > childPosition )
        {
            convertView = layoutInflater.inflate(R.layout.row_position_structure, null);
        }
        else
        {
            convertView = layoutInflater.inflate(R.layout.row_add_position_structure, null);
        }

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                if ( v.getTag() == null )
                {
                    //do some stuff onClick
                }
            }
        });

        convertView.setTag(null);

        //UI stuff here..

        return convertView;
    }

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

    public interface OnOrganizationCheckedListener
    {
        public void onTeamCheckChange( boolean isChecked, int orgPosition, int teamPosition );

        public void onPositionCheckChange( boolean isChecked, int orgPosition, int teamPosition, int position );

        public void expandCollapseGroup(int orgPosition, int teamPosition);

        public void addTeam(int orgPosition, int teamPosition);

        public void addPosition(int orgPosition, int teamPosition);

        public void editRemoveTeam(View view, int orgPosition, int teamPosition);
    }

    @Override
    public void registerDataSetObserver( DataSetObserver observer )
    {
        super.registerDataSetObserver(observer);
    }

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

}

row_organization_structure.xml

<?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 = "wrap_content">

    <RelativeLayout
        android:layout_width = "match_parent"
        android:layout_height = "50dp">

    <ImageView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:id="@+id/ivOrg"
        android:background="@null"
        android:src="@drawable/ic_org_black"
        android:layout_centerVertical="true"
        android:padding="5dp"
        android:layout_marginLeft="5dp"/>

    <CheckBox
        android:layout_width = "20dp"
        android:layout_height = "20dp"
        android:background="@drawable/selector_checkbox_default_sets_reps"
        android:button="@null"
        android:checked="true"
        android:id="@+id/chkOrg"
        android:layout_toRightOf="@id/ivOrg"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="-10dp"
        android:padding="15dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivIndicator"
        android:background="@null"
        android:src="@drawable/ic_arrow_left_indicator"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

        <LinearLayout
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:orientation="vertical"
            android:id="@+id/llCount"
            android:layout_toLeftOf="@id/ivIndicator"
            android:gravity="center"
            android:layout_centerInParent="true"
            >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tvCount"
                    android:textColor = "@color/gray_txt"
                    android:textSize="16sp"
                    android:gravity="center"
                    android:layout_centerVertical="true"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ATHLETES"
                    android:textColor = "@color/gray_txt"
                    android:textSize="8sp"
                    android:gravity="center"
                    />

        </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvOrg"
        android:textColor = "@android:color/black"
        android:textSize="16sp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/chkOrg"
        android:layout_toLeftOf="@id/llCount"
        android:layout_marginLeft="10dp"
        />

    </RelativeLayout>

</RelativeLayout>

row_team_structure.xml

<?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 = "wrap_content">


    <RelativeLayout
        android:layout_width = "match_parent"
        android:layout_height = "50dp">

    <ImageView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:id="@+id/ivTeam"
        android:background="@null"
        android:src="@drawable/ic_team"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"/>

    <CheckBox
        android:layout_width = "20dp"
        android:layout_height = "20dp"
        android:background="@drawable/selector_checkbox_default_sets_reps"
        android:button="@null"
        android:checked="true"
        android:id="@+id/chkTeam"
        android:layout_toRightOf="@id/ivTeam"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="-10dp"
        android:padding="15dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivIndicator"
        android:background="@null"
        android:src="@drawable/ic_arrow_left_indicator"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

        <LinearLayout
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:orientation="vertical"
            android:id="@+id/llCount"
            android:layout_toLeftOf="@id/ivIndicator"
            android:gravity="center"
            android:layout_centerInParent="true"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvCount"
                android:textColor = "@color/gray_txt"
                android:textSize="16sp"
                android:gravity="center"
                android:layout_centerVertical="true"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ATHLETES"
                android:textColor = "@color/gray_txt"
                android:textSize="8sp"
                android:gravity="center"
                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/chkTeam"
            android:layout_toLeftOf="@id/llCount"
            android:orientation="horizontal"
            android:layout_centerVertical="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvTeam"
                android:textColor = "@android:color/black"
                android:textSize="16sp"
                android:layout_marginLeft="10dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvLevel"
                android:textColor = "@color/gray_txt"
                android:textSize="16sp"
                android:layout_marginLeft="10dp"/>

        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

帮助将不胜感激!!

感谢。

0 个答案:

没有答案