ViewPage 水平滑动加入刷新功能

来源:互联网 发布:oracle sql rowcount 编辑:程序博客网 时间:2024/06/11 04:13
package com.tuma.tutududu.story.module.music;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.tuma.tutududu.story.R;import com.tuma.tutududu.story.module.classes.adapter.StoryPagerAdapter;public class MainActivity extends AppCompatActivity {    StoryPagerAdapter mStoryPagerAdapter=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ViewPager viewPager= (ViewPager) findViewById(R.id.viewPage);        viewPager.setAdapter(mStoryPagerAdapter=new StoryPagerAdapter(this));        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {                   viewPager.postDelayed(new Runnable() {
                       @Override                       public void run() {                           if(position==mStoryPagerAdapter.getCount()-2){  
//滑动到倒数第二个 开始模拟网络请求
mStoryPagerAdapter.setStorySize(30); View view=viewPager.findViewById(position); if(view!=null){ TextView textView= (TextView) view.findViewById(R.id.tv_text); textView.setVisibility(View.VISIBLE); mStoryPagerAdapter.notifyDataSetChanged(); Toast.makeText(MainActivity.this,"数据加载成功",Toast.LENGTH_SHORT).show(); } } } },1000); } @Override public void onPageScrollStateChanged(int state) { } }); }}
2.ViewPageAdapter 的使用
package com.tuma.tutududu.story.module.classes.adapter;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import com.tuma.tutududu.story.R;import java.util.ArrayList;/** * Created by VS on 2017/11/14. */public class StoryPagerAdapter extends PagerAdapter{    ArrayList storyList=new ArrayList();    private Context mcontext;    int StorySize=10;    public StoryPagerAdapter(Context context){                this.mcontext=context;    }    @Override    public int getCount() {        return StorySize;    }    public void setStorySize(int size){        this.StorySize=size;    }    @Override    public boolean isViewFromObject(View view, Object object) {        return  view == object;    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        View  childView=View.inflate(mcontext, R.layout.constraintlayout_lay,null);        View mview=childView.findViewById(R.id.tv_text);        container .addView (childView);        childView.setId(position);        if(position==getCount()-1){            mview.setVisibility(View.INVISIBLE);        }        return childView;    }    @Override    public int getItemPosition(Object object) {        return POSITION_NONE;    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {        container.removeView((View) object);    }}

3.布局
<?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"    tools:context="com.tuma.tutududu.story.module.music.MainActivity">    <android.support.v4.view.ViewPager        android:id="@+id/viewPage"        android:background="@android:color/darker_gray"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </android.support.v4.view.ViewPager></RelativeLayout>

4.viewPagerAdapter 中的布局
<?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">    <TextView        android:id="@+id/tv_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="80dp"        android:text="1345611"/>    <View        android:id="@+id/view"        android:layout_width="1dp"        android:background="@color/cardview_dark_background"        android:layout_alignParentRight="true"        android:layout_height="match_parent"/></RelativeLayout>

 
原创粉丝点击