如何在片段中添加滚动 [英] How to add scroll in Fragment

查看:86
本文介绍了如何在片段中添加滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在列表片段中实现了Twitter SDK.像推特一样,它具有无限滚动.但是,当我尝试将此片段添加到scrollview layout时,它会禁用twitter fragmentscrollview.

I have implemented Twitter SDK in a List Fragment. It has unlimited scroll just like twitter. But when i try to add this Fragment in a scrollview layout it disables scrollview of twitter 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片段的滚动消失了.我尝试了各种方法,但未能实现目标.

Now i want to add this Fragment in another activity. MainActivity has already a scroll view. when i add this fragment there, the scroll of twitter fragment is gone. I tried various ways, but failed to achieve the target.

我还需要在小片段中进行相同的无限制滚动.

I need the same unlimited scroll in the small fragment as well.

这是我的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>

推荐答案

您可以在fragment_layout.xml中添加ScrollView来包装整个布局( ScrollView 通常有一个孩子,这种情况下fragment_layout.xml中定义的整个布局). View然后通常由布局填充器

you can add a ScrollView in the fragment_layout.xml that wraps the entire layout (a ScrollView normally has one child, in this case the entire layout defined in fragment_layout.xml). The View is then generated normally by the layout inflater

fragment.java:

View view = inflater.inflate(R.layout.fragment_layout, container, false);
return view;

在以下带有 ScrollView fragment_layout.xml示例中,

ScrollView 包装了一个包含两个TextViews的ConstraintLayout

in the following example of a fragment_layout.xml with a ScrollView the ScrollView wraps a ConstraintLayout that contains two 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的方法)中创建 ScrollView 的实例

when using this method the LayoutInflater does all the work, one does not have to create an instance of ScrollView in fragment.java (xml based method)

这篇关于如何在片段中添加滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