繁体   English   中英

如果仅在顶部,如何限制此刷卡刷新?

[英]How to limit this swipe refresh if its only at top?

这是我用来使我的数据以列表的形式从数据库中提取的代码。但是在瞬间向上滚动后向下滚动时它总是在刷新。 我仅在顶部时才需要进行刷新。

 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仅包装可滚动视图(您的情况为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>
    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.enhancedListView.EnhancedListView
        android:id="@+id/listaNoticias"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="5dp" >
    </com.enhancedListView.EnhancedListView>

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.principal, container, false);

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeColors(android.R.color.holo_green_dark, 
            android.R.color.holo_red_dark, 
            android.R.color.holo_blue_dark, 
            android.R.color.holo_orange_dark); (...)

@Override
public void onRefresh() {
    new myTask().execute();     
}

我为上述问题找到了自己的解决方案。 我们可以通过更改以下代码来解决此问题。

 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.AbsListView;
 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 implements AbsListView.OnScrollListener {


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;
private int preLast;


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);
        }
    });
    listView.setOnScrollListener(this);
    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;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    if (view.getId() == R.id.post) {
        if (firstVisibleItem == 0) {
            swipe.setEnabled(true);
            int lastItem = firstVisibleItem + visibleItemCount;
            if (lastItem == totalItemCount) {
                if (preLast != lastItem) {
                    preLast = lastItem;
                    //Toast.makeText(getActivity(), "In Last", Toast.LENGTH_SHORT).show();
                }

            }else{

            }
        } else {
            swipe.setEnabled(false);
        }
    }
  }
}

添加以下代码行:线性布局管理器应单独定义

mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(getActivity());    
mRecyclerView.setLayoutManager(mLayoutManager);       
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);


      mSwipeRefreshLayout.setEnabled
(mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);
}
});

findFirstCompletelyVisibleItemPosition()== 0表示在recyclerview的顶部

这有点旧,但是在2019年这是正确的答案:您必须在NestedScrollView内包含RecyclerView才能使SwipeRefreshLayout正常运行,如下所示:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM