目前我正在尝试创建一个自定义视图,该视图显示一个项目网格,其中位置0上的项目占用2个跨度。由于我无法使用简单的gridview创建它,我试图创建一个有点类似的行为,它由带有TableLayout的ScrollView组成,其中包含来自适配器的视图。它在一个简单的应用程序上工作得非常好,我只需要显示内容但是一旦我在viewPager上包含的片段上添加了我的工作,所有内容都超出了屏幕的宽度,我不知道为什么。我已经将fillViewPort
设置为true但是没有任何反应,我尝试设置表格的布局参数并仍然没有解决它。
到目前为止,这是我的代码:
public class CustomHeaderGridView extends ScrollView {
TableLayout tableLayout;
private Adapter adapter;
private final DataSetObserver observer = new DataSetObserver() {
@Override
public void onChanged() {
setAdapter(adapter);
}
@Override
public void onInvalidated() {
removeAllViews();
}
};
public CustomHeaderGridView(Context context) {
super(context);
}
public CustomHeaderGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomHeaderGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Adapter getAdapter() {
return adapter;
}
public void setAdapter(Adapter adapter) {
if (this.adapter != null) {
this.adapter.unregisterDataSetObserver(observer);
}
this.adapter = adapter;
if (this.adapter != null) {
this.adapter.registerDataSetObserver(observer);
}
initViewsFromAdapter();
}
protected void initViewsFromAdapter() {
removeAllViews();
tableLayout = new TableLayout(getContext());
tableLayout.setStretchAllColumns(true);
TableLayout.LayoutParams table_params = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
tableLayout.setLayoutParams(table_params);
addView(tableLayout);
if (adapter != null) {
/*for (int i = 0; i < adapter.getCount(); i++) {
TableRow row_header = new TableRow(getContext());
row_header.addView(adapter.getView(i,null,null));
//row_header.addView(adapter.getView(i, null, this), i);
tableLayout.addView(row_header);
}*/
setCustomTabItems();
}
}
protected void refreshViewsFromAdapter() {
int childCount = getChildCount();
int adapterSize = adapter.getCount();
int reuseCount = Math.min(childCount, adapterSize);
for (int i = 0; i < reuseCount; i++) {
adapter.getView(i, getChildAt(i), this);
}
if (childCount < adapterSize) {
for (int i = childCount; i < adapterSize; i++) {
addView(adapter.getView(i, null, this), i);
}
} else if (childCount > adapterSize) {
removeViews(adapterSize, childCount);
}
}
public void setColumnCount(int col_count){
this.col_count = col_count;
}
int col_count = 2; //min 2 cols
private void setCustomTabItems(){
if(col_count==2){
if(adapter.getCount()>0) {
//header here
TableRow row_header = new TableRow(getContext());
View view_header = adapter.getView(0,null,null);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 2;
view_header.setLayoutParams(params);
//todo add listener
row_header.addView(view_header);
tableLayout.addView(row_header);
for (int i = 1; i < adapter.getCount(); i += col_count) {
TableRow row = new TableRow(getContext());
for (int ctr = 0; ctr < col_count; ctr++) {
try {
View view = adapter.getView(i + ctr,null,null);
//todo add listener
row.addView(view);
}catch (Exception e){
//Out of bounds since the count is not always even
}
}
tableLayout.addView(row);
}
}
} else {
//do something for col count more than 2
if(adapter.getCount()>0) {
//header here
TableRow row_header = new TableRow(getContext());
View view_header = adapter.getView(0, null, null);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 2;
ImageView img_header = (ImageView) view_header.findViewById(R.id.img_item);
ViewGroup.LayoutParams header_image_params = img_header.getLayoutParams();
header_image_params.height = header_image_params.height*2 + 5;
img_header.setLayoutParams(header_image_params);
view_header.setLayoutParams(params);
//todo add listener
row_header.addView(view_header);
//add the other subviews here that will be on the side of the header view
int start_header_item = 1;
for(int i = 0; i < col_count - 2; i++){
LinearLayout vertical_container = new LinearLayout(getContext());
TableRow.LayoutParams layout_params = new TableRow.LayoutParams();
layout_params.span = 1;
vertical_container.setLayoutParams(layout_params);
vertical_container.setOrientation(LinearLayout.VERTICAL);
//add the content views here
for(int ctr = start_header_item; ctr <= start_header_item + 1; ctr++){
try {
View view = adapter.getView(ctr, null, null);
//todo add listener
vertical_container.addView(view);
}catch (Exception e){
//Out of bounds since the count is not always even
}
}
start_header_item+=2;
row_header.addView(vertical_container);
}
tableLayout.addView(row_header);
int start_item = (col_count + col_count - 3);
for (int i = start_item; i < adapter.getCount(); i += col_count) {
TableRow row = new TableRow(getContext());
for (int ctr = 0; ctr < col_count; ctr++) {
try {
View view = adapter.getView(i + ctr,null,null);
//todo add listener
row.addView(view);
}catch (Exception e){
//Out of bounds since the count is not always even
}
}
tableLayout.addView(row);
}
}
}
}
}
用于显示只需使用
MyAdapter adapter = new MyAdapter(getActivity(), items);
content_tab = (CustomHeaderGridView) view.findViewById(R.id.grid_content);
content_tab.setAdapter(adapter);
请注意,这仍在进行中,所以我还没有应用任何增强功能,而且我还有一个硬编码的ID
ImageView img_header =ImageView)view_header.findViewById(R.id.img_item);
这只是为了通过乘以它的高度来使图像更大。现在通常它看起来像没有在视图寻呼机上包含它:
但在我的应用程序上,它看起来像这样。 (对不起,如果我需要覆盖图像)
有人能指出我在这里想念的东西吗?