我有一个ExpandableListView
填充了我从 Web 服务检索的一些数据。在这个 ExpView 的一些子项中,有由TableLayout
. 问题是,当表格的宽度小于屏幕宽度时,我想设置为 true;同时,如果表格的宽度大于屏幕的宽度setStretchAllColumns()
,我想将表格放入 a 中ScroolView
(让用户滚动浏览大表格中的所有数据)。
我曾尝试使用 ViewTreeObserver 但没有成功。
你能帮我一些代码或教程,或者只是指出我正确的方向吗?
PS:对不起我的英语不好,请耐心等待。
我在这里发布了我的来源ExpandableListViewAdapter
,这很混乱,但会帮助你理解我想要实现的目标。重要的方法是createTableOnUIThread
和init
。
public class MyExpListAdapter extends BaseExpandableListAdapter {
private final int TABLE_BORDER = 1;
private final int TABLE_TEXT_PADDING = 10;
private Context context = null;
private List<String> keyList = null;
private Map<String, List<LabelValueTable>> infoMap = null;
private LayoutInflater inflater = null;
public MyExpListAdapter(Context context, Map<String, List<LabelValueTable>> infoMap) {
this.context = context;
this.infoMap = infoMap;
this.keyList = new ArrayList<String>(infoMap.keySet());
this.inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.infoMap.get(this.keyList.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LabelValueTable listElement = (LabelValueTable) this.getChild(groupPosition, childPosition);
SubTableApparato table = listElement.getTable();
ViewHolder holder = null;
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.cmp_ac_scheda__expview_child, null);
holder = new ViewHolder();
holder.labelvalueContainer = (LinearLayout) convertView.findViewById(R.id.cmp_expview_child__labelvalue_container);
holder.label = (TextView) convertView.findViewById(R.id.cmp_expview_child__label);
holder.value = (TextView) convertView.findViewById(R.id.cmp_expview_child__value);
holder.tableHeader = (TextView) convertView.findViewById(R.id.cmp_expview_child__tableheader);
holder.tableScroll = (HorizontalScrollView) convertView.findViewById(R.id.cmp_expview_child__scrollview);
holder.tableLayout = (TableLayout) convertView.findViewById(R.id.cmp_expview_child__tablelayout);
holder.tableLayoutScrollable = (TableLayout) convertView.findViewById(R.id.cmp_expview_child__tablelayout_scrollable);
convertView.setBackgroundResource(R.color.scheda_asset_oggetto_apparato_layout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Se table è diverso da null disegno la tabella
if (table != null) {
holder.tableLayout.removeAllViewsInLayout();
holder.tableLayoutScrollable.removeAllViewsInLayout();
// Gruppo di controlli per il reset del layout
holder.tableHeader.setVisibility(View.VISIBLE);
holder.tableScroll.setVisibility(View.VISIBLE);
holder.tableLayout.setVisibility(View.VISIBLE);
holder.tableLayoutScrollable.setVisibility(View.VISIBLE);
holder.labelvalueContainer.setVisibility(View.GONE);
this.createTableOnUIThread(table, holder.tableHeader, holder.tableLayout, holder.tableLayoutScrollable);
} else {
// Altrimenti imposto i due valori label e value nelle rispettive
// textview
holder.tableHeader.setVisibility(View.GONE);
holder.tableScroll.setVisibility(View.GONE);
holder.tableLayout.setVisibility(View.GONE);
holder.tableLayoutScrollable.setVisibility(View.GONE);
holder.labelvalueContainer.setVisibility(View.VISIBLE);
holder.label.setText(((LabelValueTable) listElement).getLabelValue().getLabel());
holder.value.setText(((LabelValueTable) listElement).getLabelValue().getValue());
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.infoMap.get(this.keyList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this.keyList.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.infoMap.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.cmp_ac_scheda__expview_group, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.cmp_expview_group__groupname);
holder.tv.setBackgroundResource(R.color.title_bar_bg_color);
holder.tv.setTextColor(convertView.getResources().getColor(R.color.title_text_color));
holder.tv.setBackgroundResource((R.drawable.scheda_asset__groupview_bg_style));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setTypeface(null, Typeface.BOLD);
holder.tv.setText((String) getGroup(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private LinearLayout createFormattedCell(boolean isTabHeader, boolean isLast, String str) {
// Layout che circonda le textView necessario per disegnare il bordo
// delle celle
LinearLayout container = new LinearLayout(this.context);
container.setBackgroundColor(Color.BLACK);
if (isLast) {
container.setPadding(TABLE_BORDER, TABLE_BORDER, TABLE_BORDER, 0);
} else {
container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
}
TextView textView = new TextView(this.context);
textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
textView.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
if (isTabHeader) {
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(this.context.getResources().getColor(R.color.light_grayish_orange));
}
textView.setText(str);
container.addView(textView);
return container;
}
private void createTableRows(List<List<String>> table, TableLayout tableLayout) {
for (int i = 0; i < table.size(); i++) {
List<String> record = table.get(i);
TableRow column = new TableRow(this.context);
// If necessario per l'identificazione delle intestazioni delle
// colonne
if (i == 0) {
for (int j = 0; j < record.size(); j++) {
String cellValue = record.get(j);
// If necessario per l'inserimento del bordo destro
// sull'ultima cella
if (j == record.size() - 1) {
column.addView(this.createFormattedCell(true, true, cellValue));
} else {
column.addView(this.createFormattedCell(true, false, cellValue));
}
}
} else {
for (int j = 0; j < record.size(); j++) {
String cellValue = record.get(j);
if (j == record.size() - 1) {
column.addView(this.createFormattedCell(false, true, cellValue));
} else {
column.addView(this.createFormattedCell(false, false, cellValue));
}
}
}
tableLayout.addView(column);
}
}
private void createTableOnUIThread(SubTableApparato tableEntity, TextView tableHeader, final TableLayout tableLayout, final TableLayout tableLayoutScrollable) {
// Setto il titolo della table
tableHeader.setText(tableEntity.getTableName());
List<List<String>> table = tableEntity.getTable();
// if (table.size() != 0 && table.get(0).size() <= 3) {
// // Se la tabella è vuota setto le colonne per non superare la
// // larghezza dello schermo
// this.createTableRows(table, tableLayout);
// tableLayout.setStretchAllColumns(true);
// // tableLayout.getWidth();
// } else {
// tableLayout.setStretchAllColumns(false);
// this.createTableRows(table, tableLayoutScrollable);
// }
final TableLayout provvisorio = new TableLayout(this.context);
tableLayoutScrollable.removeAllViews();
tableLayoutScrollable.addView(provvisorio);
final TableLayout provvisorio2 = new TableLayout(this.context);
tableLayout.removeAllViews();
provvisorio2.setStretchAllColumns(true);
tableLayout.addView(provvisorio2);
this.createTableRows(table, provvisorio);
this.createTableRows(table, provvisorio2);
ViewTreeObserver observer = provvisorio.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
init(provvisorio, tableLayoutScrollable);
provvisorio.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
ViewTreeObserver observer1 = provvisorio2.getViewTreeObserver();
observer1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
init(provvisorio2, tableLayout);
provvisorio2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
// if (provvisorioWidth <= width) {
// provvisorio.setStretchAllColumns(true);
// tableLayout.addView(provvisorio);
// } else {
// provvisorio.setStretchAllColumns(false);
// tableLayoutScrollable.addView(provvisorio);
// }
}
protected void init(TableLayout l, TableLayout tableLayout) {
Display display = ((Activity) (this.context)).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int b = l.getMeasuredWidthAndState();
Toast.makeText(this.context, "Width schermo:" + screenWidth + " Widht view:" + b, Toast.LENGTH_SHORT).show();
if (b <= screenWidth) {
tableLayout.setVisibility(View.GONE);
} else {
tableLayout.setVisibility(View.VISIBLE);
}
}
public static class ViewHolder {
// Child view
TextView label = null;
TextView value = null;
TextView tableHeader = null;
LinearLayout labelvalueContainer = null;
HorizontalScrollView tableScroll = null;
TableLayout tableLayout = null;
TableLayout tableLayoutScrollable = null;
// Group view
TextView tv = null;
}
}