溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

expandableListView 總結

發(fā)布時間:2020-07-20 03:59:34 來源:網絡 閱讀:2333 作者:xuxiong3316 欄目:移動開發(fā)


實現(xiàn)效果圖:

expandableListView 總結

expandableListView  groupIndicator 圖片默認是在左邊,而且比較難看,而我要的是實現(xiàn)groupIndicator 在右邊自定義圖片,


  1. 換圖片



最簡單的就是直接copy 系統(tǒng)


<style name="Widget.ExpandableListView" parent="Widget.ListView">

<item name="android:groupIndicator">@android:drawable/expander_group</item>

       <item name="android:indicatorLeft">?android:attr/expandableListPreferredItemIndicatorLeft</item>

       <item name="android:indicatorRight">?android:attr/expandableListPreferredItemIndicatorRight</item>

       <item name="android:childDivider">@android:drawable/divider_horizontal_dark_opaque</item>

   </style>


看到這個沒有

<item name="android:groupIndicator">@android:drawable/expander_group</item>

我們只要把這個給換了,那 groupIndicator也就跟著變了。但是改這個有個問題顯示出來的

顯示的效果不是很好,圖片有被拉升過,系統(tǒng)自己是做了個.9圖片。我們已可以

2.做一張.9圖片

   在你eclipse 的解壓目錄下,找到\sdk\tools\

我的是D:\android-IDE-eclipse-64\adt-bundle-windows-x86-20130522\sdk\tools

在這個目錄下有一個draw9patch.bat的批處理文件。我們要用它做.9圖,雙擊直接打開。直接將你要做成.9 的原圖直接拉進工具。

3.創(chuàng)建 Indicator selector expander_group.xml 文件


<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item

       android:state_expanded="true"

       android:drawable="@drawable/up" />

   <item

       android:drawable="@drawable/down" />

</selector>

4.放一張圖片到你的res目錄下這里我直接用系統(tǒng)的

<item name="android:childDivider">@android:drawable/divider_horizontal_dark_opaque</item>

這個是一張圖片

5.在自己的style。xml里寫一個


 <style name="ExpandableListView" parent="android:Widget.ListView">

       <item name="android:groupIndicator">@drawable/expander_group</item>

       <item name="android:indicatorLeft">?android:attr/expandableListPreferredItemIndicatorLeft</item>

       <item name="android:indicatorRight">?android:attr/expandableListPreferredItemIndicatorRight</item>

       <item name="android:childDivider">@drawable/divider_horizontal_dark_opaque</item>

   </style>


這樣我們就自己定義好了expandable的style

6.加載自己的style

   <ExpandableListView

       android:id="@+id/expandableListView1"

       

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:layout_alignParentLeft="true"

       android:layout_alignParentTop="true" >

   </ExpandableListView>

7.在activity中設置 Indicator位置

expandablelistView 提供一個方法設置位置

Display dp = getWindowManager().getDefaultDisplay();

int width = dp.getWidth();

lv.setIndicatorBounds(width-50, width);


運行完結果:

expandableListView 總結

不知道什么原因還是有點拉伸,效果不是很好,但又要實現(xiàn)title圖片不回被拉伸放大這樣的效果;所以我就只能把圖標給屏蔽了,然后在groupItem的布局加一個ImageView,用ImageView自己實現(xiàn)。


   <ExpandableListView

       android:id="@+id/exlv_select_binddev"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

 android:groupIndicator="@null"

       android:childIndicator="@null"

        >

   </ExpandableListView>


這樣就把他自帶的圖標給屏蔽了,然后直接在groupitem布局加上一個ImageView,再做一個Childitem的布局,布局我直接設4個button,自由發(fā)揮了。

然后在適配器上getgroupView 設置父布局,在getChildView設置子布局。和listView的差不多。


現(xiàn)在要做的就是設置監(jiān)聽

監(jiān)聽ImageView expandableListView 的點擊事件,這里我是點圖片展開和關閉Child,長點擊expandableListView 也展開和關閉Child,點擊進入另一個界面。


