2013-04-12 138 views
0

我想顯示一個元素的網格,每個元素由一個包含在TextView頂部的ImageView的RelativeLayout組成。在Android Gridview中顯示覆合項目

我已經看到someone else問同樣的問題,我已經採用了the proposed solution;無論如何,在這個解決方案中,getView()函數只返回一個ImageView項目,而不是TextView。我試圖返回一個自定義項目,它擴展了RelativeLayout,但我無法看到Gridview中的元素。如果點擊它們,我仍然會得到Toast消息(請參閱下面的偵聽器代碼),但只有Gridview的(灰色)背景可以在屏幕上看到。

如果我僅通過ImageView或TextView,正如上面的解決方案中所建議的那樣,我仍然可以看到單個項目。所以我的問題是如何管理getView在自定義View中返回兩個項目。

這裏是我到目前爲止的代碼:

活動類:

List<String> mItemsImage; 
List<String> mItemsText; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mylayout); 

    mItemsImage = new ArrayList<String>(); 
    mItemsText = new ArrayList<String>(); 

    mGridView = (GridView) findViewById(R.id.mygridview); 

    fillArraylists(); 

    mGridView.setAdapter(new CustomItemAdapter(this, mItemsImage, mItemsText)); 

    mGridView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(MyActivity.this, "text is + "mItemsText.get(position), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

適配器類:

class CustomItemAdapter extends BaseAdapter{ 
    private Context mContext; 
    private List<String> mImageList; 
    private List<String> mTextList; 

    public CustomItemAdapter(Context c, List<String> imageList, List<String> textList) { 
     mContext = c; 
     mImageList = imageList; 
     mTextList = textList; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     CustomItem item; 

     if (convertView == null) { // if it's not recycled, initialize some attributes 
      item = new CustomItem(mContext, mImageList.get(position), mTextList.get(position)); 
      item.findViewById(R.id.grid_item);//Tried to add this line in a second moment, but it didn't work anyway 
      item.setLayoutParams(new GridView.LayoutParams(parent.getWidth() >> 2, parent.getHeight() >> 2)); 
     } else { 
      item = (CustomItem) convertView; 
     } 
     return item; 
     //if I do 'return item.image' or 'return item.text' I can see the View on screen normally 
    } 
} 

定製項目類:

class CustomItem extends RelativeLayout{ 
    public ImageView image; 
    public TextView text; 

    public CustomItem(Context ctx, String imagepath, String description){ 
     super(ctx); 

     image = new ImageView(ctx); 
     image.findViewById(R.id.grid_item_image); 
     image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     image.setPadding(8, 8, 8, 8); 
     image.setImageBitmap(BitmapFactory.decodeFile(imagepath)); 

     text = new TextView(ctx); 
     text.findViewById(R.id.grid_item_text); 
     text.setPadding(8, 0, 8, 0); 
     text.setText(description); 
    } 
} 

mylayout.xml:

... 

<GridView 
    android:id="@+id/mygridview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background_lightgrey_rect" 
    android:gravity="center" 
    android:numColumns="2" 
    android:stretchMode="columnWidth" > 
</GridView> 

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/grid_item" > 

    <ImageView 
     android:id="@+id/grid_item_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/default_image" 
     android:contentDescription="@string/image_not_found" /> 

    <TextView 
     android:id="@+id/grid_item_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/grid_item_image" 
     android:layout_centerHorizontal="true" 
     android:text="@string/image_not_found" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</RelativeLayout> 

+0

使用層次結構視圖來確定發生了什麼問題。 – CommonsWare

回答

0

好吧,我發現 - 解決方案 - 的一部分。

此基礎上this,我編輯的getView方法如下:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    RelativeLayout item; 

    if (convertView == null) { // if it's not recycled, initialize some attributes 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     item = (RelativeLayout) inflater.inflate(R.layout.grid_item, null, true); 
     ImageView image = (ImageView) item.findViewById(R.id.grid_item_image); 
     image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     image.setImageBitmap(BitmapFactory.decodeFile(mImageList.get(position))); 

     TextView text = (TextView) item.findViewById(R.id.grid_item_text); 
     text.setText(mTextList.get(position)); 
     item.setLayoutParams(new GridView.LayoutParams(parent.getWidth() >> 2, parent.getHeight() >> 2)); 

    } else { 
     item = (RelativeLayout) convertView; 
    } 

    return item; 
} 

我不再需要定製項目類。

grid_item.xml現在是:

<ImageView 
    android:id="@+id/grid_item_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/grid_item_text" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:contentDescription="@string/image_not_found" 
    android:src="@drawable/default_image" /> 

<TextView 
    android:id="@+id/grid_item_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="@string/image_not_found" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

(我仍然有GridView的一些可視化問題,雖然第一個元素是失蹤了,我可以過去的方式滾動的第二個元素以上)。