public class Jcwd extends Fragment {
private static final int MESSAGE_UPDATE_SUCCESS = 0;// 加載成功
private static final int MESSAGE_UPDATE_FAIL = 1;// 加載失敗
public All_Query_Dialog progressDialog_UI;//正在查詢對話框
private File file;
public fileActivity context;
// 樹形Listview顯示類別
private ExpandableListView listview1;
// 一級
private List<String> groups;
private List<String> grouplist = new ArrayList<String>();
// 二級
// private List<List<String>> child;
private List<String> child;
private List<String> childlist;
private List<String> Idlist;
private Map<String, List<String>> childmap = new HashMap();
private Map<String, List<String>> Idmap = new HashMap();
private Map<String, List<String>> map = new HashMap();
private Map<String, List<String>> id = new HashMap();
// 適配器
private ListViewAdapter mAdapter;
//記錄點擊的節點位置,初始爲-1
public static int children_item = -1;
public static int parent_item = -1;
//記錄上一次點擊的item
private View view;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_UPDATE_SUCCESS:
System.out.println("解析成功");
for(int i = 0;i<file.getData().size();i++){
grouplist.add(file.getData().get(i).getTypeName());
childlist = new ArrayList<String>();
Idlist = new ArrayList<String>();
for (int j = 0; j < file.getData().get(i).getQueryUnitData().size(); j++) {
childlist.add(file.getData().get(i).getQueryUnitData().get(j).getUnitName());
Idlist.add(file.getData().get(i).getQueryUnitData().get(j).getId());
System.out.println("????<<>>><<>>"+file.getData().get(i).getQueryUnitData().get(j).getUnitName());
}
childmap.put(""+i, childlist);
Idmap.put(""+i,Idlist);
if(i == 11){
map = childmap;
id = Idmap;
}
}
initview();
break;
case MESSAGE_UPDATE_FAIL:
// 請求失敗---提示錯誤
back(getResources().getString(R.string.message_query_fall));
break;
}
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
loadData();
if (activity instanceof fileActivity) {
context = (fileActivity) activity;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
view = inflater.inflate(R.layout.activity_file_jcwd,null);
return view;
}
private void initview() {
listview1 = (ExpandableListView)view.findViewById(R.id.listview1);
mAdapter = new ListViewAdapter(context);
groups = grouplist;
//一級目錄
child = getListchildData();
List<ListViewAdapter.TreeNode> treeNode = mAdapter.GetTreeNode();
//爲每一個一級目錄下添加內容
for (int i = 0; i < groups.size(); i++) {
ListViewAdapter.TreeNode node = new ListViewAdapter.TreeNode();
node.parent = groups.get(i);
for (int ii = 0; ii <map.get(""+i).size() ; ii++) {
node.childs.add(map.get(""+i).get(ii));
}
treeNode.add(node);
}
mAdapter.UpdateTreeNode(treeNode);
listview1.setAdapter(mAdapter);
listview1.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView arg0,
View arg1, int parent, int children, long arg4) {
//當前item變色
// arg1.setBackgroundResource(R.color.color1);
//上一個item還原
if (parent_item != -1
&& children_item != -1
&& (children_item != children || parent_item != parent)) {
view.setBackgroundResource(R.drawable.listview_item_bg);
}
view = arg1;
children_item = children;
parent_item = parent;
Intent intent = new Intent();
intent.setClass(context, JcwdInfo.class);
intent.putExtra("Qid",id.get(""+parent).get(children));
startActivity(intent);
Toast.makeText(context, "被點擊:"+id.get(""+parent).get(children), Toast.LENGTH_SHORT).show();
return false;
}
});
}
// 加載數據
public void loadData() {
//showProgressWin("提示信息","正在查詢...");
final Map<String, String> params = new HashMap<String, String>();
Log.i("MA", "基礎檔案數據1");
new Thread(new Runnable() {
public void run() {
try {
String result = RemoteService.get("queryBasic", params);
parseLocalResult(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
// 解析
public void parseLocalResult(String data) {
try {
Gson gson = new Gson();
file = gson.fromJson(data, File.class);
final Message msg = new Message();
if (file.getStatus().equals("OK")) {
// 回到主線程 獲取目錄結構,建立文件夾結構
msg.what = MESSAGE_UPDATE_SUCCESS;
mHandler.sendMessage(msg);
} else {
msg.what = MESSAGE_UPDATE_FAIL;
mHandler.sendMessage(msg);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public List<String> getListData() {
List<String> list = new ArrayList<String>();
list.add("語文");
list.add("數學");
list.add("英語");
list.add("歷史");
list.add("毛概");
list.add("自然");
return list;
}
public List<String> getListchildData() {
List<String> list = new ArrayList<String>();
list.add("java");
list.add("android");
list.add("js");
list.add("ps");
list.add("測試");
list.add("軟件工程");
return list;
}
private void back(String message) {
new AlertDialog.Builder(context).setTitle("提示信息").setMessage(message)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
context.finish();
}
}).create().show();
}
/**
* 創建dialog
*
* @param title
* @param content
*//*
public void showProgressWin(String title, String content) {
if (null == progressDialog_UI) {
progressDialog_UI = new All_Query_Dialog(context, title, content);
}
progressDialog_UI.show();
progressDialog_UI.setTitleContent(title, content);
progressDialog_UI.setCancelable(false); // 按返回鍵不關閉提示框
}*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
佈局:activity_file_jcwd
<ExpandableListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@null"
/>
佈局:laws_item_parent
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="交通管理"
android:textColor="#fff"
android:textSize="18sp" />
<ImageView
android:id="@+id/img"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/expand" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/single_line" />
</LinearLayout>
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:singleLine="true"
android:text="中華人民共和國道路交通安全發實施條例"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:background="@drawable/single_line" />
</LinearLayout>
public class ListViewAdapter extends BaseExpandableListAdapter {
/* public static final int ItemHeight = 72;// 每項的高度
public static final int PaddingLeft = 48;// 每項的高度
public static final int groupPaddingLeft = 48;// 每項的高度
public static final int childPaddingLeft = 60;// 每項的高度
*/ // private int myPaddingLeft = 0;
static public class TreeNode {
public String parent;
public List<String> childs = new ArrayList<String>();
}
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
Context parentContext;
public ListViewAdapter(Context view) {
parentContext = view;
// this.myPaddingLeft = myPaddingLeft;
}
public List<TreeNode> GetTreeNode() {
return treeNodes;
}
public void UpdateTreeNode(List<TreeNode> nodes) {
treeNodes = nodes;
}
public void RemoveAll() {
treeNodes.clear();
}
@Override
public int getGroupCount() {
return treeNodes.size();
}
@Override
public int getChildrenCount(int i) {
return treeNodes.get(i).childs.size();
}
@Override
public Object getGroup(int i) {
return treeNodes.get(i).parent.toString();
}
@Override
public Object getChild(int i, int i1) {
return treeNodes.get(i).childs.get(i1)
.toString();
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView == null) {
holder=new ViewHolder();
convertView = LayoutInflater.from(parentContext).inflate(R.layout.laws_item_parent, null);
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
if(isExpanded){
holder.img.setImageResource(R.drawable.collpase);
// parentImageViw.setBackgroundResource(R.drawable.arrow_down);
}else{
holder.img.setImageResource(R.drawable.expand);}
//holder.text.setText(getGroup(groupPosition).getTypeName());
holder.text.setText(getGroup(groupPosition).toString());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parentContext).inflate(R.layout.laws_item_child, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.text);
try {
//String[] name=getChild(groupPosition,childPosition).getName().split("_");
//String namefile = name[0];
tv.setText(getChild(groupPosition,childPosition).toString());
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class ViewHolder{
TextView text;
ImageView img;
}
}