2013-03-18 68 views
2

我有4個網格項目的視圖,現在點擊每個網格項目,我需要在點擊的網格項目頂部再顯示一個圖像。在網格項目頂部顯示多一個圖片

如何實現這個目標?

This is what is intended

這是我的適配器類獲得查看:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder view; 
    LayoutInflater inflator = activity.getLayoutInflater(); 

    GridView.LayoutParams params = null; 
    if (convertView == null) { 
    view = new ViewHolder(); 
    convertView = inflator.inflate(R.layout.tile_row, null); 

    view.textView = (TextView) convertView.findViewById(R.id.title); 
    view.imageView = (ImageView) convertView.findViewById(R.id.icons); 
    convertView.setTag(view); 

    params = new GridView.LayoutParams(Width, Height); 
    convertView.setLayoutParams(params); 
    } else { 
    view = (ViewHolder) convertView.getTag(); 
    } 
    // set image view parameters 
    setImageParamaeters(view.imageView); 

    view.textView.setText(adapterNames.get(position)); 
    view.imageView.setImageResource(adapterIcondIds.get(position)); 

    if (params != null) { 
    convertView.setLayoutParams(params); 
    } 

    convertView.setId(position); 
    convertView.setOnClickListener(onClickListeners.get(position)); 

    return convertView; 
} 
+0

多少圖像你想對電網項目的頂部被點擊 – Atrix1987 2013-03-18 12:43:46

+0

只有一個圖像中的中心 – Goofy 2013-03-18 12:44:21

+0

檢查編輯答案 – Atrix1987 2013-03-18 13:03:53

回答

2

您可以通過在網格項上放置一個彈出窗口來實現。 爲了找到你可以使用下面的代碼

Rect loc = new Rect(); 
int[] location = new int[2]; 
view.getLocationOnScreen(location); 
loc.left = location[0]; 
loc.top = location[1]; 
loc.right = loc.left + view.getWidth(); 
loc.bottom = loc.top + view.getHeight(); 

變量loc電網項目的屏幕位置的確切持有被點擊的view的屏幕位置準確,這不過是第二放慢參數的方法

@Override 
public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 
    // TODO Auto-generated method stub 
} 

有了這些信息,你可以很容易地實現你所需要的。最佳方法是計算視圖的中心並將彈出窗口的中心與點擊視圖的中心對齊。

注意:如果視圖的一部分可見,您需要調整以顯示彈出窗口。

編輯: ,因爲我沒有得到你的問題,我加入了一個形象帶來更清晰。

我已經提供的代碼情況1.對於案例2,您將需要修改的佈局,你可以對電網項目的點擊顯示覆蓋圖圖像 Possible cases

編輯:實施情況2

修改適配器代碼,移動周圍的一些變量優化

public CustomAdapter(Activity activity, ...){ 
    inflator = activity.getLayoutInflater(); 
... 
} 
LayoutInflater inflator; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder view; 

    if (convertView == null) { 
     view = new ViewHolder(); 
     convertView = inflator.inflate(R.layout.tile_row, null); 

     view.textView = (TextView) convertView.findViewById(R.id.title); 
     view.imageView = (ImageView) convertView.findViewById(R.id.icons); 
     view.clickedImage = (ImageView) convertView.findViewById(R.id.clickedImage); 
     convertView.setTag(view); 
    } else { 
     view = (ViewHolder) convertView.getTag(); 
    } 
    // set image view parameters 
    view.textView.setText(adapterNames.get(position)); 
    view.imageView.setImageResource(adapterIcondIds.get(position)); 
    //add a int variable to the view holder 
    view.position = position; 
    if(position != clickedPosition){ 
     view.clickedImage.setVisibility(View.GONE); 
    } 
    convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ViewHolder holder = (ViewHolder)v.getTag(); 
      clickedPosition = holder.position; 
      holder.clickedImage.setVisibility(View.VISIBLE); 
      //you can also set the resource of the imageview here. 
      notifyDataSetChanged(); 
     } 
    }); 

    return convertView; 
} 
//this is used for resetting the previous view when a new view is clicked 
int clickedPosition = -1; 

爲了保存網格視圖中項目的點擊狀態,我存儲網格的點擊項目位置 。現在在onClick我更改點擊圖像(處於GONE狀態)的可見性 ,更新變量 clickedPosition,並只需調用notifyDataSetChanged。這表明 數據集已更改的適配器以及所有可見網格項目的getView都將被調用。所以,如果您最初點擊項目1,然後點擊項目3,則您會注意到當項目3被點擊時項目1已被重置 。

XML title_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="200dp" 
    android:layout_height="200dp" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/droid_logo_black" 
     android:visibility="gone" /> 

    <ImageView 
     android:id="@+id/clickedImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/login_help_logo" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Title" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 
+0

嘿我想在點擊的網格物品上再顯示一個圖像,但是你的回答在這裏有所不同 – Goofy 2013-03-18 12:17:30

+0

你可以添加一個圖來解釋,我得到的是,如果你點擊物品3,它們將物品3壓下如果你確認我可以添加更多的代碼 – Atrix1987 2013-03-18 12:19:33

+0

不,我的網格項目不會改變,他們仍然存在,只要點擊一個項目顯示該點擊的項目的頂部多一個圖像 – Goofy 2013-03-18 12:20:57

0

在元素的onClickListener,調用ImageView的和動態加載的圖像。 或者預定義圖片資源,並用新圖片的ID調用Imageview。

+0

傢伙只有設置的選項圖像作爲背景?但是你怎麼把它設置爲froeground? – Goofy 2013-03-18 12:02:02

0

onlclick of each grid item i need to display one more image on top of that clicked grid item.

我認爲你必須創建自定義網格視圖。並根據需要動態追加ImageView。

setVisibilty(View.Visible)當你想顯示和setVisibilty(View.Hide)當你不想顯示。

+0

不,我不需要那麼做 – Goofy 2013-03-18 12:05:23

+0

okay.please詳細描述你的問題。 – 2013-03-18 12:14:16

0
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null,  null); 

嘗試這種東西,你會得到的ID。嘗試改變圖像使用

imageview.setImageResource(id); 

祝你好運!