`
龙哥IT
  • 浏览: 252673 次
  • 性别: 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  
博主 我想把文字换成图片 要怎么修改呢??

相关推荐

    linearlayoutForListview

    在某些场景下,例如数据量较小、不需要滚动或者只需要简单的布局时,我们可以考虑使用LinearLayout代替ListView。具体步骤如下: 1. 创建布局文件:为每个列表项创建一个单独的XML布局文件,包含所需元素。 2. 动态...

    解决Android中ListView和ScrollView嵌套冲突

    3. **使用LinearLayout代替ScrollView**:如果ListView下方还有其他内容,可以考虑使用LinearLayout作为容器,然后为LinearLayout设置垂直方向的weight属性,使ListView占据足够的空间。 4. **使用...

    android 使用ListView来实现表格

    然而,需要注意的是,对于大型数据集,使用RecyclerView代替ListView会更高效,因为它提供了更好的滚动性能和内存管理。不过,对于小型表格,ListView已经足够满足需求。在实际开发中,根据项目需求选择合适的组件是...

    ScrollView嵌套ListView滑动冲突的解决方案

    5. **使用CardView或LinearLayout代替ScrollView**:如果内容不多,可以考虑使用CardView或者LinearLayout来替代ScrollView,这样可以直接将多个CardView或LinearLayout放在ListView中,避免滑动冲突。 6. **优化...

    ListViewDemo

    6. **使用高效的布局**:减少嵌套布局,选择性能较好的布局,如LinearLayout、RecyclerView(代替ListView)等。 7. **滚动监听**:通过实现`OnScrollListener`,可以在滚动过程中暂停不必要的后台任务,如数据刷新...

    listView嵌套listView

    不过,考虑到现代Android开发的趋势,使用RecyclerView代替ListView可能是更好的选择,因为它提供了更强大的功能,如嵌套滚动、动画支持等。但无论选择哪种方法,都需要充分理解其工作原理,才能有效地应对复杂的...

    下拉刷新及加载更多没有使用LISTVIEW来做

    在这个场景中,开发者选择了ScrollView加上LinearLayout来代替ListView来实现下拉刷新和加载更多的功能。 首先,我们来分析一下为什么不用ListView: 1. **ListView的复用机制**:ListView通过convertView重用机制...

    仿美团嵌套ListView

    另外,对于子ListView,如果内容较少,可以考虑使用LinearLayout或者其他布局代替,以减少复杂度。 在“美团嵌套listview”项目中,开发者KJY已经实现了80%的相似度,这包括了基本的嵌套布局、下拉刷新和加载更多...

    listview二级列表(前面图标不相同)

    - 使用ExpandableListView代替ListView也可以方便地实现这一功能,但这里我们讨论的是基于普通ListView的实现。 4. **自定义视图**: - 在`getView()`方法中,为一级列表项创建布局,包含图标、标题和一个可点击...

    CardView+ListView

    如果你想要优化`CardView`在`ListView`中的滑动流畅性,更建议优化`ListView`的滚动性能,比如使用`RecyclerView`代替`ListView`,或者使用`ViewHolder`模式减少视图的创建和复用。 总的来说,`CardView`和`...

    ScollView_listview冲突解决

    如果ScrollView中的内容可以拆分成多个部分,考虑将ListView独立出来,使用多个LinearLayout或RelativeLayout来代替ScrollView。 4. 自定义滚动逻辑:如果以上方法都无法满足需求,可以尝试自定义滚动逻辑。通过...

    ScrollView中嵌套ScrollView或ListView并且内部ScrollView或ListView也可滑动.

    - 使用RecyclerView代替ListView,因为RecyclerView有更高效的视图复用机制。 - 在必要时使用NestedScrollView,而不是直接在ScrollView中嵌套其他可滚动视图。 - 使用滚动事件拦截,避免不必要的滚动操作。 总结,...

    android ScrollView嵌套ListView,并且实现动态添加ListView里的Item

    解决这个问题的一种常见方法是使用`NestedScrollView`代替ScrollView,因为NestedScrollView支持嵌套滚动,它可以与另一个可滚动的视图协同工作。 ```xml android:layout_width="match_parent" android:layout_...

    Android listView 绘制表格源码

    通常,我们可以使用LinearLayout或TableLayout作为ListView项的基础布局。TableLayout允许我们创建行和列,并通过TableRow添加单元格。每个单元格可以是TextView或其他视图,以显示数据。 ```java @Override public...

    Android ListView 和ScroolView 出现onmeasure空指针的解决办法

    同时,持续关注Android的新特性和最佳实践,如使用RecyclerView代替ListView,以及探索更现代的布局管理器,比如GridLayoutManager,以提高应用的性能和用户体验。 总结来说,遇到ListView或GridView的`onmeasure`...

    ListView 实现点击侧边A-Z快速查找.zip

    为了提高性能,可以使用SparseArray代替ArrayList存储数据,因为SparseArray对于查找操作更高效。此外,适配器中应重写getCount()、getItem()和getItemId()方法,以配合ListView的工作。 7. **视觉效果**: 为了...

    Android CircleList(圆弧形的ListView)

    为此,可以考虑使用`RecycleView`代替`ListView`,并利用其缓存机制来提高性能。此外,对适配器和视图复用策略的精细调整也是必要的。 总结一下,`CircleList`是Android开发中的一个创新实践,它通过将列表呈现在...

    android listview 水平滚动和垂直滚动的小例子

    在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。...在现代Android开发中,通常推荐使用RecyclerView及其灵活的LayoutManager来代替ListView,以获得更好的性能和定制能力。

    Android应用中通过Layout_weight属性用ListView实现表格

    几乎没有用到,我们完全可以用LinearLayout和RelativeLayout来代替TableLayout的使用,自己开发中主要使用LinearLayout,RelativeLayout这两种布局,不过刚开始我还是偏爱于RelativeLayout,因为在RelativeLayout...

Global site tag (gtag.js) - Google Analytics