上拉刷新

//主布局

<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();
             }
         });
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值