SwipeRefresh不显示在使用RecyclerView的片段中

时间:2017-11-18 09:07:59

标签: android android-recyclerview android-tablayout drawerlayout swiperefreshlayout

我正在尝试在片段中显示SwipeRefreshLayout。我已经构建了一个带有3个Tabs(片段)的TabLayout。在第一个中有一个RecyclerView。我已经尝试在CoreActivity中实现SwipeRefreshLayout并且它可以工作。有什么建议? TabLayour是否可以覆盖SwipeRefreshLayout?

这里是代码:

CoreActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout 
    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.latlo.firestore.CoreActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AppCompat.NoActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />



    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton5"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top|left"
        android:layout_margin="16dp"
        android:background="@color/white"
        android:backgroundTint="@color/white"
        android:clickable="true"
        android:src="@drawable/ic_add_black_24dp"
        app:layout_anchor="@+id/viewpager"
        app:layout_anchorGravity="bottom|right" />


     </android.support.design.widget.CoordinatorLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/menulaterale"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_top"
    app:menu="@menu/drawer_menu"/>


 </android.support.v4.widget.DrawerLayout>

Announces_content_fragment.xml:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="?android:attr/selectableItemBackground"
tools:context="chilor.lolmatch.AnnouncesContentFragment">

<ImageView
    android:id="@+id/img"
    android:layout_width="70dp"
    android:layout_height="70dp"
    tools:src="@drawable/diamond_v"
    android:layout_below="@+id/queue_type"
    android:layout_alignParentStart="true" />
<TextView
    android:id="@+id/queue_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    tools:text="ciao"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textSize="20dp"
    android:layout_marginStart="36dp"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/img" />

<Button
    android:id="@+id/button_reply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reply"
    android:layout_alignBottom="@+id/img"
    android:layout_alignParentEnd="true"
    style="@style/Widget.AppCompat.Button.Borderless.Colored"
    android:theme="@style/AppTheme.Button"/>

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/queue_type"
    android:layout_centerVertical="true"
    android:text="24/06/17 23:00" />

<TextView
    android:id="@+id/sender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ADC"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_alignBaseline="@+id/button_reply"
    android:layout_alignBottom="@+id/button_reply"
    android:layout_alignStart="@+id/time" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Looking For"
    android:layout_alignBottom="@+id/queue_type"
    android:layout_alignStart="@+id/button_reply" />

<TextView
    android:id="@+id/receiver"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:text="Support" />

<TextView
    android:id="@+id/summ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textStyle="bold" />




</RelativeLayout>

Recycler_view.xml:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh2"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView 
            xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/my_recycler_view"
                android:clipToPadding="false"
                android:paddingBottom="16dp"
                android:paddingTop="16dp"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
            </android.support.v7.widget.RecyclerView>

    </ScrollView>



</android.support.v4.widget.SwipeRefreshLayout>

Announces_content_fragment.activity:

public class AnnouncesContentFragment extends Fragment implements 
SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView.Adapter adapter;
    private RecyclerView recyclerView;
    private List<Messages> currentItems = new ArrayList<>();
    private String summoner;
    private String rank;
    private FirebaseAuth au = FirebaseAuth.getInstance();
    private FirebaseFirestore d;
    private SwipeRefreshLayout swipeRefreshLayout;
    //public static CurrentProfile current;






    @SuppressLint("ResourceAsColor")
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup 
container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(
            R.layout.recycler_view, container, false);
    recyclerView = (RecyclerView)  
    v.findViewById(R.id.my_recycler_view);
    FirebaseFirestore d = FirebaseFirestore.getInstance();


    swipeRefreshLayout = (SwipeRefreshLayout) 
    v.findViewById(R.id.swiperefresh2);
    swipeRefreshLayout.setProgressViewOffset(true, 1500, 1800);



    swipeRefreshLayout.setColorSchemeColors
    (android.R.color.holo_blue_light,
            android.R.color.holo_green_light,
            android.R.color.holo_red_light,
            android.R.color.holo_orange_light);
    swipeRefreshLayout.setOnRefreshListener(this);
    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(true);
        }
    });
    try {
        setFirebaseValueListener();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(true);
    LinearLayoutManager mm = new LinearLayoutManager(getContext(), 
    LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(mm);
    return recyclerView;
}


public void setFirebaseValueListener() throws ExecutionException, 
InterruptedException, JSONException {
    final FirebaseFirestore d = FirebaseFirestore.getInstance();
    NetworkController nc = new NetworkController();
    nc.getMessages(getContext(), new ServerCallback() {
        @Override
        public void onSucces(JSONObject result) {
            try {
                JSONArray array = result.getJSONArray("data");
                for(int i =0;i<array.length();i++){
                    JSONObject obj = array.getJSONObject(i);
                    Messages mes = new 
                    Gson().fromJson(obj.toString(),Messages.class);
                    currentItems.add(mes);
                }
                summoner = "AFKMan";
                adapter = new 
                RecyclerViewAdapter(currentItems,summoner);
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
public void onRefresh() {
    try {
        setFirebaseValueListener();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

使用 RecyclerView ,您不必担心滚动视图。如果您的列表超出布局限制,Recyclerview会自动放置 Scrollview

尝试删除 Scrollview 这可能会有所帮助。

而且看起来您还没有包含 SwipeRefreshLayout 的任何布局父级。包括 LinearLayout SwipeRefreshLayout 的任何其他布局父。

修改(解决您的问题)

替换

return recyclerView;

通过

return v;