Android开发,用ExpandableListView+Dialog 仿Spinner

讨论学习,共同进步。
纯粹闲了没事,学习了1个月的Android开发,所以稚嫩之处敬请谅解。
用ExpandableListView+Dialog 仿Spinner,还有待完善。

在iteye上下了很多资料,今天第一帖回报这个论坛。

[img]http://dl.iteye.com/upload/attachment/467573/a2a91d38-08b8-3dc5-b1ef-71e7211a3ca6.png[/img]

主要功能:
1. 异步填充list数据,TitleBar显示正在运行的进度条(犹豫数据少,很难发现运行)
2. 类似Spinner控件的弹出式ExpandableListView选择窗口,实现记忆选择项
注意点是弹出窗要常驻内存,再次绑定数据后要重新定位上次选择项

未解决问题:
1. 由于EL Adapter的特性,当点击Group项后,上次选择的Child项无法去除背景,会和新选择的项同事存在,再次点击Group后才能去除;

Android开发,用ExpandableListView+Dialog <wbr>仿Spinner


主类


package com.Anson.ExpandableListViewDialogDemo;

import com.Anson.ExpandableListViewDialogDemo.R;

import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

public class ExpandableListViewDialog extends Activity {

private Button btnShowDialog;
private EditText editText1;

private ExpandableListView elvForDialog; //Layout expandablelist.xml 中的控件
private Dialog dialogMarketList; //弹出类似Spinner选择项的窗口
private View viewList; //设给弹出窗口的view
private View viewListLastSelected; //上次选中项的缓存view

List<String> listGroup = new ArrayList<String>();
List<List<String>> listChild = new ArrayList<List<String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//先给Activity注册界面进度条功能
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.main);

try{
editText1 = (EditText) findViewById(R.id.editText1);

btnShowDialog = (Button) findViewById(R.id.btnShowDialog);
btnShowDialog.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view)
{
dialogMarketList.show();
}
});

createExpandableListViewDialog();

//Load data in different thread from the main UI
new Thread(){
@Override
public void run(){
//显示TitleBar的进度条
setProgressBarIndeterminateVisibility(true);

//Methods need time to calculate
for (int i = 0; i < 3; i++) {
listGroup.add("Group " + i);
//Log.v("LH", "Group: " + i);

List<String> children = new ArrayList<String>();
for (int j = 0; j < 2; j++) {
children.add("Group: " + i + " | Child: " + j);
//Log.v("LH", "Group: " + i + " | Child: " + j);
}
listChild.add(children);
}
//Send message to handler
handler.sendEmptyMessage(0);
}}.start();
}
catch(Exception e)
{
showToastMessage(e.toString());
Log.v("LH", "ERROR@onCreate: " + e.toString());
}
}

private void createExpandableListViewDialog()
{
viewList = ExpandableListViewDialog.this.getLayoutInflater().inflate(R.layout.expandablelist, null);
dialogMarketList = new Dialog(ExpandableListViewDialog.this);
dialogMarketList.setContentView(viewList);

dialogMarketList.setTitle("Dialog Demo");
elvForDialog = (ExpandableListView) viewList.findViewById(R.id.elvForDialog); //Here should use the Dialog view

//绑定ExpandableListView的数据
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(ExpandableListViewDialog.this, listGroup, listChild);
elvForDialog.setAdapter(mAdapter);


elvForDialog.setOnGroupClickListener(new OnGroupClickListener(){
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int arg2, long arg3) throws RuntimeException {
try{
//将上一个选中项的背景清空
//viewListLastSelected.setBackgroundDrawable(null);
Log.v("LH", "@setOnGroupClickListener");
Log.v("LH", "" + viewListLastSelected.toString());
Log.v("LH", "" + ((TextView)viewListLastSelected).getText());
//viewListLastSelected.setBackgroundResource(R.drawable.bg_toolbar);
//((TextView)viewListLastSelected).setTextColor(Color.BLUE);
//Generic.selectedTextViewID = ((TextView)viewListLastSelected).getId();

}
catch(Exception e){ Log.v("LH", "ERROR@onCreate: " + e.toString()); }
return false;
}
});


