From cc2cb085dd053d7b6459214ca158b1e19a57c6d6 Mon Sep 17 00:00:00 2001 From: YongdongHe Date: Thu, 4 Feb 2016 00:39:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复显示参与活动用户的Listview不能展开显示的问题 --- .../hifriends/activity/LookInfoActivity.java | 3 +++ .../activity/adapter/UserListAdapter.java | 24 +++++++++++++++++++ .../activity/beans/ActivityInfo.java | 4 ++-- .../layout/listview_activity_info_item.xml | 1 + 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/club/hifriends/activity/LookInfoActivity.java b/app/src/main/java/club/hifriends/activity/LookInfoActivity.java index ef823ac..327abca 100644 --- a/app/src/main/java/club/hifriends/activity/LookInfoActivity.java +++ b/app/src/main/java/club/hifriends/activity/LookInfoActivity.java @@ -108,6 +108,9 @@ public void onResponse(String response) { tv_content.setText(activityInfo.getContent()); liv_partner.setAdapter(new UserListAdapter(getBaseContext(), R.layout.listview_activity_info_item, activityInfo.getPartnerArrayList())); + + //根据listview的item数目和高度设置本身的高度,以保证listview能全部展开展示 + UserListAdapter.setListViewHeightBasedOnChildren(liv_partner); } else if (json_res.getInt("code") == 404) { Toast.makeText(getBaseContext(),"该活动不存在或者已被删除",Toast.LENGTH_SHORT).show(); onBackPressed(); diff --git a/app/src/main/java/club/hifriends/activity/adapter/UserListAdapter.java b/app/src/main/java/club/hifriends/activity/adapter/UserListAdapter.java index 8b9dfed..87f2f35 100644 --- a/app/src/main/java/club/hifriends/activity/adapter/UserListAdapter.java +++ b/app/src/main/java/club/hifriends/activity/adapter/UserListAdapter.java @@ -9,6 +9,8 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; +import android.widget.ListAdapter; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -50,4 +52,26 @@ public void onClick(View v) { }); return view; } + + public static void setListViewHeightBasedOnChildren(ListView listView) { + //根据listview的item数目设置宽度 + ListAdapter listAdapter = listView.getAdapter(); + if (listAdapter == null) { + return; + } + int totalHeight = 0; + for (int i = 0; i < listAdapter.getCount(); i++) { + View listItem = listAdapter.getView(i, null, listView); + listItem.measure(0, 0); + totalHeight += listItem.getMeasuredHeight(); + } + + ViewGroup.LayoutParams params = listView.getLayoutParams(); + + params.height = totalHeight + + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); + listView.setLayoutParams(params); + } + + } diff --git a/app/src/main/java/club/hifriends/activity/beans/ActivityInfo.java b/app/src/main/java/club/hifriends/activity/beans/ActivityInfo.java index ec42570..23ec16e 100644 --- a/app/src/main/java/club/hifriends/activity/beans/ActivityInfo.java +++ b/app/src/main/java/club/hifriends/activity/beans/ActivityInfo.java @@ -24,11 +24,11 @@ public ActivityInfo (JSONObject response)throws JSONException{ this.activity_id = response.getString("activity_id"); this.title = response.getString("title"); this.content = response.getString("content"); - partnerArrayList = new ArrayList<>(); + this.partnerArrayList = new ArrayList<>(); JSONArray partners = response.getJSONArray("partners"); for(int i = 0;i