关于pulltorefresh框架使用 下拉刷新、 上拉加载

为什么朕赶脚好乱

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.List;

使用范例

页面


public class MoreHotSingerFragment extends BaseFragment {
    private HotSingerAdapter hotSingerAdapter;
    private PullToRefreshGridView grapeGridView;
    private List<AllSingerBean.ArtistBean> artistBeen;
    private GridView mGridView;
    private int a;
    private int b;

## 绑定布局 ##
    @Override
    protected int setLayout() {
        return R.layout.activity_singer_more;
    }

    @Override
    protected void initView() {
        // 获取控件
        grapeGridView = findViewById(R.id.ggv_singer_hot);
        // 初始化adapter
        hotSingerAdapter = new HotSingerAdapter(getContext());
        // gridView设置adapter
        grapeGridView.setAdapter(hotSingerAdapter);
        mGridView = grapeGridView.getRefreshableView();
        bundle = getArguments();
        a = bundle.getInt(KeyValues.MORE_HOT_SINGER_A); 
        // 加载数量
        b = bundle.getInt(KeyValues.MORE_HOT_SINGER_B);
         // 初始加载位置

// 设置加载刷新模式
grapeGridView.setMode(PullToRefreshBase.Mode.BOTH);

       // 设置刷新监听
        grapeGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {

            // 下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                int urlA = bundle.getInt(KeyValues.MORE_HOT_SINGER_A);
                int urlB = bundle.getInt(KeyValues.MORE_HOT_SINGER_B);
               // 网络数据请求(封装类)再次重新请求一次网络数据
                mNetTool.getNetData(URLVlaues.HOT_SINGER_A + urlA + URLVlaues.HOT_SINGER_BODAY + urlB + URLVlaues.HOT_SINGER_B, AllSingerBean.class, new NetTool.NetListener<AllSingerBean>() {
                // 当数据请求成功时
                    @Override
                    public void onSuccess(AllSingerBean allSingerBean) {

                        artistBeen = allSingerBean.getArtist();

                        hotSingerAdapter.setArtistBeen(artistBeen);
                       // 刷新完成 , 页面回弹
                        grapeGridView.onRefreshComplete();
                    }
                    @Override
                    public void onError(String errorMsg) {
                      Log.d("Mainactivity","网络请求失败");
                    }
                });
            }

                // 上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
            //讲初始加载位置增加 加载数据个数
                b += a;
                // 通过新改变的url 再次请求数据
                mNetTool.getNetData(URLVlaues.HOT_SINGER_A + a + URLVlaues.HOT_SINGER_BODAY + b + URLVlaues.HOT_SINGER_B, AllSingerBean.class, new NetTool.NetListener<AllSingerBean>() {
                // 当数据请求成功时
                    @Override
                    public void onSuccess(AllSingerBean allSingerBean) {


                        List<AllSingerBean.ArtistBean> list = allSingerBean.getArtist();
                        // 讲请求的数据添加到原有集合
                        artistBeen.addAll(list);

                   //将adapter中的数据集合 添加新数据                        hotSingerAdapter.addArtistBeen(list);
                   // 数据请求成功 回弹
                        grapeGridView.onRefreshComplete();
                    }

                    @Override
                    public void onError(String errorMsg) {
  Log.d("Mainactivity","网络请求失败");
                    }
                });

            }
        });


    }

    @Override
    protected void initData() {
        bundle = getArguments();
        int urlA = bundle.getInt(KeyValues.MORE_HOT_SINGER_A);
        int urlB = bundle.getInt(KeyValues.MORE_HOT_SINGER_B);

        mNetTool.getNetData(URLVlaues.HOT_SINGER_A + urlA + URLVlaues.HOT_SINGER_BODAY + urlB + URLVlaues.HOT_SINGER_B, AllSingerBean.class, new NetTool.NetListener<AllSingerBean>() {

            @Override
            public void onSuccess(AllSingerBean allSingerBean) {
                artistBeen = allSingerBean.getArtist();
                hotSingerAdapter.setArtistBeen(artistBeen);
            }

            @Override
            public void onError(String errorMsg) {

            }

        });

        // 点击事件
        grapeGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DetailFragment detailFragment = new DetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString(KeyValues.RANK_TYPE, artistBeen.get(position).getTing_uid());

                bundle.putString(KeyValues.RANK_ARTIST_ID, artistBeen.get(position).getArtist_id());

                bundle.putString(KeyValues.RANK_NAME, artistBeen.get(position).getName());
                bundle.putString("songs_total", artistBeen.get(position).getSongs_total());
                bundle.putString("albums_total", artistBeen.get(position).getAlbums_total());
                bundle.putString(KeyValues.RANK_PIC, artistBeen.get(position).getAvatar_big());
                bundle.putInt(KeyValues.MY_TYPE_ID, 1);

                detailFragment.setArguments(bundle);
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.updateView(detailFragment);
            }
        });

    }
}

Adapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import vampire.com.baidumusic.R;
import vampire.com.baidumusic.fragment.allsinger.AllSingerBean;
import vampire.com.baidumusic.tools.nettools.NetTool;

/**
 * Created BaiDuMusic by *Vampire* on 16/8/18.
 */
public class HotSingerAdapter extends BaseAdapter{
    private static final String TAG = "Vampire_HotSingerAdapter";
    private Context context;
    private List<AllSingerBean.ArtistBean> artistBeen;

    public void setArtistBeen(List<AllSingerBean.ArtistBean> artistBeen) {
        this.artistBeen = artistBeen;
        notifyDataSetChanged();
    }
    public void addArtistBeen(List<AllSingerBean.ArtistBean> artistBeenNew) {
        this.artistBeen.addAll(artistBeenNew);
        notifyDataSetChanged();
    }


    public HotSingerAdapter(Context context) {
        this.context = context;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return artistBeen!=null?artistBeen.size():0;
    }

    @Override
    public Object getItem(int position) {
        return artistBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        MyViewHolder myViewHolder;
        if (convertView == null){
            convertView =inflater.inflate(R.layout.item_ggv_hot_singer,null);
            myViewHolder =new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        AllSingerBean.ArtistBean artistBean =artistBeen.get(position);
        NetTool netTool =new NetTool();
        netTool.getImage(artistBean.getAvatar_big(),myViewHolder.pic);
        myViewHolder.name.setText(artistBean.getName());

        return convertView;
    }

    private class MyViewHolder {
        private ImageView pic;
        private TextView name;
        public MyViewHolder(View view) {
            pic= (ImageView) view.findViewById(R.id.iv_item_hot_singer_pic);
            name = (TextView) view.findViewById(R.id.tv_item_hot_singer_name);

        }
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:background="@mipmap/bac_six"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#7e16a4f8"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/back"
            android:layout_marginLeft="5dp"
            android:id="@+id/iv_go_back"
            android:layout_gravity="center_vertical"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="全部歌手"
            android:textSize="22sp"
            android:textColor="#d6d6d6"
            android:gravity="center_vertical"/>
    </LinearLayout>

    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:id="@+id/ggv_singer_hot">
    </com.handmark.pulltorefresh.library.PullToRefreshGridView>

</LinearLayout>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值