创建drawable目录下的xml文件 frame_loading.xml
<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_01"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_02"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_03"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_04"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_05"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_06"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_07"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_08"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_09"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_10"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_11"
android:gravity="left" />
</item>
<item android:duration="150">
<clip
android:clipOrientation="horizontal"
android:drawable="@mipmap/loading_12"
android:gravity="left" />
</item>
</animation-list>
values目录下style文件加入
<!--进度条样式--> <style name="MyDialog" parent="AppTheme">
加入线性布局
<ProgressBar
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_100"
android:indeterminateDrawable="@drawable/frame_loading" />
线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/scro_luck_PullToRefreshScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_200"
android:scaleType="centerCrop"
android:src="@mipmap/luck_background"/>
<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
<com.gao.xuejun.makingfriends.view.ExpandGridView
android:layout_marginTop="@dimen/dp_5"
android:id="@+id/pull_refresh_expandable_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_5"
android:horizontalSpacing="@dimen/dp_2"
android:verticalSpacing="@dimen/dp_4"
android:numColumns="3"/>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
fragment中的代码逻辑
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import com.gao.xuejun.makingfriends.Base.fragment.adapter.Adapter_Luck;
import com.gao.xuejun.makingfriends.Base.fragment.entity.Luck;
import com.gao.xuejun.makingfriends.Base.fragment.entity.UserList;
import com.gao.xuejun.makingfriends.MakeFriend;
import com.gao.xuejun.makingfriends.R;
import com.gao.xuejun.makingfriends.luck.MainActivity;
import com.gao.xuejun.makingfriends.util.PreferencesUtil;
import com.gao.xuejun.makingfriends.util.ToastUtils;
import com.gao.xuejun.makingfriends.view.ExpandGridView;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class FragmentLuck extends BaseFragment {
//加载数据的list
List<UserList> userList1 = new ArrayList<UserList>();
//控件
ExpandGridView pull_refresh_grid;
//滑动控件
PullToRefreshScrollView scro_luck_PullToRefreshScrollView;
//当前页面
int currPage = 1;
Adapter_Luck adapter_luck;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_luck, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initWidget();
//加载网络的方法
getLuck();
pull_refresh_grid = (ExpandGridView) getActivity().findViewById(R.id.pull_refresh_expandable_list);
scro_luck_PullToRefreshScrollView = (PullToRefreshScrollView)getActivity().findViewById(R.id.scro_luck_PullToRefreshScrollView);
adapter_luck = new Adapter_Luck((MainActivity)getActivity());
pull_refresh_grid.setAdapter(adapter_luck);
//设置可以上拉加载下啦刷新
scro_luck_PullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
//设置监听事件
scro_luck_PullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
currPage = 1;
userList1.clear();
getLuck();
refreshView.onRefreshComplete();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
currPage ++;
getLuck();
refreshView.onRefreshComplete();
}
});
}
//获取网络数据,进度加载
public void getLuck(){
showProgressDialog(getActivity());
String ticket = PreferencesUtil.getPreferences(MakeFriend.TICKET_TOKEN,"",getActivity());
Log.d("ticket", ticket);
Ion.with(getActivity())
.load(MakeFriend.HOST_DOCTOR + "index/index.html")
.setMultipartParameter("ticket", ticket)
.setMultipartParameter("currPage",currPage+"")
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, com.google.gson.JsonObject result) {
if (e != null) {
return;
}
Log.d("result", result.toString());
closeProgressDialog();
int code = result.get("status").getAsInt();
switch (code) {
case 0:
ToastUtils.show(getActivity(), result.get("msg").getAsString());
return;
case 1:
Gson gson = new Gson();
Type type = new TypeToken<Luck>() {
}.getType();
Luck luck = gson.fromJson(result, type);
List<UserList> userList = luck.userList;
userList1.addAll(userList);
adapter_luck.addrest(userList1);
Log.d("userList1", userList1.size() + "");
return;
default:
}
}
});
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
initWidget();
}
}
/**
* 启动一个dialog
*/
public void showProgressDialog(Context context) {
dialog = new Dialog(context, R.style.MyDialog);
dialog.setContentView(R.layout.proessdialog);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
/**
* 关闭dialog
*/
public void closeProgressDialog() {
if (dialog != null) {
dialog.dismiss();
// dialog = null;
}
}
}