2014-03-05 136 views
0

我已經研究了很多關於此主題的內容,並且我終於發佈了此問題,因爲我沒有找到我搜索的內容。Android中的滾動視圖中的ListView

我在ScrollView中有一個Listview的佈局。

Listview用於顯示來自不同用戶的評論。 佈局如下:

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> 

而且佈局的樣子:

所以佈局由三個部分組成:(粗灰線作爲分頻器)

第一部分:

Screen 1

包括Web服務調用的形象和高性價比,產品名稱和用戶等

第二部分:

Screen 2

有3個標籤,所有的標籤內容將被檢索單一的網絡服務。

第三部分:

Screen 3

這就是實際的ListView存在但它無法顯示。 編輯文本底部實際上是低於列表視圖,它無法顯示。這還包括一個Web服務調用。

我已經添加了一個標題到列表視圖。

的解決方案,我到目前爲止已經試過:

我使用了一個輔助類動態設置列表視圖的高度(ATLEAST這就是我想它):

類是如如下:

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; 
      } 
     }); 

從我讀,這根據的ListView同步滾動視圖。

Web Service調用:

我只要上述佈局發佈會呼籲徵求意見的web服務。我可以看到Web服務被調用,因爲我可以看到在logcat中打印的響應。但它不設置列表視圖。

注:

我已分別實現ListView控件的Web服務調用和它的作品,並將意見列表視圖。所以,真正存在的問題是我在列表視圖中使用了scrollview。

任何幫助將不勝感激。提前致謝。

+0

我有這個相同的問題,我遵循:http://www.androidhub4you.com/2014/03/android-listview-into-scrollview-issue.html,並能夠實現我所需要的(這似乎是什麼你也需要)。 –

回答

1

ScrollView中的ListView不是一種好的做法,因爲如果ListView不適合放在屏幕上,它有自己的滾動。

+0

我知道這是一個不好的做法,但我需要這個。我將如何實現它?有任何想法嗎 ? – San

+0

你可以給你的滾動視圖內的列表視圖和editext賦予權重,然後你可以在列表視圖中禁用觸摸滾動視圖。 – srbyk1990