代码之家  ›  专栏  ›  技术社区  ›  Lorenzo Lattari

SwipeRefresh未在带有RecyclerView的片段中显示

  •  0
  • Lorenzo Lattari  · 技术社区  · 8 年前

    我试图在片段中显示SwipeRefreshLayout。我已经构建了一个包含3个选项卡(片段)的TabLayout。在第一个视图中,有一个RecyclerView。我曾尝试在CoreActivity中实现SwipeRefreshLayout,它很管用。有什么建议吗?是否可能覆盖SwipeRefreshLayout?

    代码如下:

    核心活动。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>
    

    宣布_content_片段。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\u视图。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>
    

    宣布_content_片段。活动:

    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 回复  |  直到 8 年前
        1
  •  1
  •   Mo Faizan Shaikh    8 年前

    具有 回收视图 ,您不需要担心scrollview。Recyclerview自动放置 滚动视图

    尝试删除 滚动视图 这可能会有所帮助。

    而且,看起来您还没有包括任何布局父对象 SwipeRefreshLayout . 包括 线性布局 或任何其他布局父对象 SwipeRefreshLayout .

    编辑 (解决您的问题)

    代替

    return recyclerView;
    

    通过

    return v;    
    
    推荐文章