elvForDialog.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) throws RuntimeException
{
//Generic.setGroupPosition(arg2);
//Generic.setChildPosition(arg3);

showToastMessage("[Child Click]:" + arg2 + "|" + arg3 + " -- " + listChild.get(arg2).get(arg3).toString());

//elvForDialog.clearChoices();
elvForDialog.clearChildFocus(viewListLastSelected);

try{
//将上一个选中项的背景清空
Log.v("LH", "@setOnChildClickListener");
viewListLastSelected.setBackgroundDrawable(null);
((TextView)viewListLastSelected).setTextColor(Color.BLACK);
//elvForDialog.getSelectedView().setBackgroundDrawable(null);
}
catch(Exception e){}

//设置在当前选中项的背景
arg1.setBackgroundResource(R.drawable.bg_toolbar);
((TextView)arg1).setTextColor(Color.WHITE);

//缓存当前选中项至上一个选中项
viewListLastSelected = arg1;

//Put value to main UI's control
editText1.setText(listChild.get(arg2).get(arg3).toString());

//Close the dialog
dialogMarketList.dismiss();
return false;
}
});
}

private void showToastMessage(String value)
{
Toast.makeText(ExpandableListViewDialog.this, value, Toast.LENGTH_LONG).show();
}


private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg){

switch (msg.what) {
case 0: //Success, refresh UI
/
package com.Anson.ExpandableListViewDialogDemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
//import android.util.Log;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
Activity activity;
List<String> listGroup = new ArrayList<String>();
List<List<String>> listChild = new ArrayList<List<String>>();

private int _posGroup = 0;
private int _posChild = 0;

public MyExpandableListAdapter(Activity a, List<String> group, List<List<String>> children){
super();
activity = a;
listGroup = group;
listChild = children;
//Log.v("LH","@MyExpandableListAdapter: ");
}

@Override
public Object getChild(int groupPosition, int childPosition) {
//Log.v("LH", listChild.get(groupPosition).get(childPosition));
return listChild.get(groupPosition).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
Log.v("LH", "ChildID: " + childPosition);
_posGroup = groupPosition;
_posChild = childPosition;
return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
//Log.v("LH", "ChildrenCount: " + groupPosition);
return listChild.get(groupPosition).size();
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String string = listChild.get(groupPosition).get(childPosition);

//Log.v("LH","@getChildView: " + string);
View view = getGenericView(string);

TextView text = (TextView) view;

if(this._posGroup==groupPosition &&
this._posChild==childPosition)
{
text.setTextColor(Color.WHITE);
text.setBackgroundResource(R.drawable.bg_toolbar);
}

return view;
}


//group method stub
@Override
public Object getGroup(int groupPosition) {
//Log.v("LH", listGroup.get(groupPosition));
return listGroup.get(groupPosition);
}

@Override
public int getGroupCount() {
//Log.v("LH", "GroupCount: " + listGroup.size());
return listGroup.size();
}

@Override
public long getGroupId(int groupPosition) {
Log.v("LH", "GroupID: " + groupPosition);
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String string = listGroup.get(groupPosition);
//Log.v("LH","@getGroupView: " + string);
return getGenericView(string);
}

//View stub to create Group/Children's View
public TextView getGenericView(String s) {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);

lp.height=36;
TextView text = new TextView(activity);
text.setLayoutParams(lp);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setTextColor(Color.BLACK);
text.setText(s);

return text;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

========
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="@color/White">

<TextView android:id="@+id/textView002" android:layout_width="fill_parent"
android:layout_height="20dip" android:text="" />

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/absTest" android:layout_width="fill_parent"
android:layout_height="50dip">
<TextView android:id="@+id/textView1"
android:layout_width="66dip" android:layout_height="40dip"
android:text="Selected: " android:paddingTop="10dip" android:paddingLeft="10dip"
android:textSize="14sp" android:textColor="@color/Black" />
<EditText android:id="@+id/editText1" android:text=""
android:layout_x="70dip" android:layout_width="160dip"
android:layout_height="40dip" android:textSize="14sp"
android:editable="false" />
<Button android:id="@+id/btnShowDialog"
android:layout_width="66dip" android:layout_height="36dip"
android:text="Show" android:layout_x="236dip" android:textSize="14sp"
/>
</AbsoluteLayout>
</LinearLayout>


========
expandablelist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:id="@+id/LinearLayoutGetList"
android:background="@color/White">
<ExpandableListView
android:id="@+id/elvForDialog"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值