android – 我的gridview只显示一行

前端之家收集整理的这篇文章主要介绍了android – 我的gridview只显示一行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//我的xml代码
  1. <ScrollView
  2. android:id="@+id/scrollview"
  3. android:layout_width="wrap_content"
  4. android:layout_height="match_parent"
  5. android:layout_marginTop="35dp">
  6.  
  7. <RelativeLayout
  8. android:id="@+id/relativeLayout2"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:layout_marginBottom="5dp"
  12. android:layout_marginTop="5dp"
  13. android:background="@android:color/background_light"
  14. android:gravity="center"
  15. android:paddingBottom="5dp" >
  16.  
  17. <TextView
  18. android:id="@+id/textView4"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignLeft="@+id/textView3"
  22. android:layout_below="@+id/textView3"
  23. android:text="Venue,Date"
  24. android:textSize="12sp" />
  25.  
  26. <TextView
  27. android:id="@+id/textView5"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignLeft="@+id/textView4"
  31. android:layout_below="@+id/textView4"
  32. android:text="Description"
  33. android:textSize="12sp"
  34. android:paddingBottom="5dp" />
  35.  
  36. <ImageView
  37. android:id="@+id/imageView7"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:layout_alignParentLeft="true"
  41. android:layout_below="@+id/textView2"
  42. android:layout_marginTop="14dp"
  43. android:src="@drawable/demo" />
  44.  
  45. <TextView
  46. android:id="@+id/textView2"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_alignParentTop="true"
  50. android:layout_centerHorizontal="true"
  51. android:layout_marginTop="5dp"
  52. android:text="Event of the Week" />
  53.  
  54. <TextView
  55. android:id="@+id/textView3"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_alignParentLeft="true"
  59. android:layout_below="@+id/imageView7"
  60. android:layout_marginLeft="14dp"
  61. android:text="Event Name" />
  62.  
  63. <GridView
  64. android:id="@+id/gridView1"
  65. android:layout_width="match_parent"
  66. android:layout_height="fill_parent"
  67. android:layout_alignParentLeft="true"
  68. android:layout_below="@+id/textView6"
  69. android:layout_marginTop="14dp"
  70. android:horizontalSpacing="10dp"
  71. android:numColumns="2"
  72. android:paddingBottom="5dp"
  73. android:verticalSpacing="10dp" />
  74.  
  75. <TextView
  76. android:id="@+id/textView6"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_below="@+id/textView5"
  80. android:layout_centerHorizontal="true"
  81. android:layout_marginTop="14dp"
  82. android:text="Today&apos;s Events" />
  83.  
  84. </RelativeLayout>
  85. </ScrollView>

//主要活动

  1. class LoadProfile extends AsyncTask<String,String,String> {
  2.  
  3. @Override
  4. protected void onPreExecute() {
  5. super.onPreExecute();
  6. pDialog = new ProgressDialog(EventHome.this);
  7. pDialog.setMessage("Loading...");
  8. pDialog.setIndeterminate(false);
  9. pDialog.setCancelable(false);
  10. pDialog.show();
  11. }
  12.  
  13. protected String doInBackground(String... args) {
  14. // Building Parameters
  15. String json = null;
  16. PROFILE_URL = "http://www.example.com/filter_event_android.PHP?pin="+phone;
  17. try {
  18. List<NameValuePair> params = new ArrayList<NameValuePair>();
  19.  
  20. HttpClient httpclient = new DefaultHttpClient();
  21. HttpPost httppost = new HttpPost(PROFILE_URL);
  22. httppost.setEntity(new UrlEncodedFormEntity(params));
  23.  
  24. // Execute HTTP Post Request
  25. HttpResponse response = httpclient.execute(httppost);
  26. HttpEntity resEntity = response.getEntity();
  27. json = EntityUtils.toString(resEntity);
  28.  
  29. Log.i("All Events: ",json.toString());
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. return json;
  35. }
  36.  
  37. @SuppressLint("InlinedApi") @Override
  38. protected void onPostExecute(String json) {
  39. super.onPostExecute(json);
  40. pDialog.dismiss();
  41. try{
  42. event = new JSONObject(json);
  43. final ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
  44. JSONArray user = event.getJSONArray("events");
  45. String contains=json.toString();
  46.  
  47. if(contains.contains("id"))
  48. {
  49. for (int i = 0; i < user.length(); i++) {
  50. JSONObject object = user.getJSONObject(i);
  51.  
  52. HashMap<String,String> map = new HashMap<String,String>();
  53. map.put("id",object.getString("id"));
  54. map.put("name",object.getString("name"));
  55. map.put("date_d",object.getString("date_d"));
  56. map.put("location",object.getString("location"));
  57. map.put("images","http://www.example.com/"+object.getString("images"));
  58. arraylist.add(map);
  59. }
  60.  
  61. String[] from = {"name","date_d","location","images"};
  62. int[] to = {R.id.textView1,R.id.textView2,R.id.textView3,R.id.iv_flag};
  63.  
  64. ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to);
  65. gv1.setAdapter(adapters);
  66. }
  67. else
  68. {
  69. gv1.setVisibility(View.GONE);
  70. TextView dynamicTextView = new TextView(EventHome.this);
  71. dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  72. dynamicTextView.setText("No events available");
  73. }
  74.  
  75. gv1.setOnItemClickListener(new OnItemClickListener() {
  76. @Override
  77. public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
  78. // TODO Auto-generated method stub
  79. Intent i = new Intent(EventHome.this,EventSingle.class);
  80. i.putExtra("event_id",arraylist.get(arg2).get("id"));
  81. startActivity(i);
  82. }
  83. });
  84.  
  85. }catch(Exception e)
  86. {
  87. e.printStackTrace();
  88. }
  89.  
  90. }
  91. }

