十載的海原網(wǎng)站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整海原建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“海原網(wǎng)站設計”,“海原網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
實現(xiàn)效果圖:
expandableListView groupIndicator 圖片默認是在左邊,而且比較難看,而我要的是實現(xiàn)groupIndicator 在右邊自定義圖片,
換圖片
最簡單的就是直接copy 系統(tǒng)
看到這個沒有
我們只要把這個給換了,那 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 文件
android:state_expanded="true" android:drawable="@drawable/up" /> android:drawable="@drawable/down" /> 4.放一張圖片到你的res目錄下這里我直接用系統(tǒng)的 這個是一張圖片 5.在自己的style。xml里寫一個 6.加載自己的style android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > 7.在activity中設置 Indicator位置 expandablelistView 提供一個方法設置位置 Display dp = getWindowManager().getDefaultDisplay(); int width = dp.getWidth(); lv.setIndicatorBounds(width-50, width); 運行完結果: 不知道什么原因還是有點拉伸,效果不是很好,但又要實現(xiàn)title圖片不回被拉伸放大這樣的效果;所以我就只能把圖標給屏蔽了,然后在groupItem的布局加一個ImageView,用ImageView自己實現(xiàn)。 android:id="@+id/exlv_select_binddev" android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" android:childIndicator="@null" > 這樣就把他自帶的圖標給屏蔽了,然后直接在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(); } 這樣就搞定了 代碼: groupView 布局:這里最好用relativeLayout,之前用linearlayout不能實現(xiàn)點擊p_w_picpathView沒反應回去不到事件,后面監(jiān)聽觸摸事件,那做法更麻煩,后來改用relativelayout后就可以了。 然后設置 RelativeLayout 設置 Child布局:
這樣我們就自己定義好了expandable的stylepackage 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;
}
}
}
android:descendantFocusability=
"blocksDescendants"
要獲取點擊事件的 ImageView設置 android:focusable=
"false"就行了;
名稱欄目:expandableListView總結
當前鏈接:http://weahome.cn/article/jdspgd.html