//主布局
<RelativeLayout 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.a1.day9_xiala.MainActivity"> <com.handmark.pulltorefresh.library.PullToRefreshScrollView android:id="@+id/pull_to_refresh_scrollview" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.example.a1.day9_xiala.MyListview android:id="@+id/lis_view" android:layout_width="match_parent" android:layout_height="wrap_content"></com.example.a1.day9_xiala.MyListview> </com.handmark.pulltorefr
//适配器
public class ListAdapter extends BaseAdapter{ private Context context; private List<NewsBean.ResultBean.DataBean>list; public ListAdapter(Context context, List<NewsBean.ResultBean.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null){ convertView=View.inflate(context,R.layout.item_layout,null); holder=new ViewHolder(); holder.textView=convertView.findViewById(R.id.text_title); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } holder.textView.setText(list.get(position).getTitle()); return convertView; } class ViewHolder{ TextView textView; } }
//Activty
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity---"; public static String one_url="http://v.juhe.cn/toutiao/index?type=&key=2f41498b35e69877fc56dc96776e5d1f"; private MyListview listView; private PullToRefreshScrollView pullToRefreshScrollView; private int page=1; private ListAdapter listAdapter; private List<NewsBean.ResultBean.DataBean> list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); listAdapter=new ListAdapter(MainActivity.this,list); listView.setAdapter(listAdapter); initDatas(); } private void initDatas() { HttpUtils httpUtils=HttpUtils.getInstance(); httpUtils.get(one_url); httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { Log.d(TAG, "成功"+json); Gson gson=new Gson(); NewsBean newsBean=gson.fromJson(json,NewsBean.class); List<NewsBean.ResultBean.DataBean> shuju_list=newsBean.getResult().getData(); if (page==1){ list.clear(); } list.addAll(shuju_list); listAdapter.notifyDataSetChanged();; pullToRefreshScrollView.onRefreshComplete(); } @Override public void getError(String error) { Log.d(TAG, "失败"+error); } }); } private void initViews() { pullToRefreshScrollView=findViewById(R.id.pull_to_refresh_scrollview); listView=findViewById(R.id.lis_view); //下拉加载 pullToRefreshScrollView.setMode(PullToRefreshScrollView.Mode.BOTH); pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { page=1; initDatas(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { page++; initDatas(); } }); } }