创建具有可点击图像的 Gridview,Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11947452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Gridview having clickable images,Android
提问by Harshit Syal
I want to create a gridview having clickable images..
我想创建一个具有可点击图像的网格视图..
When ever an image is clicked a corresponding value will be shown below that grid view.
当单击图像时,相应的值将显示在该网格视图下方。
The problem I am facing is in designing part, I dont know how to design a grid view like this.. every time I try to do that I get some bad results.. I have no android ui designing experience as of now.
我面临的问题是在设计部分,我不知道如何设计这样的网格视图..每次我尝试这样做时都会得到一些不好的结果..我现在没有android ui设计经验。
Please Help !
请帮忙 !
回答by Avi Kumar Manku
GridView
is a ViewGroup
that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter
.
GridView
是ViewGroup
在二维、可滚动网格中显示项目的 。网格项目使用ListAdapter
.
For an introduction to how you can dynamically insert views using an adapter, read Building Layouts with an Adapter.
有关如何使用适配器动态插入视图的介绍,请阅读使用适配器构建布局。
http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
and These are good GridView
tutorials will help you
这些很好的GridView
教程会帮助你
http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
http://www.mkyong.com/android/android-gridview-example/
http://www.mkyong.com/android/android-gridview-example/
and
和
http://developer.android.com/guide/topics/ui/layout/gridview.html
http://developer.android.com/guide/topics/ui/layout/gridview.html
回答by Chaitu
回答by Sunil
Try this
尝试这个
Main activity
public class MainActivity extends AppCompatActivity { List<String> list; int[] imageId = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, }; String[] web = { "Google", "Github", "Instagram", "Facebook", "Flickr", "Pinterest", "Quora", "Twitter", "Vimeo", "WordPress", "Youtube", "Stumbleupon", "SoundCloud", "Reddit", "Blogger" } ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageAdapter adapter = new ImageAdapter(MainActivity.this,web, imageId); GridView grid=(GridView)findViewById(R.id.grid_view); grid.setAdapter(adapter); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mypc.grid.MainActivity"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="2" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView> </LinearLayout>
ImageAdapter class
public class ImageAdapter extends BaseAdapter { private Context mContext; private final int[] Imageid; private final String[] web; public ImageAdapter(Context c,String[] web,int[] Imageid ) { mContext = c; this.Imageid = Imageid; this.web=web; } @Override public int getCount() { return Imageid.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View gridView; if (convertView == null) { gridView = new View(mContext); // get layout from mobile.xml gridView = inflater.inflate(R.layout.grid_layout, null); // set value into textview TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label); textView.setText(web[position]); // set image based on selected text ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image); imageView.setImageResource(Imageid[position]); } else { gridView = (View) convertView; } return gridView; } }
grid_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/grid_item_image" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginRight="10dp" > </ImageView> <TextView android:id="@+id/grid_item_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:layout_marginTop="5px" android:textSize="15px" > </TextView> </LinearLayout>
主要活动
public class MainActivity extends AppCompatActivity { List<String> list; int[] imageId = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, }; String[] web = { "Google", "Github", "Instagram", "Facebook", "Flickr", "Pinterest", "Quora", "Twitter", "Vimeo", "WordPress", "Youtube", "Stumbleupon", "SoundCloud", "Reddit", "Blogger" } ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageAdapter adapter = new ImageAdapter(MainActivity.this,web, imageId); GridView grid=(GridView)findViewById(R.id.grid_view); grid.setAdapter(adapter); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } }
活动主
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mypc.grid.MainActivity"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="2" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView> </LinearLayout>
ImageAdapter 类
public class ImageAdapter extends BaseAdapter { private Context mContext; private final int[] Imageid; private final String[] web; public ImageAdapter(Context c,String[] web,int[] Imageid ) { mContext = c; this.Imageid = Imageid; this.web=web; } @Override public int getCount() { return Imageid.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View gridView; if (convertView == null) { gridView = new View(mContext); // get layout from mobile.xml gridView = inflater.inflate(R.layout.grid_layout, null); // set value into textview TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label); textView.setText(web[position]); // set image based on selected text ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image); imageView.setImageResource(Imageid[position]); } else { gridView = (View) convertView; } return gridView; } }
网格布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/grid_item_image" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginRight="10dp" > </ImageView> <TextView android:id="@+id/grid_item_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:layout_marginTop="5px" android:textSize="15px" > </TextView> </LinearLayout>