【问题标题】:Android scrolling in card viewAndroid 在卡片视图中滚动
【发布时间】:2016-12-22 08:11:11
【问题描述】:

我的应用中有这个 XML 布局,其中包含 ListViewCardView,但 CardView 内容有时隐藏在屏幕下方,我想将滚动应用到整个布局。

<?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>

出了什么问题?当屏幕尺寸较小时,半布局隐藏在屏幕下方。我只想对此应用滚动行为。我会非常感谢你。

【问题讨论】:

  • 将布局放在滚动视图中,使用 recyclerview 而不是列表视图可能会解决您的问题。如果您使用的是列表视图本身,那么您必须分别处理滚动视图的滚动和滑动
  • 我必须使用listview本身,所以建议相应
  • 当我将滚动视图放在线性布局上方时,它只会滚动列表视图,而卡片视图内容仍然隐藏在屏幕下方
  • 你必须为它使用页脚视图,将cardview xml内容放在页脚视图中,然后将其添加到列表视图中。 @neha
  • 但是当我使用页脚查看时,卡片视图的内容会变成黑色背景

标签: android listview android-scrollview


【解决方案1】:

由于您使用的是列表视图,因此您可以添加页脚。像这样

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>

Activity 类将是

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。

【讨论】:

  • 你的意思是我必须为listview制作两个不同的xml,另一个是包含cardview的页脚布局,???/然后在主活动文件中设置它
  • 确实如此。您将有一个单独的 xml 文件用于页脚设计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多