可扩展ListView不扩展

时间:2016-11-28 14:59:19

标签: android expandablelistview

我有一个带有一些TextView和图像的ExpandleListview。 在cliked之后,我想将showTextView作为子节点并在GroupView中隐藏另一个TextView。不幸的是,列表显示正确,但点击项目后它根本没有扩展,当我设置OnGroupClickListener并尝试调试它时,断点不会触发。

XML

<<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/notificationItem"
    android:orientation="vertical"
    android:padding="15sp"
    android:animateLayoutChanges="true"
    >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_gravity="start"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/notificationLogo"
        android:adjustViewBounds="true"
        android:layout_alignParentLeft="true"
        android:clickable="false"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="20sp"
        android:paddingLeft="10sp"
        android:layout_toRightOf="@+id/notificationLogo"
        android:orientation="vertical"
        android:id="@+id/headerContent">

        <TextView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/date"/>
    <TextView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventName"/>

    <TextView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/shortDescription"
        android:ellipsize="end"
        android:maxLines="2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fullDescription"
            android:layout_gravity="center"/>
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/downButton"
        android:layout_centerInParent="true"
        android:layout_below="@+id/headerContent"
        android:background="@android:color/transparent"/>
</RelativeLayout>
</LinearLayout>

ADAPTER CLASS

public class NotificationAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ViewHolder viewHolder;
    final Typeface typeface;
    List<MuseumNotification> notificationList;

    public NotificationAdapter(Context context, List<MuseumNotification>notificationList){
        mContext = context;
        typeface = Typeface.createFromAsset(mContext.getAssets(),"fonts/fontawesome-webfont.ttf");
        this.notificationList =notificationList;
    }

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

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

    @Override
    public Object getGroup(int groupPosition) {
        return notificationList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return notificationList.get(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(viewHolder==null)
            viewHolder =new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.notification_item,parent,false);

        viewHolder.notificationTitle=(TextView)convertView.findViewById(R.id.eventName);
        viewHolder.shortDescription=(TextView)convertView.findViewById(R.id.shortDescription);
        viewHolder.image_url=(ImageView)convertView.findViewById(R.id.notificationLogo);
       viewHolder.notificationTitle.setText(notificationList.get(groupPosition).getTitle());
        viewHolder.shortDescription.setText(notificationList.get(groupPosition).getText());
        Glide.with(mContext)
                .load("www.logo.png")
                .fitCenter()
                .into(viewHolder.image_url);
        viewHolder.navDown=(Button)convertView.findViewById(R.id.downButton);
        viewHolder.navDown.setTypeface(typeface);
        viewHolder.navDown.setText(R.string.navDown);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.notification_item,parent,false);
        viewHolder.description=(TextView)convertView.findViewById(R.id.fullDescription);
        viewHolder.description.setText(notificationList.get(groupPosition).getText());
        return convertView;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    private class ViewHolder{
        private TextView notificationTitle,description,shortDescription;
        private ImageView image_url;
        private Button navDown;
    }
}

编辑:已解决:在组项目中我有一个可点击的元素(按钮)。它应该是不可聚焦的(android:focusable =&#34; false&#34;在XML中)

2 个答案:

答案 0 :(得分:0)

从您提到的代码中,我认为您尚未在代码中定义HashMap<>()。您需要定义HashMap,其中已将'group_title'存储为key并且'child title'为value。通过使用所需的getChild(int groupPosition, int childPosition)(键)访问hashmap的特定值,替换groupPosition方法中的代码。

对于例如 - 如果您将HashMap作为 - HashMap<>(MuseumNotification, String) hm; 然后在getChild(int groupPosition, int childPosition)方法中编写以下代码

  return this.hm.get(this.notificationList.get(groupPosition))
                .get(childPosition);

此处,notificationList是您在代码中定义的List。这将为您提供特定groupPosition所需的子项。

您的代码将在Group中返回getChild(int groupPosition, int childPosition)(对象),并且您需要Child(对象)

答案 1 :(得分:0)

samarth kejriwal 你的answear并没有解决问题但是当我尝试在onCreate方法中以编程方式打开项目时,它正在扩展(expandleListView.expandGroup(position);}我尝试了expandleListView.setOnItemClickListener但是它不起作用。我可以滚动我的列表,只有当我点击它时才会发生任何事情。