java - 如果仅在顶部,如何限制此滑动刷新?

标签 java android android-fragments

这是我的代码,我用它来以列表的形式从我的数据库中获取数据。但是在向上滚动片刻后向下滚动时它总是刷新。我需要让刷新仅在顶部时有效。

 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.widget.SwipeRefreshLayout;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.ProgressBar;
 import android.widget.TextView;

 import com.shuan.Project.R;
 import com.shuan.Project.Utils.Common;
 import com.shuan.Project.adapter.ConnectAdapter;
 import com.shuan.Project.asyncTasks.GetHome;
 import com.shuan.Project.asyncTasks.GetPost;
 import com.shuan.Project.employer.PostViewActivity;
 import com.shuan.Project.list.Sample;

 import java.util.ArrayList;
 import java.util.HashMap;

public class OffersFragment extends Fragment {


   private ArrayList<Sample> list;
   private ConnectAdapter adapter;
private ListView listView;
private HashMap<String, String> cData;
private Common mApp;
private Context mContext;
private ProgressBar progressBar;
private SwipeRefreshLayout swipe;


public OffersFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    mContext=getActivity();
    mApp= (Common) mContext.getApplicationContext();
    View view = inflater.inflate(R.layout.fragment_employer_home, container, false);

    swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
    listView = (ListView) view.findViewById(R.id.post);
    progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);

    list = new ArrayList<Sample>();

    new GetPost(getActivity(), listView, progressBar, mApp.getPreference().getString(Common.u_id,""),"all", swipe).execute();


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView txt = (TextView) view.findViewById(R.id.jId);
            TextView txt1= (TextView) view.findViewById(R.id.frm_id);
            Intent in=new Intent(getActivity(),PostViewActivity.class);
            in.putExtra("jId",txt.getText().toString());
            in.putExtra("frmId",txt1.getText().toString());
            in.putExtra("apply","no");
            startActivity(in);
        }
    });
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new GetHome(getActivity(), listView, progressBar, mApp.getPreference().getString(Common.u_id, ""), "all",swipe).execute();
        }
    });

    return view;
   }

 }

我的xml文件

   <android.support.v4.widget.SwipeRefreshLayout 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/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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="match_parent"
    tools:context="com.shuan.Project.fragment.ConnectionFragment">


    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <ListView
        android:id="@+id/post"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:visibility="gone" />
</RelativeLayout>

最佳答案

更改您的 xml 布局,使 SwipeRefreshLayout 仅包装一个可 ScrollView (您的情况是 ListView)。

<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="match_parent"
    tools:context="com.shuan.Project.fragment.ConnectionFragment">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/post"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null"
            android:visibility="gone" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

关于java - 如果仅在顶部,如何限制此滑动刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41906805/

相关文章:

java - 当 executorservice 关闭时运行/阻塞的可运行对象会发生什么()

java - 数字/货币格式

java - 类(class)成员是什么意思? In-keepclassmembers Proguard

android - 应用程序崩溃 "back"映射 fragment

java - 使用 FragmentManager 始终返回同一个 fragment

java - 用于将命令行 Java 应用程序执行与 args 匹配的正则表达式

java - 如何让我的代码找出在合理的时间内按下了哪个按钮?

android - Github 中的 Google Maps Api key 代码

java - ListPreference 和翻译

android - 从另一个 Activity 返回到 Activity-A 的特定 fragment