//MyAdapter.java

  1. public class MyAdapter extends SimpleAdapter{
  2.  
  3. public MyAdapter(Context context,List<? extends Map<String,?>> data,int resource,String[] from,int[] to){
  4. super(context,data,resource,to);
  5. }
  6.  
  7. public View getView(int position,View convertView,ViewGroup parent){
  8. // here you let SimpleAdapter built the view normally.
  9. View v = super.getView(position,convertView,parent);
  10.  
  11. // Then we get reference for Picasso
  12. ImageView img = (ImageView) v.getTag();
  13. if(img == null){
  14. img = (ImageView) v.findViewById(R.id.iv_flag);
  15. v.setTag(img); // <<< THIS LINE !!!!
  16. }
  17. // get the url from the data you passed to the `Map`
  18. @SuppressWarnings("rawtypes")
  19. String url = (String) ((Map)getItem(position)).get("images");
  20. // do Picasso
  21. Picasso.with(v.getContext()).load(url).into(img);
  22.  
  23. // return the view
  24. return v;
  25. }
  26. }

上面的布局在gridview中只显示了一行值.但是我对gridview有5个以上的值.为什么它没有显示其他值.我尝试了很多但没有用.有没有人有解决方案.

解决方法

这可能是由ScrollView中的GridView引起的.由于两个布局都是可滚动的,这会导致很多问题,在这种情况下,无法正确确定GridView的高度,并且ScrollView会使用滚动事件.

使用ListView,您可以简单地将ListView声明为根元素,然后将其他可滚动内容添加为页眉或页脚. GridView本身不支持这个,但幸运的是有HeaderGridView的子类实现解决了这个问题.

你应该做的只是将HeaderGridView放到你的活动膨胀的xml中.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <your.package.HeaderGridView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/gridView1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:layout_alignParentLeft="true"
  7. android:horizontalSpacing="10dp"
  8. android:numColumns="2"
  9. android:verticalSpacing="10dp" />

然后将RelativeLayout实现为不同xml中的标题视图(例如header.xml)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/relativeLayout2"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:layout_marginBottom="5dp"
  7. android:layout_marginTop="5dp"
  8. android:background="@android:color/background_light"
  9. android:gravity="center"
  10. android:paddingBottom="5dp">
  11.  
  12. <TextView
  13. android:id="@+id/textView4"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignLeft="@+id/textView3"
  17. android:layout_below="@+id/textView3"
  18. android:text="Venue,Date"
  19. android:textSize="12sp" />
  20.  
  21. <TextView
  22. android:id="@+id/textView5"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignLeft="@+id/textView4"
  26. android:layout_below="@+id/textView4"
  27. android:text="Description"
  28. android:textSize="12sp"
  29. android:paddingBottom="5dp" />
  30.  
  31. <ImageView
  32. android:id="@+id/imageView7"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentLeft="true"
  36. android:layout_below="@+id/textView2"
  37. android:layout_marginTop="14dp"
  38. android:src="@drawable/demo" />
  39.  
  40. <TextView
  41. android:id="@+id/textView2"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_alignParentTop="true"
  45. android:layout_centerHorizontal="true"
  46. android:layout_marginTop="5dp"
  47. android:text="Event of the Week" />
  48.  
  49. <TextView
  50. android:id="@+id/textView3"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_alignParentLeft="true"
  54. android:layout_below="@+id/imageView7"
  55. android:layout_marginLeft="14dp"
  56. android:text="Event Name" />
  57.  
  58. <TextView
  59. android:id="@+id/textView6"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_below="@+id/textView5"
  63. android:layout_centerHorizontal="true"
  64. android:layout_marginTop="14dp"
  65. android:text="Today&apos;s Events" />
  66.  
  67. </RelativeLayout>

在您的活动中,您将标题添加到HeaderGridView

  1. HeaderGridView gridView = (HeaderGridView) findViewById(R.id.gridView1);
  2. View header = getLayoutInflater().inflate(R.layout.header,gridView,false);
  3. gridView.addHeaderView(header);
  4. // set adapter AFTER adding headerViews
  5. gridView.setAdapter(MyAdapter(...))

尝试过它,就像一个魅力.希望能帮助到你!

猜你在找的Android相关文章