監(jiān)聽ImageView的點擊事件,這里關鍵是保存position。直接寫一個內部類保存position

class ImageListener implements OnClickListener {


private int groupPosition;


public ImageListener(int groupPosition)

{

this.groupPosition = groupPosition;

}


@Override

public void onClick( View v )

{

Toast.makeText(ExpandableActivity.this, " p_w_picpath groupPosition : " + groupPosition, Toast.LENGTH_SHORT)

.show();

if (mlist.isGroupExpanded(groupPosition))//是否展開

{

mlist.collapseGroup(groupPosition);//設置關閉

}

else

{

mlist.expandGroup(groupPosition);//設置展開

}

}

}

監(jiān)聽長點擊事件

mlist.setOnItemLongClickListener(new OnItemLongClickListener() {


public boolean onItemLongClick( AdapterView<?> parent, View childView, int flatPos, long id )

{

if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP)

{

long position = ((ExpandableListView) parent).getExpandableListPosition(flatPos);

int groupPosition = ExpandableListView.getPackedPositionGroup(position);

int childPosition = ExpandableListView.getPackedPositionChild(position);

System.out.println(groupPosition + childPosition + " :groupPosition childPosition" + flatPos + " "

+ id);

if (mlist.isGroupExpanded(groupPosition))

{

mlist.collapseGroup(groupPosition);

}

else

{

mlist.expandGroup(groupPosition);

}

return true;

}

else if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD)

{

long position = ((ExpandableListView) parent).getExpandableListPosition(flatPos);

int groupPosition = ExpandableListView.getPackedPositionGroup(position);

int childPosition = ExpandableListView.getPackedPositionChild(position);

System.out.println(childPosition + " :groupPosition");

return true;

}

return false;

}

});

獲取點擊事件

mlist.setOnGroupClickListener(new OnGroupClickListener() {


@Override

public boolean onGroupClick( ExpandableListView parent, View v, final int groupPosition, long id )

{

Toast.makeText(ExpandableActivity.this, " click groupPosition : " + groupPosition + " id : " + id,

Toast.LENGTH_SHORT).show();

return true;

}

});

這樣就可以了

Child 的點擊事件那就比較簡單了,直接和ImageView的點擊事件一樣處理,或者直接寫死了,Child是橫向的,也只有4個不多。

ch.tv_choose.setOnClickListener(new OnClickListener() {


@Override

public void onClick( View v )

{

// TODO 子控件進入

onChildChick(0, FatherItem);

}

});

public void onChildChick( int childItemid, int fatherItemid )

{

// TODO 接收child

Toast.makeText(this, "  childItemid : " + childItemid + " fatherItemid : " + fatherItemid, Toast.LENGTH_SHORT)

.show();

}

這樣就搞定了

代碼:

