您可以通过在网格项上放置一个弹出窗口来实现。 为了找到你可以使用下面的代码
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,您将需要修改的布局,你可以对电网项目的点击显示覆盖图图像
编辑:实施情况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>
多少图像你想对电网项目的顶部被点击 – Atrix1987 2013-03-18 12:43:46
只有一个图像中的中心 – Goofy 2013-03-18 12:44:21
检查编辑答案 – Atrix1987 2013-03-18 13:03:53