2016-11-12 97 views
1

我在List Fragment中實現了Twitter SDK。它像Twitter一樣擁有無限的滾動條。但是,當我嘗試在scrollview layout中添加此片段時,它將禁用scrollviewtwitter fragment如何在片段中添加滾動

public class TwitterFragment extends ListFragment { 
    public static TwitterFragment newInstance() { 
     return new TwitterFragment(); 
    } 

佈局

<TextView 
     android:id="@id/android:empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal|center_vertical" 
     android:text="No Tweets" /> 

    <ListView 
     android:id="@id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="50dp" 
     android:layout_weight="1" 
     android:divider="#e1e8ed" 
     android:dividerHeight="1dp" 
     android:drawSelectorOnTop="false" /> 
</LinearLayout> 

現在我想在另一個活動添加此片段。 MainActivity已經有一個滾動視圖。當我在那裏添加這個片段時,twitter片段的滾動消失了。我嘗試了各種方法,但未能實現目標。

我需要在小片段中同樣無限滾動。

這是我的MainActivity代碼。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_users_feedback" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.myapp.app.usersreviews.reviews" 
    android:background="@drawable/users_bg" 
    tools:showIn="@layout/activity_user_reviews"> 



    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:isScrollContainer="false" 
     android:id="@+id/scroll_view" 
     android:layout_below="@+id/sv_users" 
     > 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      > 


      <!-- USER DATA--> 
      <include layout="@layout/users_data_list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/layout_data" 

       /> 



      <!-- TWITTER--> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="match_parent" 
       android:layout_height="33.62dp" 
       android:background="#054F92" 
       android:gravity="center_vertical" 
       android:text="\@tweets" 
       android:textColor="@android:color/white" 
       android:textSize="16sp" 
       android:paddingLeft="15dp" 
       android:textStyle="normal|bold" /> 


       <fragment 
        android:id="@+id/cart_twitter_fragment" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginTop="-50dp" 
        android:nestedScrollingEnabled="true" 
        class="com.myapp.app.TwitterFragment" 
        tools:layout="@layout/fragment_twitter" /> 


      <!-- TWITTER --> 


     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 
+0

只是爲了確認創建的滾動型一個實例,做用戶數據列表包含它需要滾動的數據太多了? – clownba0t

+0

@ clownba0t,是的,它有很多數據,我沒有在問題中添加所有視圖 – Kirmani88

+0

我的理解是,通常不建議嵌套'ScrollView',至少部分是因爲可能難以確定哪個他們應該處理任何給定的輕掃手勢。看來你遇到了這個問題。我建議改變數據的呈現方式。考慮在活動佈局中移動「ScrollView」,以便它僅包裝用戶數據列表。這樣你有兩個滾動佈局,但它們沒有嵌套。然後,您可以設置片段的寬度,高度,重量等,並通過滾動視圖來實現所需的UI,同時仍然允許滾動。 – clownba0t

回答

2

您可以添加在了覆蓋整個佈局fragment_layout.xml一個ScrollView(一滾動型通常有一個孩子,在這種情況下fragment_layout.xml定義的整個佈局)。所述View然後由佈局充氣正常生成

fragment.java:

View view = inflater.inflate(R.layout.fragment_layout, container, false); 
return view; 
fragment_layout.xml的與滾動型 滾動型 以下示例

包裝一個ConstraintLayout包含兩個TextViews

fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.ralf.gpstelephony.TelephonyManagerFragment"> 


    <TextView 
     android:id="@+id/TextView_1" 
     android:layout_width="match_parent" 
     android:layout_height="600dp" 
     android:text="@string/string1" 
     app:layout_constraintTop_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="10dp" /> 

<TextView 
     android:id="@+id/TextView_2" 
     android:layout_width="match_parent" 
     android:layout_height="600dp" 
     android:text="@string/string2" 
     app:layout_constraintTop_toBottomOf="@id/TextView1" 
     app:layout_constraintLeft_toLeftOf="@id/TextView1" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="0dp" /> 

</android.support.constraint.ConstraintLayout> 

</ScrollView> 

使用這種方法時,LayoutInflater完成所有的工作,一個不具有fragment.java(基於XML的方法)