美文网首页
SwipeToLayout

SwipeToLayout

作者: lx0306 | 来源:发表于2017-03-06 20:46 被阅读0次

添加依赖


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

        //添加过渡滑动 其他设置 自己根据英文尝试吧
        swipeToLoadLayout.setRefreshCompleteDelayDuration(1000);

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


        //为swipeToLoadLayout设置下拉刷新监听者
        swipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                for (int i = 0; i < 3; i++) {
                    list.add(0,"刷新获得的数据");
                }
                adapter.notifyDataSetChanged();
                //设置下拉刷新结束
                swipeToLoadLayout.setRefreshing(false);
            }
        });
        //为swipeToLoadLayout设置上拉加载更多监听者
        swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                for (int i = 0; i < 3; i++) {
                    list.add("加载更多获得的数据");
                }
                adapter.notifyDataSetChanged();
                //设置上拉加载更多结束
                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适应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>


给头部和脚部设一个布局

<?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>

相关文章

  • SwipeToLayout

    添加依赖 mainActivity中的代码 footview中自定义LinearLayout再自定义一个Progr...

网友评论

      本文标题:SwipeToLayout

      本文链接:https://www.haomeiwen.com/subject/qagugttx.html