`
龙哥IT
  • 浏览: 237163 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

用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);
	}

}

 

 下面这个压缩包项目只是相对应的框架形式,数据是死的

  • 大小: 51.8 KB
  • 大小: 59.4 KB
分享到:
评论
1 楼 liu_zheng 2012-09-14  
博主 我想把文字换成图片 要怎么修改呢??

相关推荐

Global site tag (gtag.js) - Google Analytics