我想在運行時以線性佈局的方式將多個gridview添加到scrollview。 但只顯示第一行。 請幫助如何將多個gridviews添加到android java中的scrollview?
回答
直到你把這裏的佈局,我可以假設只有你在你的線性佈局中設置了錯誤的水平/垂直參數。
您不能在Android中嵌套可滾動視圖 - 即ListView,GridView,ScrollView。
你可以給看看下面的代碼:
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
public class ScrollableGridView extends GridView {
boolean expanded = true;
public ScrollableGridView(Context context)
{
super(context);
}
public ScrollableGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ScrollableGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
這是一個好一點的版本的GridView的允許它幾乎工作嵌套,滾動型的時候。我說「幾乎可以工作」,因爲我發現它有時候太短或太長,20-30像素。 你應該注意到這會阻止視圖被重用,所以它比正常的GridView更重。
在我的情況下,我最終擴展了LinearLayout並使用它來對齊列中的子項。這不是很難 - 如果你願意,我可以給你舉例。 :)
我從this answer得到GridView示例。
下面是基於的LinearLayout一個GridView的例子:
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class GridLikeLayout extends LinearLayout {
private static final int DEFAULT_ITEMS_PER_ROW = 1;
private final int DEFAULT_COLUMN_WIDTH = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 150, getContext().getResources()
.getDisplayMetrics());
private int itemsPerRow = DEFAULT_ITEMS_PER_ROW;
private List<View> innerViews = null;
private int columnWidth;
public GridLikeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.GridLikeLayout);
// itemsPerRow = a.getInt(R.styleable.GridLikeLayout_columns,
// DEFAULT_ITEMS_PER_ROW);
try {
columnWidth = (int) a.getDimension(
R.styleable.GridLikeLayout_column_width, DEFAULT_COLUMN_WIDTH);
} catch (UnsupportedOperationException uoe) {
columnWidth = (int) a.getInt(
R.styleable.GridLikeLayout_column_width, DEFAULT_COLUMN_WIDTH);
}
setOrientation(LinearLayout.VERTICAL);
}
public GridLikeLayout(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
}
public void setInnerViews(List<View> innerViews) {
this.innerViews = innerViews;
processViews();
}
public List<View> getInnerViews() {
return innerViews;
}
protected void processViews() {
if (null != innerViews) {
LinearLayout innerContainer = null;
innerContainer = generateInnerContainer();
int childrenCount = innerViews.size();
for (int index = 0; index < childrenCount; ++index) {
if (isFull(innerContainer)) {
addInnerContainer(innerContainer);
innerContainer = generateInnerContainer();
}
View child = innerViews.get(index);
if (null != child.getParent()) {
((ViewGroup) child.getParent()).removeView(child);
}
addInnerView(innerContainer, child);
}
addInnerContainer(innerContainer);
}
}
protected boolean isFull(LinearLayout innerContainer) {
return 0 == (innerContainer.getChildCount() % itemsPerRow)
&& 0 < innerContainer.getChildCount();
}
protected void addInnerView(LinearLayout innerContainer, View child) {
int width = LayoutParams.WRAP_CONTENT;
int height = LayoutParams.WRAP_CONTENT;
LayoutParams innerParams = new LayoutParams(width, height);
innerParams.weight = 1;
innerParams.gravity = Gravity.CENTER;
innerContainer.addView(child, innerParams);
}
protected void addInnerContainer(LinearLayout innerContainer) {
LayoutParams params = generateDefaultLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
addView(innerContainer, params);
}
protected LinearLayout generateInnerContainer() {
LinearLayout innerContainer;
innerContainer = new LinearLayout(getContext());
innerContainer.setGravity(Gravity.CENTER);
return innerContainer;
}
public void setOnInnerViewClickListener(OnClickListener listener) {
for (View innerView : innerViews) {
innerView.setOnClickListener(listener);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED) {
if (columnWidth > 0) {
widthSize = columnWidth + getPaddingLeft() + getPaddingRight();
} else {
widthSize = getPaddingLeft() + getPaddingRight();
}
widthSize += getVerticalScrollbarWidth();
}
int childWidth = widthSize - getPaddingLeft() - getPaddingRight();
int columnsNumber = determineColumns(childWidth);
if (columnsNumber > 0 && columnsNumber != itemsPerRow) {
itemsPerRow = columnsNumber;
removeAllViews();
processViews();
}
}
protected int determineColumns(int availableSpace) {
int columnsNumber = itemsPerRow;
if (0 < columnWidth) {
columnsNumber = availableSpace/columnWidth;
}
return columnsNumber;
}
}
這是自定義我的資源文件屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="column_width" format="dimension|integer">
<enum name="single_column" value="-1" />
</attr>
<declare-styleable name="GridLikeLayout">
<attr name="column_width" />
</declare-styleable>
</resources>
下面是一個例子用法:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="@+id/itemsScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/itemsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<your_package.view.GridLikeLayout
xmlns:my="YOUR APPLICATION PACKAGE"
android:id="@+id/MyGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
my:column_width="260dp"
android:focusable="true"
android:gravity="center_horizontal"
android:padding="5dp" >
</your_package.GridLikeLayout>
<View
android:id="@+id/viewSeparator"
android:layout_width="fill_parent"
android:layout_height="2dp" />
<your_package.GridLikeLayout
xmlns:my="YOUR APPLICATION PACKAGE"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
my:column_width="single_column" >
</your_package.GridLikeLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
請告訴我,如果你有任何麻煩運行的例子 - 我無法嘗試在mome NT。所以如果你有問題,我會在稍後檢查。不要忘記更改軟件包名稱 - 「your_package」應該是存儲GridLikeLayout的軟件包,「您的應用軟件包」是您應用的軟件包 - 應用軟件清單中指定的軟件包。 :)
PLZ給我的例子... – Venkat 2012-09-11 05:08:09
@venkat,我已經添加了一個例子到我的文章。試一試,告訴我你是否需要任何幫助。 :) – 2012-09-17 08:26:35
你的第一個解決方案確實有效,但沒有考慮到GridView通常暗示圖像緩存,就像我的情況。這裏你的第一個解決方案每次開始滾動時都會導致OutOfMemoryError。 有沒有人已經有一個示例解決方案,它考慮了圖像緩存?
- 1. 如何在ScrollView中添加多個UIImageViews?
- 2. 如何將多個java類添加到Mono for Android項目中?
- 3. 將UIButtons添加到ScrollView中
- 4. Android如何添加ScrollView
- 5. 將多個視圖添加到LinearLayout android
- 6. 如何將ScrollView添加到佈局?
- 7. 將多個gridviews導出到Excel中
- 8. 如何添加ImageButtons到ScrollView?
- 9. 如何在Android XML中添加ScrollView?
- 10. 將不同的TextView添加到ScrollView中
- 11. Android在ScrollView中添加一個ListView
- 12. 如何在ScrollView中添加2個ListViews
- 13. 如何在iPhone和Android的ScrollView中添加近1000個視圖?
- 14. Swift:將圖像添加到ScrollView中
- 15. 如何在android中的scrollView中添加gridView android
- 16. 將scrollView添加到另一個scrollView不工作
- 17. 如何將ScrollView添加到我的TableviewController類中?
- 18. 如何在Swift中將圖像添加到ScrollView中
- 19. 將多個圓圈添加到JFrame? (Java)
- 20. 將ScrollView添加到我的viewController
- 21. 如何將多個TileOverlayOptions添加到GoogleMap?
- 22. 如何將多個jar添加到classpath?
- 23. 如何以編程方式在scrollView中添加多個按鈕
- 24. 如何在scrollview中添加多個圖像瀏覽?
- 25. 動態添加按鈕到ScrollView android
- 26. 在Android上動態添加TextView到ScrollView
- 27. 在Android上動態添加TextView到ScrollView
- 28. 將多個字節[]添加到Java中的一個數組中
- 29. 如何將uiview作爲子視圖添加到scrollview中?
- 30. 如何將多個JComponents添加到JPanel?
你可以添加布局 – user936414 2012-03-27 12:12:26