Android:ExpandableListView Childs的水平滚动

时间:2016-02-29 14:11:30

标签: android expandablelistview

我必须为ExpandableListView的子元素实现水平滚动视图,现在子元素以垂直方向显示。

<ExpandableListView android:id="@+id/myGroupListView"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:divider="@color/navDrawerIcon"
                    android:dividerHeight="1dp" />

item_group.xml

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


    <ImageView
        android:id="@+id/userImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:src="@drawable/ic_account_circle_808088_36dp"
        android:paddingTop="5dp"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="20dp" />

    <TextView
        android:id="@+id/groupMemberList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Group Member"
        android:gravity="left"
        android:paddingTop="15dp"
        android:paddingBottom="10dp"
        android:paddingLeft="50dp"
        android:paddingRight="20dp"
        android:textColor="@color/primary" />

</RelativeLayout>

item_group_child.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/sharedCardImage"
        android:layout_width="@dimen/image_card_xsmall_width"
        android:layout_height="@dimen/image_card_xsmall_height"/>

    <TextView
        android:id="@+id/sharedBalAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/sharedCardImage"
        android:text="TextView"
        android:textSize="@dimen/text_size_small"
        android:textColor="@color/primary"
        android:textAlignment="center"
        android:gravity="center" />


</LinearLayout>

我尝试将item_group_child.xml LinearLayout方向更改为水平,但没有运气。

item_group.xml应以垂直方式显示,有效,item_group_child.xml应为水平方式,因此用户可以水平滚动子项。有没有办法实现这个目标?

3 个答案:

答案 0 :(得分:0)

没有库存ExpandableListViewExpandableListView只是一个常规的垂直ListView,其中ExpandableListAdapter根据群组展开/展开事件处理添加/删除列表项。

如果您想要水平滚动子项,那么您需要设置ExpandableListAdapter,以便每个组只有一个可能的子视图,并且在创建子视图时,它有一个水平滚动视图,其中包含所有小组的孩子。

答案 1 :(得分:0)

是的,您可以使用CustomHorizontalListView实现此目的。见link。只需在getChildView()上实施即可。我只是从上面的链接自定义horizontalListView。看下面的代码: 这是您的group_child_item.xml

<com.customlayout.HorizontalListView 
    android:id="@+id/horizontalListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="@color/gray_d7"
    android:padding="20dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

</com.customlayout.HorizontalListView>

现在将上面的xml扩展到getChildView()

@Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastchild, View convertView, ViewGroup parent) 
    {
        ChildHolder childHolder = null;
        if (convertView == null) 
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_child_item, null, false);
            childHolder = new ChildHolder();
            childHolder.horizontalListView = (HorizontalListView) convertView.findViewById(R.id.horizontalListView);

            convertView.setTag(childHolder);
        }
        else
        {
            childHolder = (ChildHolder) convertView.getTag();
        }

HorizontalListViewAdapter horizontalListAdapter = new ModuleRoomHorizontalListAdapter(context, groupList, groupPosition);
        ChildHolder.horizontalListView.setAdapter(horizontalListAdapter);

        return convertView;
    }


    private static class ChildHolder
    {
        HorizontalListView horizontalListView;
    }

现在为horizo​​ntalListViewChild实现item_group_child.xml文件并在getView HorizontalListAdapter

中充气
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/sharedCardImage"
        android:layout_width="@dimen/image_card_xsmall_width"
        android:layout_height="@dimen/image_card_xsmall_height"/>

    <TextView
        android:id="@+id/sharedBalAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/sharedCardImage"
        android:text="TextView"
        android:textSize="@dimen/text_size_small"
        android:textColor="@color/primary"
        android:textAlignment="center"
        android:gravity="center" />


</LinearLayout>

Horizo​​ntalListAdapter:

public class HorizontalListAdapter extends BaseAdapter
{
    private Context mContext;
    private ArrayList<Groups> mGroupListList;
    private int mGroupPosition;



    public HorizontalListAdapter(Context context, ArrayList<Groups> mGroupListList, int mGroupPosition) 
    {
        this.mContext = context;
        this.mGroupListList = mGroupListList;
        this.mGroupPosition = mGroupPosition;

    }


    @Override
    public int getCount() 
    {
        return mGroupListList.get(mGroupPosition).getChildList().size();
    }


    @Override
    public Object getItem(int position) 
    {
        return mGroupListList.get(mGroupPosition).getChildList().get(position);
    }


    @Override
    public long getItemId(int position) 
    {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        ViewHolder viewHolder;
        if (convertView == null) 
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_group_child, null, false);
            viewHolder = new ViewHolder();

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // Implement Your code here what functionality you need.

        return convertView;
    }

    private static class ViewHolder
    {
        TextView txtName;

    }
}

希望它对你有所帮助。如果您有任何问题,请告诉我

答案 2 :(得分:0)

在@Sam想法的帮助下,使用RecyclerView完成了ExpandableListView的子元素的水平滚动,没有任何第三方插件。

演示应用:https://github.com/sivailango/ExpandableListView-RecylerChildItems