PullToRefresh下拉和上拉简单实现

 一  导入架包,添加依赖.

1,布局文件(activity_user_defined_list__pulltore_fresh.xml)

<com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrAnimationStyle="rotate"
        ptr:ptrHeaderTextColor="#ffffff"
        ptr:ptrHeaderSubTextColor="#00ffff"
        ptr:ptrHeaderBackground="@null"
       />
2程序(userDefinedList_PulltoreFresh.java)
public class MainActivity extends AppCompatActivity {

    private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler"};
    private LinkedList<String> mListItems;
    private ArrayAdapter<String> mAdapter;
    private PullToRefreshListView pu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pu = findViewById(R.id.pull_refresh_list);
        //设置可以上拉加载更多;
        pu.setMode(PullToRefreshListView.Mode.BOTH);

        ListView refreshableView = pu.getRefreshableView();

        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

        refreshableView.setAdapter(mAdapter);

        pu.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                new GetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                new GetDataTask2().execute();
            }
        });
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("Added after refresh...");
            mAdapter.notifyDataSetChanged();

            // Call onRefreshComplete when the list has been refreshed.
            pu.onRefreshComplete();

            super.onPostExecute(result);
        }
    }

    private class GetDataTask2 extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addLast("Added after loadmore1...");
            mListItems.addLast("Added after loadmore2...");
            mListItems.addLast("Added after loadmore3...");
            mAdapter.notifyDataSetChanged();

            // Call onRefreshComplete when the list has been refreshed.
            pu.onRefreshComplete();

            super.onPostExecute(result);
        }
    }

}

方法2

public class Childf1 extends Basefragment {

    private PullToRefreshListView pull;
    String url = "https://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603";
    List<com.bawei.day15_conghe2.Bean.bean.ResultBean.RowsBean> list=new ArrayList<>();
    private ImageLoader instance;
    private Myadaper m;
    List<String> img=new ArrayList<>();
    private Banner ban;

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        //设置可以上拉加载更多;
        pull.setMode(PullToRefreshListView.Mode.BOTH);
        //获取listview
        ListView listView = pull.getRefreshableView();
        m = new Myadaper();
        listView.setAdapter(m);
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                // 清空集合,
                list.clear();
                //重新请求数据;更新适配器
                getNetData();
            }
            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                getNetData();
            }
        });
        //添加头部
        View view = View.inflate(getActivity(), R.layout.header_1, null);
        listView.addHeaderView(view);
        ban = view.findViewById(R.id.ban);
        new MyAsynctask().execute(url);
    }

    @Override
    protected void initFindViewById(View view) {
        pull = view.findViewById(R.id.pull);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        instance = ImageLoader.getInstance();
        View inflate = inflater.inflate(R.layout.ch1, container, false);
        return inflate;
    }
    private void getNetData() {
        new MyAsynctask().execute(url);

    }

    class MyAsynctask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            return Netutils.getNetJson(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson g=new Gson();
            bean bean = g.fromJson(s, bean.class);
            List<com.bawei.day15_conghe2.Bean.bean.ResultBean.RowsBean> rows = bean.getResult().getRows();
            list.addAll(rows);
            for (int a=0;a<rows.size()-1;a++){
                img.add(rows.get(a).getInfo().getDefault_image());
            }
            m.notifyDataSetChanged();

            //配置重写好的加载图片的类
            ban.setImageLoader(new Myloader());
            //需要传入一个图片地址的集合
            ban.setImages(img);
            //设置自动轮播,默认为true
            ban.isAutoPlay(true);
            //设置轮播时间
            ban.setDelayTime(1000);
            //开启轮播
            ban.start();
            pull.onRefreshComplete();//关闭刷新头和底部;
        }
    }

    class Myadaper extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null){
                view=View.inflate(getActivity(),R.layout.ch1_1,null);
            }
            TextView tv= view.findViewById(R.id.tv);
            ImageView iv= view.findViewById(R.id.iv);
            tv.setText(list.get(i).getInfo().getLoupan_name());
            instance.displayImage(list.get(i).getInfo().getDefault_image(),iv);
            return view;
        }
    }
    /**
     * 这个类是Banner中的一个类;我们进行重写;
     */
    private class Myloader extends com.youth.banner.loader.ImageLoader{
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            instance.displayImage((String)path,imageView,Netutils.getDisplayImageOption());
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值