expandableListView 总结
實現效果圖:
expandableListView ?groupIndicator 圖片默認是在左邊,而且比較難看,而我要的是實現groupIndicator 在右邊自定義圖片,
換圖片
最簡單的就是直接copy 系統
<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也就跟著變了。但是改這個有個問題顯示出來的
顯示的效果不是很好,圖片有被拉升過,系統自己是做了個.9圖片。我們已可以
2.做一張.9圖片
? ?在你eclipse 的解壓目錄下,找到\sdk\tools\
我的是D:\android-IDE-eclipse-64\adt-bundle-windows-x86-20130522\sdk\tools
在這個目錄下有一個draw9patch.bat的批處理文件。我們要用它做.9圖,雙擊直接打開。直接將你要做成.9 的原圖直接拉進工具。
3.創建 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目錄下這里我直接用系統的
<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"
? ? ? ?style="@style/ExpandableListView"
? ? ? ?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);
運行完結果:
不知道什么原因還是有點拉伸,效果不是很好,但又要實現title圖片不回被拉伸放大這樣的效果;所以我就只能把圖標給屏蔽了,然后在groupItem的布局加一個ImageView,用ImageView自己實現。
? ?<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,自由發揮了。
然后在適配器上getgroupView 設置父布局,在getChildView設置子布局。和listView的差不多。
現在要做的就是設置監聽
監聽ImageView expandableListView 的點擊事件,這里我是點圖片展開和關閉Child,長點擊expandableListView 也展開和關閉Child,點擊進入另一個界面。
監聽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);//設置展開
}
}
}
監聽長點擊事件
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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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 接收childToast.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;}@Overridepublic 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;}@Overridepublic int getGroupCount(){return 10;}@Overridepublic int getChildrenCount( int groupPosition ){return 1;}@Overridepublic Object getGroup( int groupPosition ){return null;}@Overridepublic Object getChild( int groupPosition, int childPosition ){return null;}@Overridepublic long getGroupId( int groupPosition ){return groupPosition;}@Overridepublic long getChildId( int groupPosition, int childPosition ){return childPosition;}@Overridepublic boolean hasStableIds(){return false;}public void setTitleImage( boolean isexpandeb ){}@Overridepublic 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;}@Overridepublic 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() {@Overridepublic void onClick( View v ){// TODO 子控件進入onChildChick(0, FatherItem);}});ch.tv_select.setOnClickListener(new OnClickListener() {@Overridepublic void onClick( View v ){// TODO 子控鍵查看onChildChick(1, FatherItem);}});ch.tv_up.setOnClickListener(new OnClickListener() {@Overridepublic void onClick( View v ){// TODO 子控件更新onChildChick(2, FatherItem);}});ch.tv_delete.setOnClickListener(new OnClickListener() {@Overridepublic void onClick( View v ){// TODO 子控件你刪除onChildChick(3, FatherItem);}});return convertView;}@Overridepublic boolean isChildSelectable( int groupPosition, int childPosition ){// TODO Auto-generated method stubreturn 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不能實現點擊p_w_picpathView沒反應回去不到事件,后面監聽觸摸事件,那做法更麻煩,后來改用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" ><ImageViewandroid:id="@+id/p_w_picpathView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" /><TextViewandroid: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" /><TextViewandroid: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" /><ImageViewandroid: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" ><TextViewandroid: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" /><ImageViewandroid:id="@+id/p_w_picpathView1"android:layout_width="2dp"android:layout_height="@dimen/p_w_picpath_height"android:background="@color/black" /><TextViewandroid: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" /><ImageViewandroid:id="@+id/p_w_picpathView1"android:layout_width="2dp"android:layout_height="@dimen/p_w_picpath_height"android:background="@color/black"/><TextViewandroid: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" /><ImageViewandroid:id="@+id/p_w_picpathView1"android:layout_width="2dp"android:layout_height="@dimen/p_w_picpath_height"android:background="@color/black"/><TextViewandroid: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>轉載于:https://blog.51cto.com/xuxiong3316/1398060
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的expandableListView 总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse配置详解(包括智能提示设置
- 下一篇: jdk安装后提示错误