package com.example.listviewanditemchick;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.SearchViewCompat.OnCloseListenerCompat;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
public class ExpandableActivity extends Activity {
    private ExpandableListView mlist;
    private MyExpandableadapter madapter;
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable);
        mlist = (ExpandableListView) findViewById(R.id.expandableListView1);
        madapter = new MyExpandableadapter(this);
        mlist.setAdapter(madapter);
        Display dp = getWindowManager().getDefaultDisplay();
        int width = dp.getWidth();
        mlist.setIndicatorBounds(width - 50, width);
        onClickExpandable();
    }
    private void onClickExpandable()
    {
        mlist.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick( ExpandableListView parent, View v, final int groupPosition, long id )
            {
                Toast.makeText(ExpandableActivity.this, " click groupPosition : " + groupPosition + " id : " + id,
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        //
        mlist.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick( AdapterView<?> parent, View childView, int flatPos, long id )
            {
                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP)
                {
                    long position = ((ExpandableListView) parent).getExpandableListPosition(flatPos);
                    int groupPosition = ExpandableListView.getPackedPositionGroup(position);
                    int childPosition = ExpandableListView.getPackedPositionChild(position);
                    System.out.println(groupPosition + childPosition + " :groupPosition childPosition" + flatPos + " "
                            + id);
                    if (mlist.isGroupExpanded(groupPosition))
                    {
                        mlist.collapseGroup(groupPosition);
                    }
                    else
                    {
                        mlist.expandGroup(groupPosition);
                    }
                    return true;
                }
                else if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
                {
                    long position = ((ExpandableListView) parent).getExpandableListPosition(flatPos);
                    int groupPosition = ExpandableListView.getPackedPositionGroup(position);
                    int childPosition = ExpandableListView.getPackedPositionChild(position);
                    System.out.println(childPosition + " :groupPosition");
                    return true;
                }
                return false;
            }
        });
        // 子菜單打開一個其他的就關閉
        mlist.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand( int groupPosition )
            {
                for (int i = 0; i < madapter.getGroupCount(); i++)
                {
                    if (groupPosition != i)
                    {
                        mlist.collapseGroup(i);
                    }
                }
            }
        });
    }
    public void onChildChick( int childItemid, int fatherItemid )
    {
        // TODO 接收child
        Toast.makeText(this, "  childItemid : " + childItemid + " fatherItemid : " + fatherItemid, Toast.LENGTH_SHORT)
                .show();
    }
    public void onImageChick( int groupPosition )
    {
        Toast.makeText(ExpandableActivity.this, " p_w_picpath groupPosition : " + groupPosition, Toast.LENGTH_SHORT).show();
        // if(isExpanded) p_w_picpath.setImageResource(R.drawable.up);
        // else p_w_picpath.setImageResource(R.drawable.down);
        if (mlist.isGroupExpanded(groupPosition))
        {
            mlist.collapseGroup(groupPosition);
        }
        else
        {
            mlist.expandGroup(groupPosition);
        }
    }
    class ImageListener implements OnClickListener {
        private int groupPosition;
        public ImageListener(int groupPosition)
        {
            this.groupPosition = groupPosition;
        }
        @Override
        public void onClick( View v )
        {
            Toast.makeText(ExpandableActivity.this, " p_w_picpath groupPosition : " + groupPosition, Toast.LENGTH_SHORT)
                    .show();
                                                                                                          
            if (mlist.isGroupExpanded(groupPosition))
            {
                mlist.collapseGroup(groupPosition);
            }
            else
            {
                mlist.expandGroup(groupPosition);
            }
        }
    }
    class MyExpandableadapter extends BaseExpandableListAdapter {
        private Context context;
        private int Imageposition;
        private boolean expanded;
        private int FatherItem;
        public MyExpandableadapter(Context c)
        {
            this.context = c;
        }
        @Override
        public int getGroupCount()
        {
            return 10;
        }
        @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 false;
        }
        public void setTitleImage( boolean isexpandeb )
        {
        }
        @Override
        public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent )
        {
            Imageposition = groupPosition;
            groupHowd gh;
            if (convertView == null)
            {
                gh = new groupHowd();
                // convertView =
                // LayoutInflater.from(context).inflate(R.layout.item, null);
                convertView = LayoutInflater.from(context).inflate(R.layout.item_p_w_picpathbutton, null);
                gh.p_w_picpath = (ImageView) convertView.findViewById(R.id.p_w_picpathButton1);
                convertView.setTag(gh);
            }
            else
            {
                gh = (groupHowd) convertView.getTag();
            }
            expanded = isExpanded;
            if (isExpanded)
                gh.p_w_picpath.setImageResource(R.drawable.up);
            else gh.p_w_picpath.setImageResource(R.drawable.down);
            gh.p_w_picpath.setOnClickListener(new ImageListener(Imageposition));
            return convertView;
        }
        @Override
        public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView,
                ViewGroup parent )
        {
            FatherItem = groupPosition;
            childHowd ch;
            if (convertView == null)
            {
                ch = new childHowd();
                convertView = LayoutInflater.from(context).inflate(R.layout.selecte_exlv_item, null);
                ch.tv_choose = (TextView) convertView.findViewById(R.id.tv_choose);
                ch.tv_select = (TextView) convertView.findViewById(R.id.tv_select);
                ch.tv_up = (TextView) convertView.findViewById(R.id.tv_up);
                ch.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
                convertView.setTag(ch);
            }
            else
            {
                ch = (childHowd) convertView.getTag();
            }
            ch.tv_choose.setText("進入");
            ch.tv_select.setText("查看");
            ch.tv_up.setText("修改");
            ch.tv_delete.setText("刪除");
            ch.tv_choose.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick( View v )
                {
                    // TODO 子控件進入
                    onChildChick(0, FatherItem);
                }
            });
            ch.tv_select.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick( View v )
                {
                    // TODO 子控鍵查看
                    onChildChick(1, FatherItem);
                }
            });
            ch.tv_up.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick( View v )
                {
                    // TODO 子控件更新
                    onChildChick(2, FatherItem);
                }
            });
            ch.tv_delete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick( View v )
                {
                    // TODO 子控件你刪除
                    onChildChick(3, FatherItem);
                }
            });
            return convertView;
        }
        @Override
        public boolean isChildSelectable( int groupPosition, int childPosition )
        {
            // TODO Auto-generated method stub
            return true;
        }
        class groupHowd {
            ImageView p_w_picpath;
            TextView tv_name;
            TextView tv_id;
            TextView tv_state;
        }
        class childHowd {
            TextView tv_choose;
            TextView tv_select;
            TextView tv_up;
            TextView tv_delete;
        }
    }
}

