2016-12-22 102 views
0

我的應用程序中有這個XML佈局,其中包含ListViewCardView,但CardView內容有時隱藏在屏幕下方,我想將滾動應用於整個佈局。Android在卡片視圖中滾動

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ffffff" 
      android:orientation="vertical" 
      android:weightSum="2"> 

      <include 
       android:id="@id/tool" 
       layout="@layout/toolbar" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

      <ListView 
       android:id="@+id/lvcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:layout_below="@+id/toolbar" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="50dp" /> 

      <android.support.v7.widget.CardView 
       android:id="@+id/cardView3" 
       android:layout_width="match_parent" 
       android:layout_height="160dp" 
       android:layout_below="@+id/lvcontent" 
       android:layout_margin="16dp" 
       android:layout_weight="0.2" 
       app:layout_anchorGravity="bottom" 
       card_view:cardCornerRadius="4dp" 
       card_view:contentPadding="8dp"> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:id="@+id/tvcusttest" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:text="What Customers are Saying" 
         android:textColor="#000" 
         android:textSize="16dp" /> 

        <View 
         android:layout_width="match_parent" 
         android:layout_height="1dp" 
         android:layout_below="@+id/tvcusttest" 
         android:background="@android:color/darker_gray" 
         android:paddingLeft="3dp" 
         android:paddingRight="3dp" /> 
       </RelativeLayout> 

       <com.viewpagerindicator.CirclePageIndicator 
        android:id="@+id/cpiBanner" 
        android:layout_width="fill_parent" 
        android:layout_height="15dp" 
        android:layout_gravity="bottom|center_horizontal" 
        android:layout_marginBottom="7dp" 
        android:layout_marginTop="15dp" 
        android:padding="1dp" 
        app:fillColor="#ffffff" 
        app:pageColor="#80cccccc" /> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/vpCustomerTestimonials" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

      </android.support.v7.widget.CardView> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_marginTop="30dp" 
       android:layout_weight="1"></View> 

      <LinearLayout 
       android:id="@+id/content" 
       android:layout_width="fill_parent" 
       android:layout_height="61dp" 
       android:layout_alignParentBottom="true" 
       android:layout_below="@+id/cardView3" 
       android:gravity="center_vertical" 
       android:orientation="horizontal" 
       android:weightSum="1"> 

       <Button 
        android:id="@+id/btnviewcart" 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent" 
        android:layout_alignParentBottom="true" 
        android:layout_weight="0.5" 
        android:background="@color/selecteditem" 
        android:text="View Cart" 
        android:textAlignment="center" 
        android:textAllCaps="true" 
        android:textColor="#ffffff" 
        android:textSize="23sp" 
        android:textStyle="bold" /> 
      </LinearLayout> 
     </LinearLayout> 
</RelativeLayout> 

怎麼回事?當屏幕尺寸較小時,半屏幕隱藏在屏幕下方。我只想要將滾動行爲應用於此。我會非常感謝你。

+0

將佈局放置在滾動視圖中,並且使用recyclerview而不是列表視圖可能會解決您的問題。如果你使用的是listview本身,你必須要處理滾動和滾動視圖分別滑動 – Nivedh

+0

我必須使用listview本身,,所以建議相應 – neha

+0

當我把scrollview上面那個線性佈局然後它只滾動列表視圖並且卡片視圖仍然隱藏在屏幕下方 – neha

回答

0

由於您使用的是一個列表視圖,您可以添加一個頁腳。像這樣

View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false); 
ListView.addFooterView(footerView); 

然後頁腳佈局

 <?xml version="1.0" encoding="utf-8"?> 
     <android.support.v7.widget.CardView 
      android:id="@+id/cardView3" 
      android:layout_width="match_parent" 
      android:layout_height="160dp" 
      android:layout_below="@+id/lvcontent" 
      android:layout_margin="16dp" 
      android:layout_weight="0.2" 
      app:layout_anchorGravity="bottom" 
      card_view:cardCornerRadius="4dp" 
      card_view:contentPadding="8dp"> 


      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/tvcusttest" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="What Customers are Saying" 
        android:textColor="#000" 
        android:textSize="16dp" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:layout_below="@+id/tvcusttest" 
        android:background="@android:color/darker_gray" 
        android:paddingLeft="3dp" 
        android:paddingRight="3dp" /> 
      </RelativeLayout> 

      <com.viewpagerindicator.CirclePageIndicator 
       android:id="@+id/cpiBanner" 
       android:layout_width="fill_parent" 
       android:layout_height="15dp" 
       android:layout_gravity="bottom|center_horizontal" 
       android:layout_marginBottom="7dp" 
       android:layout_marginTop="15dp" 
       android:padding="1dp" 
       app:fillColor="#ffffff" 
       app:pageColor="#80cccccc" /> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/vpCustomerTestimonials" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

     </android.support.v7.widget.CardView> 

以及活動類將

public class MyListActivty extends ListActivity { 
    private Context context = null; 
    private ListView list = null; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     list = (ListView)findViewById(android.R.id.list); 

     //code to set adapter to populate list 
     View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false); 
     list.addFooterView(footerView); 
    } 
} 

即使如此,仍然可以考慮使用recyclerview。

+0

你的意思是我必須爲listview製作兩個不同的xmls,另一個是包含cardview的頁腳佈局,然後在main活動文件 – neha

+0

這是真的。你將有一個單獨的XML文件的頁腳設計。 – gilokimu