【问题标题】:Implement Divider between the last item of the child and next Groupheader expandableListview在子项的最后一项和下一个 Groupheader expandableListview 之间实现 Divider
【发布时间】:2013-12-30 10:23:24
【问题描述】:

我已经在 android app 中实现了可扩展列表视图,并在其中实现了分隔符。

我有一个问题是没有得到子项的最后一项和下一个组标题之间的分隔符。

下面的图片是我想要的:

但以下是我得到的:

如果您比较两个图像,CID 和 About Set 之间的分隔符不会出现如何实现该分隔符?

尽管在包含项目标记的 groupindicator 中的 xml 中提供了 android:state_expanded 和 android:state_empty 中的 2 个不同图像,但 groupIndicator 也没有改变。

但是没有出现android:state_expanded和android:state_empty的属性。

【问题讨论】:

    标签: android expandablelistview divider


    【解决方案1】:

    很容易实现。
    1- 我们将在 groub_item_layout 和 child_item_layout 中添加分隔线:
    group_item_layout :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:minHeight="50dp"
        android:orientation="vertical">
    
        <View
            android:id="@+id/adapter_divider_top"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#B6B6B6" />
    
        <TextView
            android:id="@+id/tv_adapter_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingBottom="10dp"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:text="Home"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/primary_text" />
    
    </LinearLayout>
    


    child_item_layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_adapter_desc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:text="description"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#212121" />
    
        <View
            android:id="@+id/adapter_divider_bottom"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divider" />
    </LinearLayout>
    


    2- 在您的适配器 getChildView()getGroupView() 方法中:

     @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
    
            if (view == null) {
    
                view = mInflater.inflate(R.layout.group_item_layout, null);
    
                mGroupViewHolder = new GroupViewHolder();
    
                mGroupViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_adapter_title);
    
                mGroupViewHolder.dividerTop = view.findViewById(R.id.adapter_divider_top);
    
                view.setTag(mGroupViewHolder);
    
            } else {
    
                mGroupViewHolder = (GroupViewHolder) view.getTag();
    
            }
    
            // That if you want to show divider in expanded group only.
            // If you want to show divider in all group items remove this block.
            if (isExpanded) {
    
                mGroupViewHolder.dividerTop.setVisibility(View.VISIBLE);
    
            } else {
    
                mGroupViewHolder.dividerTop.setVisibility(View.INVISIBLE);
    
            }
            // That if you want to show divider in expanded group only.
    
            String myTitle = getGroup(groupPosition);
    
            if(myTitle != null) {
    
                mGroupViewHolder.tvTitle.setText(myTitle);
    
            }
    
            return view;
        }
    


    @Override
        public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
    
    
            if (view == null) {
    
                view = mInflater.inflate(R.layout.child_item_layout, null);
    
                mChildViewHolder = new ChildViewHolder();
    
                mChildViewHolder.tvDesc = (TextView) view.findViewById(R.id.tv_adapter_desc);
    
                mChildViewHolder.dividerBottom = view.findViewById(R.id.adapter_divider_bottom);
    
                view.setTag(mChildViewHolder);
    
            } else {
    
                mChildViewHolder = (ChildViewHolder) view.getTag();
    
            }
    
            if(isLastChild) {
    
                mChildViewHolder.dividerBottom.setVisibility(View.VISIBLE);
    
            } else {
    
                mChildViewHolder.dividerBottom.setVisibility(View.INVISIBLE);
    
            }
    
            Timesheet timesheet = getChild(groupPosition, childPosition);
    
            if(timesheet != null) {
    
                mChildViewHolder.tvDesc.setText(timesheet.getDescription());
    
            }
    
    
            return view;
        }
    

    希望这对任何人都有帮助。

    【讨论】:

      【解决方案2】:

      您可以制作自己的分隔线,而不是使用 ExpandableListView 的分隔线。将列表的父元素的背景设置为

      android:background="@drawable/top_divider"
      

      drawable/top_divider.xml 包含的地方

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:left="-1dp" android:right="-1dp" android:bottom="-1dp">
            <shape>
                  <solid android:color="@android:color/transparent" />
                  <stroke android:width="1dp" android:color="@android:color/black" />
            </shape>
          </item>
      </layer-list>
      

      【讨论】:

        【解决方案3】:

        首先制作自定义可扩展列表视图... 然后将不同的分隔符添加到标题及其子项

        参考代码(可扩展列表视图xml的标题):-

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#000000" >
        
            <ImageView
                android:id="@+id/divider_top"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/divider_vertical" />
        
            <TextView
                android:id="@+id/lblListHeader"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/divider_top"
                android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
                android:textColor="#c5e26d"
                android:textSize="17dp" />
        
            <ImageView
                android:id="@+id/divider_bottom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lblListHeader"
                android:layout_centerHorizontal="true"
                android:background="@drawable/divider" />
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-15
          • 2013-08-13
          • 1970-01-01
          • 1970-01-01
          • 2019-01-06
          • 1970-01-01
          • 2017-07-06
          • 2019-08-08
          相关资源
          最近更新 更多