我的gridview只显示一行

Jit*_*ese 9 android gridview android-layout

//我的xml代码

<ScrollView
    android:id="@+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="35dp">

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/background_light"
    android:gravity="center"
    android:paddingBottom="5dp" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="Venue, Date"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Description"
        android:textSize="12sp"
        android:paddingBottom="5dp" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="14dp"
        android:src="@drawable/demo" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="Event of the Week" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView7"
        android:layout_marginLeft="14dp"
        android:text="Event Name" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="14dp"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:paddingBottom="5dp"
        android:verticalSpacing="10dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Today&apos;s Events" />

    </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

//主要活动

class LoadProfile extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EventHome.this);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        // Building Parameters
        String json = null;
        PROFILE_URL = "http://www.example.com/filter_event_android.php?pin="+phone;
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(PROFILE_URL);
            httppost.setEntity(new UrlEncodedFormEntity(params));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            json = EntityUtils.toString(resEntity);

            Log.i("All Events: ", json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }

    @SuppressLint("InlinedApi") @Override
    protected void onPostExecute(String json) {
        super.onPostExecute(json);
        pDialog.dismiss();
        try{
            event = new JSONObject(json);
        final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
        JSONArray user = event.getJSONArray("events");
        String contains=json.toString();

        if(contains.contains("id"))
        {
            for (int i = 0; i < user.length(); i++) {
                JSONObject object = user.getJSONObject(i);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", object.getString("id"));
                map.put("name", object.getString("name"));
                map.put("date_d", object.getString("date_d"));
                map.put("location", object.getString("location"));
                map.put("images", "http://www.example.com/"+object.getString("images"));
                arraylist.add(map);    
            }

            String[] from = {"name", "date_d", "location", "images"};
            int[] to = {R.id.textView1, R.id.textView2, R.id.textView3,  R.id.iv_flag};

            ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to);
            gv1.setAdapter(adapters);
        }
        else
        {
            gv1.setVisibility(View.GONE);
            TextView dynamicTextView = new TextView(EventHome.this);
            dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            dynamicTextView.setText("No events available");
        }

        gv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Intent i = new Intent(EventHome.this,EventSingle.class);
                i.putExtra("event_id", arraylist.get(arg2).get("id"));
                startActivity(i);
            }
        });

        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

//MyAdapter.java

public class MyAdapter extends SimpleAdapter{

   public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
      super(context, data, resource, from, to);
}

   public View getView(int position, View convertView, ViewGroup parent){
      // here you let SimpleAdapter built the view normally.
      View v = super.getView(position, convertView, parent);

      // Then we get reference for Picasso
      ImageView img = (ImageView) v.getTag();
      if(img == null){
         img = (ImageView) v.findViewById(R.id.iv_flag);
         v.setTag(img); // <<< THIS LINE !!!!
      }
      // get the url from the data you passed to the `Map`
      @SuppressWarnings("rawtypes")
    String url = (String) ((Map)getItem(position)).get("images");
      // do Picasso
      Picasso.with(v.getContext()).load(url).into(img);

      // return the view
      return v;
   }
}
Run Code Online (Sandbox Code Playgroud)

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

Lam*_*rak 15

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

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

你应该做的只是把HeaderGridView你的xml 放到活动中.

<?xml version="1.0" encoding="utf-8"?>
<your.package.HeaderGridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:verticalSpacing="10dp" />
Run Code Online (Sandbox Code Playgroud)

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/background_light"
    android:gravity="center"
    android:paddingBottom="5dp">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="Venue, Date"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Description"
        android:textSize="12sp"
        android:paddingBottom="5dp" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="14dp"
        android:src="@drawable/demo" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="Event of the Week" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView7"
        android:layout_marginLeft="14dp"
        android:text="Event Name" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Today&apos;s Events" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

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

HeaderGridView gridView = (HeaderGridView) findViewById(R.id.gridView1);
View header = getLayoutInflater().inflate(R.layout.header, gridView, false);
gridView.addHeaderView(header);
// set adapter AFTER adding headerViews
gridView.setAdapter(MyAdapter(...))
Run Code Online (Sandbox Code Playgroud)

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