tableLayout 实现类似gridview的效果 带分割线

时间:2023-02-08 20:30:58

/**
	 * 设置表格的数据
	 * @param tableDatas
	 */
	public void setTableLayout(List<HashMap<String, Object>> tableDatas) {
		TableLayout mainLayout = new TableLayout(getContext());
		LinearLayout rowLayout = null;
		for (int i = 0; i < tableDatas.size(); i++) {
			if (i % 4 == 0) {
				if(i != 0) {
					setHorizontalSplit(mainLayout);
				}
				rowLayout = getRowLayout(mainLayout);
			}
			setItemLayout(i, 4, rowLayout, tableDatas,View.VISIBLE);
		}
		if(tableDatas.size()%4 != 0) {
		for(int i = 0; i < (4 -tableDatas.size()%4); i++) {
				setItemLayout(i+3, 4, rowLayout, tableDatas,View.INVISIBLE);
			}
		}
		setCustomView(mainLayout);
	}
	
	/**
	 * 设置水平分割线
	 * @param mainLayout
	 */
	private void setHorizontalSplit(LinearLayout mainLayout) {
		ImageView view = new ImageView(getContext());
		android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.MATCH_PARENT, 2);
		view.setBackgroundColor(Color.rgb(227, 227, 227));
		mainLayout.addView(view, params);
	}
	
	/**
	 * 获取rowLayout
	 * @param mainLayout
	 * @return
	 */
	private LinearLayout getRowLayout(TableLayout mainLayout) {
		LinearLayout rowLayout = new LinearLayout(getContext());
		rowLayout.setOrientation(LinearLayout.HORIZONTAL);
		android.view.ViewGroup.LayoutParams rowParams = new android.view.ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		mainLayout.addView(rowLayout,rowParams);
		
		return rowLayout;
	}
	
	/**
	 * 设置itemlayout
	 * @param index
	 * @param columnNum
	 * @param rowLayout
	 * @param visibility
	 */
	private void setItemLayout(int index, int columnNum, LinearLayout rowLayout, 
			List<HashMap<String, Object>> tableDatas, int visibility) {
		LinearLayout itemLayout = new LinearLayout(getContext());
		android.widget.LinearLayout.LayoutParams params1 = new android.widget.LinearLayout.LayoutParams(
				android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,100f);
		itemLayout.setOrientation(LinearLayout.VERTICAL);
		itemLayout.setId(index);
		itemLayout.setBackgroundResource(R.drawable.bottom_dialog_item_selector);
		itemLayout.setOnClickListener(this);
		ImageView iv = new ImageView(getContext()); 
		iv.setImageResource((Integer) tableDatas.get(index).get("extra_big_icon"));
		itemLayout.addView(iv);
		TextView tv = new TextView(getContext());
		tv.setTextColor(Color.rgb(127, 127, 127));
		tv.setPadding(0, 15, 0, 15); 
		tv.setGravity(Gravity.CENTER);
		tv.setText((CharSequence) tableDatas.get(index).get("extra_type")); 
		itemLayout.addView(tv);
		rowLayout.addView(itemLayout,params1);
		if(index % columnNum != (columnNum - 1)) {
			ImageView view = new ImageView(getContext());
			android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(
					2, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
			view.setBackgroundColor(Color.rgb(227, 227, 227));
			rowLayout.addView(view,params);
			view.setVisibility(visibility);
		}
		itemLayout.setVisibility(visibility);
	}


这是一个dialog从底部弹出的效果:

tableLayout 实现类似gridview的效果 带分割线

tableLayout 实现类似gridview的效果 带分割线