linux经典源代码,Android 经典示例源码

这是我根据自己的良好习惯,把网络上的经典Android 案例组织在一个工程中。对于Android 学习者有很好的好处。欢迎大家把这个示例项目越做越大。为后来的新人积累财富。发扬前人种树,后人乘凉的精神。

ae4b7fcb895ef68a447bf222c0867bd8.png

33f4bc84518f400398fe672448b90971.png

package com.cmw.android.widgets;

import java.util.ArrayList;

import java.util.List;

import android.app.ListActivity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import com.cmw.android.data.TreeDataProvider;

import com.cmw.android.model.TreeElement;

public class MainFrameActivity  extends ListActivity{

private List nodes = new ArrayList();

private TreeViewAdapter treeViewAdapter = null;

private TreeDataProvider provider = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

provider = new TreeDataProvider(this);

nodes = provider.getDataSource();

treeViewAdapter = new TreeViewAdapter(this, R.layout.mainframe,nodes);

setListAdapter(treeViewAdapter);

registerForContextMenu(getListView());

}

@SuppressWarnings("unchecked")

private class TreeViewAdapter extends ArrayAdapter {

public TreeViewAdapter(Context context, int textViewResourceId,

List objects) {

super(context, textViewResourceId, objects);

mInflater = LayoutInflater.from(context);

mfilelist = objects;

mIconCollapse = BitmapFactory.decodeResource(

context.getResources(), R.drawable.plus);

mIconExpand = BitmapFactory.decodeResource(context.getResources(),

R.drawable.cut);

}

private LayoutInflater mInflater;

private List mfilelist;

private Bitmap mIconCollapse;

private Bitmap mIconExpand;

public int getCount() {

return mfilelist.size();

}

public Object getItem(int position) {

return position;

}

public long getItemId(int position) {

return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

convertView = mInflater.inflate(R.layout.mainframe, null);

holder = new ViewHolder();

holder.text = (TextView) convertView.findViewById(R.id.treetext);

holder.icon = (ImageView) convertView.findViewById(R.id.icon);

convertView.setTag(holder);

final TreeElement obj = mfilelist.get(position);

holder.text.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.i("TreeView", "obj.id:" + obj.getId());

obj.forward(MainFrameActivity.this);

}

});

int level = obj.getLevel();

holder.icon.setPadding(25 * (level + 1),

holder.icon.getPaddingTop(), 0,

holder.icon.getPaddingBottom());

holder.text.setText(obj.getTitle());

if (obj.isHasChild()&& (obj.isExpanded() == false)) {

holder.icon.setImageBitmap(mIconCollapse);

} else if (obj.isHasChild() && (obj.isExpanded() == true)) {

holder.icon.setImageBitmap(mIconExpand);

} else if (!obj.isHasChild()) {

holder.icon.setImageBitmap(mIconCollapse);

holder.icon.setVisibility(View.INVISIBLE);

}

return convertView;

}

class ViewHolder {

TextView text;

ImageView icon;

}

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);

Log.i("TreeView", "position:" + position);

if (!nodes.get(position).isHasChild()) {

Toast.makeText(this, nodes.get(position).getTitle(), 2000);

return;

}

if (nodes.get(position).isExpanded()) {

nodes.get(position).setExpanded(false);

TreeElement element = nodes.get(position);

ArrayList temp = new ArrayList();

for (int i = position + 1; i < nodes.size(); i++) {

if (element.getLevel() >= nodes.get(i).getLevel()) {

break;

}

temp.add(nodes.get(i));

}

nodes.removeAll(temp);

treeViewAdapter.notifyDataSetChanged();

} else {

TreeElement obj = nodes.get(position);

obj.setExpanded(true);

int level = obj.getLevel();

int nextLevel = level + 1;

for (TreeElement element : obj.getChilds()) {

element.setLevel(nextLevel);

element.setExpanded(false);

nodes.add(position + 1, element);

}

treeViewAdapter.notifyDataSetChanged();

}

}

}

Android 经典示例源码下载地址:

用户名与密码都是www.linuxidc.com

具体下载目录在 /pub/Android源码集锦/2011年/9月/Android 经典示例源码/

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值