SwipeToLayout

添加依賴


 compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.3'
    compile 'com.android.support:recyclerview-v7:23.+'

mainActivity中的代碼

package com.example.swipetoloadlayout1;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
   private SwipeToLoadLayout swipeToLoadLayout;
    private ListView listView;
    private List list;
    private RefreshHeaderView refreshHeaderView;
    private ScrollView scrollView;
    private ArrayAdapter<String> adapter;
    private ViewPager vp;
    private int[] imgs={R.mipmap.caoqian,R.mipmap.yuxi,R.mipmap.kunlun,R.mipmap.liqihui
                  ,R.mipmap.zhangguohua,R.mipmap.zhangzhenhuan};
    private List<ImageView> imglist;
    private MyPageAdapter myPageAdapter;
    private boolean flag;
    private int index;
private LoadMoreFooterView loadMoreFooterView;
    private boolean b;

    private ImageView[] imgbot=new ImageView[imgs.length];
    private LinearLayout container;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
        refreshHeaderView = (RefreshHeaderView) findViewById(R.id.swipe_refresh_header);
        loadMoreFooterView=(LoadMoreFooterView)findViewById(R.id.swipe_load_more_footer);
        vp=(ViewPager)findViewById(R.id.vp);
        scrollView=(ScrollView)findViewById(R.id.swipe_target);
        container=(LinearLayout)findViewById(R.id.container);

        scrollView.smoothScrollTo(0,0);
        swipeToLoadLayout.setRefreshHeaderView(refreshHeaderView);

        //添加過渡滑動 其他設(shè)置 自己根據(jù)英文嘗試吧
        swipeToLoadLayout.setRefreshCompleteDelayDuration(1000);

        listView = (ListView) findViewById(R.id.lv);
        list = new ArrayList();
        for (int i = 0; i < 20; i++) {
            list.add("原始數(shù)據(jù)");
        }
        adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);


        //為swipeToLoadLayout設(shè)置下拉刷新監(jiān)聽者
        swipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                for (int i = 0; i < 3; i++) {
                    list.add(0,"刷新獲得的數(shù)據(jù)");
                }
                adapter.notifyDataSetChanged();
                //設(shè)置下拉刷新結(jié)束
                swipeToLoadLayout.setRefreshing(false);
            }
        });
        //為swipeToLoadLayout設(shè)置上拉加載更多監(jiān)聽者
        swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                for (int i = 0; i < 3; i++) {
                    list.add("加載更多獲得的數(shù)據(jù)");
                }
                adapter.notifyDataSetChanged();
                //設(shè)置上拉加載更多結(jié)束
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
        initViewPager();
        initBottom();
    }

    public void initViewPager(){
        imglist=new ArrayList<ImageView>();
        for(int i=0;i<imgs.length;i++){
            ImageView img=new ImageView(this);
            img.setImageResource(imgs[i]);

            imglist.add(img);
        }
        myPageAdapter=new MyPageAdapter(imglist);
        vp.setAdapter(myPageAdapter);

        vp.setOnPageChangeListener(this);
        t.start();
    }

    public void initBottom(){
        for(int i=0;i<imgs.length;i++){
            ImageView bottom=new ImageView(this);
            //給 bottom控件加上布局屬性
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(60, 60);
            params.setMargins(5, 0,5, 0);
            bottom.setLayoutParams(params);
            if(i==0){
                bottom.setImageResource(R.mipmap.xuanze1);
            }else{
                bottom.setImageResource(R.mipmap.xuanze);
            }
            imgbot[i]=bottom;
            container.addView(bottom);
        }
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
          index=position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if(state==ViewPager.SCROLL_STATE_IDLE){
            b=false;
        }else{
            b=true;
        }
    }

    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            while(!flag){

                try {
                    Thread.sleep(3000);

                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                if(b){
                    return;
                }
               index++;

                vp.setCurrentItem(index);
            }

        }
    };
}


footview中
自定義LinearLayout
再自定義一個ProgressBar

package com.example.swipetoloadlayout1;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class MyProgressBar extends ProgressBar{

    public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyProgressBar(Context context) {
        super(context);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }



}


自定義一個 headview
自定義LinearLayout

自定義listview

public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    /**
     * 重寫該方法,達到使ListView適應(yīng)ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

寫一個適配器

public class MyPageAdapter extends PagerAdapter {
    private List<ImageView> list;
    public MyPageAdapter(List<ImageView> list){
        this.list=list;
    }
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int p=position%list.size();

        container.addView(list.get(p));
        return list.get(p);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        int p=position%list.size();

        container.removeView(list.get(p));
    }
}

主 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.swipetoloadlayout1.MainActivity">

    <com.aspsine.swipetoloadlayout.SwipeToLoadLayout
        android:id="@+id/swipeToLoadLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.example.swipetoloadlayout1.RefreshHeaderView
            android:id="@id/swipe_refresh_header"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="center"/>



                    <ScrollView
                        android:id="@+id/swipe_target"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                            <android.support.v4.view.ViewPager
                                android:id="@+id/vp"
                                android:layout_width="match_parent"
                                android:layout_height="100dp"
                                >
                               <LinearLayout
                                   android:id="@+id/container"
                                   android:layout_width="30dp"
                                   android:layout_height="match_parent"
                                   android:orientation="horizontal"
                                   android:layout_marginTop="95dp"
                                   android:layout_marginLeft="300dp"
                                   ></LinearLayout>
                            </android.support.v4.view.ViewPager>
                            <com.example.swipetoloadlayout1.ListViewForScrollView
                                android:id="@+id/lv"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">

                            </com.example.swipetoloadlayout1.ListViewForScrollView>
                        </LinearLayout>

                    </ScrollView>

        <com.example.swipetoloadlayout1.LoadMoreFooterView
            android:id="@id/swipe_load_more_footer"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:gravity="center" />
    </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</RelativeLayout>


給頭部和腳部設(shè)一個布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:gravity="center_horizontal"
>

      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
          <ProgressBar
              android:id="@+id/pb"
              android:layout_width="40dp"
              android:layout_height="40dp"
              />

          <TextView
              android:id="@+id/tv"
              android:layout_width="100dp"
              android:layout_height="40dp"
              android:textSize="16sp"
              android:gravity="center_vertical"
              android:layout_marginLeft="10dp"/>
      </LinearLayout>
</RelativeLayout>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,353評論 25 708
  • Day1: 在代碼中通過R.string.hello_world可以獲得該字符串的引用; 在XML中通過@stri...
    冰凝雪國閱讀 1,451評論 0 5
  • 因為,昨天的中午飯做的挺成功,嘎娘信心大增。今天中午,嘎娘把凍了好久的,以前涮鍋子剩下的羊肉片,拿了出來,想做成蒜...
    a宮雨閱讀 307評論 0 2
  • 對于現(xiàn)在的大多數(shù)人來說,我們的生活是過于安逸并且充滿誘惑的!很多人因太滿足于安逸的生活,而日漸墮落在前行的路上,終...
    辛昳閱讀 358評論 0 0
  • 我:對了,那個,有個地方可能要改一下...工程師:你看著我的眼睛!我:...我:我有一個好消息和一個壞消息,你想先...
    楊夏閱讀 2,194評論 12 28