groupView 布局:這里最好用relativeLayout,之前用linearlayout不能實現(xiàn)點擊p_w_picpathView沒反應回去不到事件,后面監(jiān)聽觸摸事件,那做法更麻煩,后來改用relativelayout后就可以了。

然后設置 RelativeLayout 設置 android:descendantFocusability="blocksDescendants"

要獲取點擊事件的 ImageView設置 android:focusable="false"就行了;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:padding="8dp" >
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/p_w_picpathView1"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/p_w_picpathView1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/p_w_picpathView1"
        android:layout_alignLeft="@+id/textView1"
        android:text="TextView" />
    <ImageView
        android:id="@+id/p_w_picpathButton1"
        android:scaleType="fitXY"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:src="@drawable/down"
        android:focusable="false" />
</RelativeLayout>

Child布局:

<?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="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/tv_choose"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/p_w_picpath_height"
        android:layout_weight="1"
        android:background="@drawable/btn_selector_blue"
        android:clickable="true"
        android:enabled="true"
        android:focusable="true"
        android:gravity="center"
        android:padding="4dp"
        android:text="進入"
        android:textColor="@color/white"
        android:textSize="@dimen/fontxiao" />
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="2dp"
        android:layout_height="@dimen/p_w_picpath_height"
        android:background="@color/black" />
    <TextView
        android:id="@+id/tv_select"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/p_w_picpath_height"
        android:layout_weight="1"
        android:background="@drawable/btn_selector_blue"
        android:clickable="true"
        android:enabled="true"
        android:focusable="true"
        android:gravity="center"
        android:padding="4dp"
        android:text="查看"
        android:textColor="@color/white"
        android:textSize="@dimen/fontxiao" />
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="2dp"
        android:layout_height="@dimen/p_w_picpath_height"
        android:background="@color/black"/>
    <TextView
        android:id="@+id/tv_up"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/p_w_picpath_height"
        android:layout_weight="0.96"
        android:background="@drawable/btn_selector_blue"
        android:clickable="true"
        android:enabled="true"
        android:focusable="true"
        android:gravity="center"
        android:padding="4dp"
        android:text="修改"
        android:textColor="@color/white"
        android:textSize="@dimen/fontxiao" />
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="2dp"
        android:layout_height="@dimen/p_w_picpath_height"
        android:background="@color/black"/>
    <TextView
        android:id="@+id/tv_delete"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/p_w_picpath_height"
        android:layout_weight="1"
        android:background="@drawable/btn_selector_blue"
        android:clickable="true"
        android:enabled="true"
        android:focusable="true"
        android:gravity="center"
        android:padding="4dp"
        android:text="刪除"
        android:textColor="@color/white"
        android:textSize="@dimen/fontxiao" />
</LinearLayout>


附件:http://down.51cto.com/data/2364330
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI