向下滚动从所有位置刷新我的列表

时间:2017-01-24 07:16:11

标签: listview android-studio android-fragments swiperefreshlayout

我在主页片段中向上滚动后无法向下滚动。列表视图已设置向下滑动刷新。我需要向下滑动并在我的应用程序中向下滚动但刷新操作应该只在列表位于顶部。

我的家庭片段活动

     public class EmployerHome extends Fragment implements AbsListView.OnScrollListener {


private ArrayList<Sample> list;
private ConnectAdapter adapter;
private ListView listView;
private HashMap<String, String> cData;
private CardView cv;
private Common mApp;
private Context mContext;
private RelativeLayout udyowel;
private ProgressBar progressBar;

private int preLast;
private SwipeRefreshLayout swipe;

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


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

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

    udyowel = (RelativeLayout) view.findViewById(R.id.udyopro_post);
    listView = (ListView) view.findViewById(R.id.post);
    progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
    swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
    cv = (CardView) view.findViewById(R.id.udp);
    list = new ArrayList<Sample>();

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


   /* if (listView.getCount()==0){
        udyowel.setVisibility(View.VISIBLE);
    }else {*/
        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);
                Intent in = new Intent(getActivity(), PostViewActivity.class);
                in.putExtra("jId", txt.getText().toString());
                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) {
        int lastItem = firstVisibleItem + visibleItemCount;
        if (lastItem == totalItemCount) {
            if (preLast != lastItem) {
                preLast = lastItem;
                //Toast.makeText(getActivity(), "In Last", Toast.LENGTH_SHORT).show();
            }

        } else {
           // udyowel.setVisibility(View.VISIBLE);
        }
    }
 }
}

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

1 个答案:

答案 0 :(得分:1)

在您的onScroll方法中,如果firstVisibleItem == 0设置了滑动启用,否则它应该保持禁用状态,因为如果您位于列表顶部,则只应滑动以刷新。