我正在尝试创建一个水平&一个垂直gridview,具有滚动功能。
我正在尝试关注博客Horizontal scroll
**在上面的博客中,他们正在从/ drawable文件夹加载图片。
使图像网格视图水平的源代码实现使用此功能,我可以水平滚动图像gridview。
使用Listview我也可以垂直加载图像。
现在按照上面的博客,水平网格视图中的每个图像都会添加一个按钮,如下所示。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<com.devsmart.android.ui.HorizontalListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:background="#ddd"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dip">
<ListView android:layout_width="200dip"
android:layout_height="wrap_content"
android:id="@+id/myverticalView"
android:layout_weight="1">
</ListView>
</LinearLayout>
</LinearLayout>
viewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:scaleType="centerCrop"
/>
<Button
android:id="@+id/clickbutton"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignBottom="@+id/image"
android:layout_alignLeft="@+id/image"
android:layout_marginBottom="56dp"
android:layout_marginLeft="64dp"
android:gravity="center"
android:padding="16dip" />
<TextView
android:id="@+id/title"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/image"
android:layout_below="@+id/image"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:gravity="center"
android:textColor="#000" />
</RelativeLayout>
现在位于 MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
}
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3","Text #4","Text #5"};
private BaseAdapter mAdapter = new BaseAdapter() {
private OnClickListener mOnButtonClicked = new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("hello from " + v);
builder.setPositiveButton("Cool", null);
builder.show();
}
};
@Override
public int getCount() {
return image.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
// Initialize the views in the layout
ImageView iv = (ImageView) retval.findViewById(R.id.image);
int pos = position;
// Set the views in the layout
iv.setBackgroundResource(image[pos]);
TextView title = (TextView) retval.findViewById(R.id.title);
title.setText("newImage");
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
return retval;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
现在在上面的代码点击按钮事件,他们可以得到哪个按钮 单击,
但我想知道我想要点击哪个图像的按钮 将该图像(单击水平网格视图图像)添加到verticale 列表图像视图。
任何人都可以指导我如何才能知道点击了哪个图片的按钮?