当前位置: 编程技术>移动开发
本页文章导读:
▪用linearLayout替代ListView 用linearLayout代替ListView
因为一个界面上面的内容太多,下面ListView查看不到,想在外面
加上scrollView,可是还是不行,结果没办法,用linearlayout代替
。对于具体要求,自己慢慢完善,这个相当.........
▪ 滑动图片放大缩小成效 滑动图片放大缩小效果
触及手势向外图片就会放大。向里就会缩小
......
▪ juqery文件下传 plupload java修改版 juqery文件上传 plupload java修改版
最近需要用到jquery文件上传插件,发现plupload这东西挺好的,奈何后台代码是php,tomcat得配置php才能跑起来,于是稍微研究了下,改成了java代码
plupload的特.........
[1]用linearLayout替代ListView
来源: 互联网 发布时间: 2014-02-18
用linearLayout代替ListView
因为一个界面上面的内容太多,下面ListView查看不到,想在外面
加上scrollView,可是还是不行,结果没办法,用linearlayout代替
。对于具体要求,自己慢慢完善,这个相当于一个框架形式:
自己修改的地方:
public class AdapterForLinearLayout extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, Object>> data;
public int count;
private Context context;
public AdapterForLinearLayout(Context context) {
super();
this.context = context;
this.mInflater = (LayoutInflater) context
.getSystemService("layout_inflater");
this.data = new ArrayList<Map<String, Object>>();
count = data.size();
}
public final class ListItemView {// 自定义控件集合
public TextView thread_number;
public TextView thread_author;
public TextView thread_time;
public TextView thread_text;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public void addList(List<Map<String, Object>> mList) {
data.addAll(mList);
}
public void remove(List<Map<String, Object>> mList) {
data.remove(mList);
}
public View getView(int position, View convertView, ViewGroup arg2) {
ListItemView listItemView = null;
if (convertView == null) {
listItemView = new ListItemView();
convertView = LayoutInflater.from(context).inflate(
R.layout.forum_threaddetails_item, null);
listItemView.thread_number = (TextView) convertView
.findViewById(R.id.thread_number);
listItemView.thread_author = (TextView) convertView
.findViewById(R.id.thread_author);
listItemView.thread_time = (TextView) convertView
.findViewById(R.id.thread_time);
listItemView.thread_text = (TextView) convertView
.findViewById(R.id.thread_text);
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}
listItemView.thread_number.setText((String) data.get(position).get(
"thread_number"));
listItemView.thread_author.setText((String) data.get(position).get(
"thread_author"));
listItemView.thread_time.setText((String) data.get(position).get(
"thread_time"));
listItemView.thread_text.setText((String) data.get(position).get(
"thread_text"));
return convertView;
}
/**
* 绑定视图
*
* @param view
* @param item
* @param from
*/
private void bindView(View view, Map<String, Object> item, String from) {
Object data = item.get(from);
if (view instanceof TextView) {
((TextView) view).setText(data == null ? "" : data.toString());
}
}
}
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View v = adapter.getView(i, null, null);
v.setOnClickListener(this.onClickListener);
if (i == count - 1) {
LinearLayout ly = (LinearLayout) v;
ly.removeViewAt(2);
}
addView(v, common.mNUM);
common.mNUM++;
}
}
private ArrayList<Map<String, Object>> initValues() {
list_buffer = CommonUtil.parseXml2(buffer, "r");
adapter = new AdapterForLinearLayout(this);
ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int total = common.CURRENTPAGE * common.PAGESIZE;
if (list_buffer.size() <= 0) {
// return;
Toast.makeText(ThreadDetailsActivity.this, "没有回复内容",
Toast.LENGTH_SHORT).show();
} else {
if (total < list_buffer.size()) {
addPageMore();
}
for (int i = (common.CURRENTPAGE - 1) * common.PAGESIZE; i < total; i++) {
if (total >= list_buffer.size()) {
total = list_buffer.size();
}
String mContent = (String) list_buffer.elementAt(i);
detailAuthor = CommonUtil.parseXml(mContent, "a");
Content = CommonUtil.parseXml(mContent, "c");
floor = CommonUtil.parseXml(mContent, "l");
Time = CommonUtil.parseXml(mContent, "rt");
Map<String, Object> win = new HashMap<String, Object>();
win.put("thread_number", floor + "楼");
win.put("thread_author", detailAuthor);
win.put("thread_time", Time);
win.put("thread_text", Content);
list.add(win);
}
}
return list;
}
然后调入到Handler handler = new Handler() {
public void handleMessage(Message msg) {
//具体....
}中。
我自己做的一个是论坛帖子详情,以及回帖,跟帖,而且是带分页效果的:
public class ThreadDetailsActivity extends Activity {
LinearLayoutForListView list_threaddetails;
Vector tag_content = new Vector();
View itemview;
ImageView line, view_refresh;
LinearLayout linear_image, linear_refresh, linear_post, linear_number;
LinearLayout linear_title2, linear_title1;
TextView btn_text, payment_tv;
TextView title_authorname, title_time, title_theme, title_content;
EditText reply_edit;
Button btn_reply;
// -------------------
final static int PROGRESS_DIALOG = 0;
final static int PROGRESS_DIALOG1 = 1;
final static int PROGRESS_DIALOG2 = 2;
final static int PROGRESS_DIALOG3 = 3;
ProgressThread progressThread;
ProgressDialog progressDialog;
public String url;// @@1
int state;
// ------------------------------------------
private List<Map<String, Object>> data;
private View loadingView;
AdapterForLinearLayout adapter;
private ScrollView sc;
Vector keys = new Vector();
Vector names = new Vector();
private ProgressBar pbar;
String buffer, tid;
Vector list_buffer;
String detailAuthor, Content, floor, Time;
String threadContent, iContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forum_threaddetails);
linear_image = (LinearLayout) findViewById(R.id.linear_image);
sc = (ScrollView) findViewById(R.id.scroll);
list_threaddetails = (LinearLayoutForListView) findViewById(R.id.list_forum_threaddetails);
// title_listview = (ListView) findViewById(R.id.title_listview);
view_refresh = (ImageView) findViewById(R.id.view_refresh);
linear_refresh = (LinearLayout) findViewById(R.id.linear_refresh);
linear_post = (LinearLayout) findViewById(R.id.linear_post);
payment_tv = (TextView) findViewById(R.id.payment_tv);
reply_edit = (EditText) findViewById(R.id.reply_edit);
btn_reply = (Button) findViewById(R.id.btn_reply);
linear_title1 = (LinearLayout) findViewById(R.id.linear_title1);
linear_title2 = (LinearLayout) findViewById(R.id.linear_title2);
// -----------截取数据--------------------
buffer = this.getIntent().getStringExtra("buffer");
tid = this.getIntent().getStringExtra("tid");
String authorName = CommonUtil.parseXml(buffer, "a");
String titleTime = CommonUtil.parseXml(buffer, "pt");
String titleTheme = CommonUtil.parseXml(buffer, "t");
String titleContent = CommonUtil.parseXml(buffer, "c");
common.CURRENTPAGE = 1;
// ----------主题-----------------------
title_authorname = (TextView) findViewById(R.id.title_authorname);
title_time = (TextView) findViewById(R.id.title_time);
title_theme = (TextView) findViewById(R.id.title_theme);
title_content = (TextView) findViewById(R.id.title_content);
title_authorname.setText(authorName);
title_time.setText(titleTime);
title_theme.setText(titleTheme);
title_content.setText(titleContent);
showDialog(PROGRESS_DIALOG);
// ---------------------
// 加载试图布局、
loadingView = LayoutInflater.from(this).inflate(
R.layout.list_page_load, null);
linear_number = (LinearLayout) loadingView
.findViewById(R.id.linear_number);
btn_text = (TextView) loadingView.findViewById(R.id.btn_text);
btn_text.setText("更多跟帖");
pbar = (ProgressBar) loadingView.findViewById(R.id.pbar);
linear_number.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
pbar.setVisibility(View.VISIBLE);
linear_number
.setBackgroundResource(R.drawable.football_shownumber1);
btn_text.setText("更多跟帖");
btn_text.setTextColor(Color.WHITE);
loadingHandler.postDelayed(new Runnable() {
public void run() {
list_threaddetails.removeView(loadingView);
addListItem();
loadingHandler.sendEmptyMessage(0);
adapter.notifyDataSetChanged();
loadingHandler.removeMessages(0);// 取消线程
}
}, 1000);
}
});
// -----------------------------------------------
if (sc != null) {
sc.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
ThreadDetailsActivity.this.CloseKeyBoard();
return false;
}
});
}
// 刷新
linear_refresh.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
CloseKeyBoard();
view_refresh.setImageResource(R.drawable.refulse1);
common.CURRENTPAGE = 1;
showDialog(PROGRESS_DIALOG);
}
});
// 返回
linear_image.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ThreadDetailsActivity.this.finish();
}
});
// 回复
btn_reply.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
CloseKeyBoard();
btn_reply.setBackgroundResource(R.drawable.chatroom_publish1);
if (common.USERNAME.length() <= 0
&& common.PASSWORD.length() <= 0) {
new AlertDialog.Builder(ThreadDetailsActivity.this)
.setTitle("提 示").setMessage("还没登陆,请立即登陆")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface arg0, int arg1) {
Utils.User_Login(
ThreadDetailsActivity.this,
PROGRESS_DIALOG1);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface arg0, int arg1) {
arg0.cancel();
arg0.dismiss();
}
}).create().show();
} else {
iContent = reply_edit.getText().toString();
if (iContent.length() <= 0) {
Toast.makeText(ThreadDetailsActivity.this, "消息不能为空!",
Toast.LENGTH_SHORT).show();
} else {
url = common.SERVER + "/client/bbsreply.php?name="
+ common.USERNAME + "&pwd=" + common.PASSWORD
+ "&tid=" + tid;
System.out.println("@@@@~~" + url);
showDialog(PROGRESS_DIALOG2);
reply_edit.setText("");
}
}
}
});
}
Handler loadingHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 改变适配器的数目
adapter.count = common.CURRENTPAGE * common.PAGESIZE;
// 通知适配器,发现改变操作
adapter.notifyDataSetChanged();
loadingHandler.removeMessages(0);
};
};
private void addPageMore() {
list_threaddetails.addView(loadingView);
}
// 添加List元素
private void addListItem() {
// common.INDEX = 0;
// common.mNUM = common.CURRENTPAGE * common.PAGESIZE;
common.CURRENTPAGE++;
data = initValues();
adapter.addList(data);
list_threaddetails.setAdapter(adapter);
}
protected Dialog onCreateDialog(int id) {
state = id;
switch (state) {
case PROGRESS_DIALOG:
case PROGRESS_DIALOG1:
case PROGRESS_DIALOG2:
case PROGRESS_DIALOG3:
progressDialog = new ProgressDialog(ThreadDetailsActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("请稍后...");
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
default:
return null;
}
}
private class ProgressThread extends Thread {
// 由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,
Handler mHandler;
ProgressThread(Handler h) {
mHandler = h;
}
public void run() {
String buffer = "";
if (state == PROGRESS_DIALOG) {
url = common.SERVER + "/client/bbstopicreply.php?tid=" + tid
+ "&page=1&pagesize=100000&imei=587982314717858"
+ "&version=1.0&platform=j2me";
buffer = Http.Get(url);
} else if (state == PROGRESS_DIALOG2) {
keys.addElement("body");
names.addElement(iContent);
buffer = Http.Post(url, keys, names);
} else {
buffer = Http.Get(url);
}
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putString("buffer", buffer);
msg.setData(b);
mHandler.sendMessage(msg);
}
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
// 获取数据
buffer = msg.getData().getString("buffer");
removeDialog(state);
if (state == PROGRESS_DIALOG) {
data = initValues();
adapter.addList(data);
list_threaddetails.setAdapter(adapter);
view_refresh.setImageResource(R.drawable.refluts);
} else if (state == PROGRESS_DIALOG1) {
Utils.LoginStatus(ThreadDetailsActivity.this, buffer);
} else if (state == PROGRESS_DIALOG2) {
String code = CommonUtil.parseXml(buffer, "c");
if (code.equals("1")) {
common.CURRENTPAGE = 1;
list_threaddetails.removeView(loadingView);
list_threaddetails.removeAllViews();
adapter.notifyDataSetChanged();
common.mNUM = 0;
url = common.SERVER + "/client/bbstopicreply.php?tid="
+ tid
+ "&page=1&pagesize=100000&imei=587982314717858"
+ "&version=1.0&platform=j2me";
showDialog(PROGRESS_DIALOG);
} else if (code.equals("-1")) {
Toast.makeText(ThreadDetailsActivity.this, "此用户被禁言",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ThreadDetailsActivity.this, "未知错误",
Toast.LENGTH_SHORT).show();
}
}
}
};
private ArrayList<Map<String, Object>> initValues() {
list_buffer = CommonUtil.parseXml2(buffer, "r");
adapter = new AdapterForLinearLayout(this);
ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int total = common.CURRENTPAGE * common.PAGESIZE;
if (list_buffer.size() <= 0) {
// return;
Toast.makeText(ThreadDetailsActivity.this, "没有回复内容",
Toast.LENGTH_SHORT).show();
} else {
if (total < list_buffer.size()) {
addPageMore();
}
for (int i = (common.CURRENTPAGE - 1) * common.PAGESIZE; i < total; i++) {
if (total >= list_buffer.size()) {
total = list_buffer.size();
}
String mContent = (String) list_buffer.elementAt(i);
detailAuthor = CommonUtil.parseXml(mContent, "a");
Content = CommonUtil.parseXml(mContent, "c");
floor = CommonUtil.parseXml(mContent, "l");
Time = CommonUtil.parseXml(mContent, "rt");
Map<String, Object> win = new HashMap<String, Object>();
win.put("thread_number", floor + "楼");
win.put("thread_author", detailAuthor);
win.put("thread_time", Time);
win.put("thread_text", Content);
list.add(win);
}
}
return list;
}
/**
* 定义弹出View试图
*/
private View getView(int id) {
LayoutInflater factory = LayoutInflater
.from(ThreadDetailsActivity.this);
View view = factory.inflate(id, null);
return view;
}
// 点击Activity中的任意位置,edittext焦距消失,软键盘隐藏
public void CloseKeyBoard() {
list_threaddetails.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(list_threaddetails.getWindowToken(), 0);
pbar.setVisibility(View.INVISIBLE);
linear_number.setBackgroundResource(R.drawable.football_shownumber);
btn_text.setText("更多跟帖");
btn_text.setTextColor(R.color.white);
}
public boolean onTouchEvent(MotionEvent event) {
CloseKeyBoard();
return super.onTouchEvent(event);
}
}
下面这个压缩包项目只是相对应的框架形式,数据是死的
1 楼
liu_zheng
2012-09-14
博主 我想把文字换成图片 要怎么修改呢??
[2] 滑动图片放大缩小成效
来源: 互联网 发布时间: 2014-02-18
滑动图片放大缩小效果
触及手势向外图片就会放大。向里就会缩小
触及手势向外图片就会放大。向里就会缩小
[3] juqery文件下传 plupload java修改版
来源: 互联网 发布时间: 2014-02-18
juqery文件上传 plupload java修改版
我等会把代码发你
最近需要用到jquery文件上传插件,发现plupload这东西挺好的,奈何后台代码是php,tomcat得配置php才能跑起来,于是稍微研究了下,改成了java代码
plupload的特色是
1、可以配置chunk,将一个大文件分成许多小文件上传,后台通过php合并成大文件,这里通过相应的java代码
2、实际上传的文件名是经过生成唯一的uuid,通过参数name传递到后台
3、上传文件的过程是先上传临时命名为uuid的临时文件,上传成功后会自动生成几个input标签,对应上传之后的临时文件的文件名,之后通过另一个action调用uploadFinish对临时文件进行重命名 操作或者其他操作
这个java代码是基于 Struts2的,不是servlet,反正都是类似的 在这基础上也容易改
public class UploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 2 * 1024;
private File upload;
private String name; //plupload上传文件的临时文件名 uuid.文件后缀
private String uploadFileName;
private String uploadContentType;
private int chunk;
private int chunks;
// 。。。一堆getter setter自己生成
private void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
if (dst.exists()) {
out = new BufferedOutputStream(new FileOutputStream(dst, true),
BUFFER_SIZE); //plupload 配置了chunk的时候新上传的文件appand到文件末尾
} else {
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
}
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String upload() throws Exception {
String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp")
+ "\\" + this.getName(); // 保存目录可以自己配置 或者定义变量自行配置
File dstFile = new File(dstPath);
// 文件已存在删除旧文件(上传了同名的文件)
if (chunk == 0 && dstFile.exists()) {
dstFile.delete();
dstFile = new File(dstPath);
}
copy(this.upload, dstFile);
//System.out.println(uploadFileName + " " + uploadContentType + " "
+ chunk + " " + chunks);
if (chunk == chunks - 1) {
// 一个完整的文件上传完成
}
return SUCCESS;
}
public String uploadFinish() {
String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp");
HttpServletRequest request = ServletActionContext.getRequest();
int count = Integer.parseInt(request.getParameter("uploader_count"));
for (int i = 0; i < count; i++) {
uploadFileName = request.getParameter("uploader_" + i + "_name");
name = request.getParameter("uploader_" + i + "_tmpname");
System.out.println(uploadFileName + " " + name);
try {
//对已经上传成功的临时文件进行操作
} catch(Exception e) {
}
}
return SUCCESS;
}
}
补充:
从项目里抽取出了文件上传的代码,单独一个eclipse上可以跑的例子,上传上来了(注意build path里的jre路径)
原创,欢迎转载,请标明源
http://asyty.iteye.com/blog/1230119/
1 楼
cwm_chen
2011-11-15
你好请问你又例子吗,麻烦共享一份,让我们可以下载,谢谢了。我的邮箱cwm_chen@126.com
2 楼
asyty
2011-11-15
cwm_chen 写道
你好请问你又例子吗,麻烦共享一份,让我们可以下载,谢谢了。我的邮箱cwm_chen@126.com
我等会把代码发你
3 楼
cwm_chen
2011-11-15
太感谢了。
4 楼
cwm_chen
2011-11-16
我已经调试成功了,但是出现问题。
1。采用jquyer dialog 对话框创建上传组件
var uploaderDialog;
//判断是否已经加载上传组件
if($("#uploaderDialog").html() == null){
//创建上传组件
uploaderDialog = $("<div id='uploaderDialog' title='"+title+"'></div>").appendTo("body");
uploaderDialog.html($("#uploader"));
$("#uploader").plupload({
runtimes:"gears,flash,silverlight,browserplus,html5,html4",
url:"uploadFileAction",
max_file_size:"200mb",
chunk_size:"2mb",
unique_names:true,
filters:[
{title:"请选择压缩文件", extensions:"zip"}
],
flash_swf_url:path + "/js/plupload/plupload.flash.swf",
silverlight_xap_url:path + "/js/plupload/plupload.silverlight.xap"
});
//jquery ui对话框显示上传组件
uploaderDialog.dialog({
autoOpen:false,
width:700,
height:420,
resizable:false,
modal: true,
buttons:{
'关闭':function(){
uploaderDialog.dialog("close");
}
}
});
}else{
$("#uploaderDialog").dialog("open");
}
出现问题:1。打开对话框,选择文件上传,没有问题。
2。关闭dialog之前选择文件,然后在打开dialog上传文件上传停止不动。
3。选择文件后,提交表单,进行上传,与2一个效果。
求解答我的QQ295099383 陈伟明
1。采用jquyer dialog 对话框创建上传组件
var uploaderDialog;
//判断是否已经加载上传组件
if($("#uploaderDialog").html() == null){
//创建上传组件
uploaderDialog = $("<div id='uploaderDialog' title='"+title+"'></div>").appendTo("body");
uploaderDialog.html($("#uploader"));
$("#uploader").plupload({
runtimes:"gears,flash,silverlight,browserplus,html5,html4",
url:"uploadFileAction",
max_file_size:"200mb",
chunk_size:"2mb",
unique_names:true,
filters:[
{title:"请选择压缩文件", extensions:"zip"}
],
flash_swf_url:path + "/js/plupload/plupload.flash.swf",
silverlight_xap_url:path + "/js/plupload/plupload.silverlight.xap"
});
//jquery ui对话框显示上传组件
uploaderDialog.dialog({
autoOpen:false,
width:700,
height:420,
resizable:false,
modal: true,
buttons:{
'关闭':function(){
uploaderDialog.dialog("close");
}
}
});
}else{
$("#uploaderDialog").dialog("open");
}
出现问题:1。打开对话框,选择文件上传,没有问题。
2。关闭dialog之前选择文件,然后在打开dialog上传文件上传停止不动。
3。选择文件后,提交表单,进行上传,与2一个效果。
求解答我的QQ295099383 陈伟明
5 楼
asyty
2011-11-16
哦,混合上jquyer dialog我倒没试过。。不过jquery插件混用确实有可能出问题。。
cwm_chen 写道
我已经调试成功了,但是出现问题。
1。采用jquyer dialog 对话框创建上传组件
出现问题:1。打开对话框,选择文件上传,没有问题。
2。关闭dialog之前选择文件,然后在打开dialog上传文件上传停止不动。
3。选择文件后,提交表单,进行上传,与2一个效果。
1。采用jquyer dialog 对话框创建上传组件
出现问题:1。打开对话框,选择文件上传,没有问题。
2。关闭dialog之前选择文件,然后在打开dialog上传文件上传停止不动。
3。选择文件后,提交表单,进行上传,与2一个效果。
6 楼
huawin
2012-07-04
学习了,谢谢共享
7 楼
king_shanqiu
2012-07-24
[size=xx-small][/size][align=left][/align]
8 楼
dyyweb
2012-08-22
发给我一份 急用 拜求 275001477@qq.com
最新技术文章: