【发布时间】:2014-03-06 00:51:15
【问题描述】:
我对此主题进行了很多研究,我终于发布了这个问题,因为我没有找到我搜索的内容。
我在 ScrollView 中有一个带有 Listview 的布局。
Listview 用于显示来自不同用户的 cmets。 布局如下:
Layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/panel_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="@drawable/image" />
<View
android:id="@+id/image_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/image"
android:background="#c2c2c2" />
<TextView
android:id="@+id/prod_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image_divider"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="SUV car by Nissaan"
android:textAlignment="gravity"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="@+id/productCost_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/prod_name_text"
android:layout_below="@+id/prod_name_text"
android:text="$150"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="@+id/likes_details_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/prod_name_text"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/productCost_text"
android:text="rob + 1,124"
android:textColor="#000000" />
<ImageButton
android:id="@+id/dreamitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/likes_details_text"
android:layout_margin="10dp"
android:background="@null"
android:src="@drawable/dreamplus" />
<ImageButton
android:id="@+id/addtoListBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/likes_details_text"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/dreamitBtn"
android:background="@null"
android:src="@drawable/add_to_list_bg" />
<View
android:id="@+id/_first_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_below="@+id/addtoListBtn"
android:background="#838B8B" />
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/_first_divider"
android:layout_marginTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<Button
android:id="@+id/cost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignRight="@+id/image"
android:layout_alignTop="@+id/addtoListBtn"
android:layout_marginLeft="120dp"
android:layout_toRightOf="@+id/addtoListBtn"
android:background="@drawable/buy_button"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="60$"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/second_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#838B8B" />
<RelativeLayout
android:id="@+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<TextView
android:id="@+id/comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/upper_divider"
android:layout_alignParentLeft="true"
android:text="1"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/comments_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/comments_count"
android:text="Comment(s)"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:id="@+id/upper_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="@id/comments_text"
android:background="#000000" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/upper_divider"
>
</ListView>
<View
android:id="@+id/lower_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="@id/list"
android:background="#000000" />
<EditText
android:id="@+id/comments_edit"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_below="@+id/lower_divider"
android:background="#FFFFFF"
android:hint="Enter your comment"
android:paddingLeft="8dp"
android:paddingTop="10dp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:background="#000000" />
<Button
android:id="@+id/post_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/lower_divider"
android:layout_toRightOf="@+id/comments_edit"
android:background="#FFFFFF"
android:text="Post" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
布局看起来像:
所以布局由三部分组成:(粗灰线作为分隔线)
第一部分:
包括对图像和成本、产品名称和用户等的 Web 服务调用。
第二部分:
有 3 个选项卡,所有选项卡的内容都将从单个 Web 服务中检索。
第三部分:
这是实际列表视图存在的地方,但它无法显示。底部的编辑文本实际上是在列表视图下方,它没有显示出来。这还包括 Web 服务调用。
我在列表视图中添加了一个标题。
到目前为止我尝试过的解决方案:
我使用了一个辅助类来动态设置 Listview 的高度(至少我认为是这样):
类如下:
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = myListView.getPaddingBottom() + myListView.getPaddingTop();
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
System.out.println("List view id: "+myListView.getId());
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter
.getCount() - 1));
System.out.println("The params height is " + params.height);
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
我称之为
Helper.getListViewSize(MainActivity.listView);
将适配器设置为列表后。
第二种解决方案:
listView.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
根据我的阅读,这会根据列表视图同步滚动视图。
网络服务调用:
只要上述布局启动,我就会调用 cmets 的 web 服务。我可以看到正在调用 Web 服务,因为我可以看到打印在 logcat 中的响应。但它没有设置列表视图。
注意:
我已经单独实现了listview的webservice调用,它可以工作并将cmets设置为listview。所以,真正的问题在于我在列表视图中使用了滚动视图。
任何帮助将不胜感激。提前致谢。
【问题讨论】:
-
我遇到了同样的问题,我关注了:androidhub4you.com/2014/03/… 并且能够实现我所需要的(这似乎也是您所需要的)。
标签: android web-services android-layout android-listview