多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > PullToRefreshListView下拉刷新

PullToRefreshListView下拉刷新

來源:程序員人生   發布時間:2016-07-11 08:27:59 閱讀次數:2784次

弄了好幾天,各種bug,現在終究弄定了!!趕快開1篇blog,空話不說了 上代碼記錄1下,方便以后查看~~
MainActivity代碼:

package com.example.lenovo.pulltorefreshlistview; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.ArrayList; import java.util.List; import Adapter.MyBaseAdapter; import Adapter.MyViewPagerAdapter; import Bean.NewsBean; public class MainActivity extends Activity { private PullToRefreshListView listView; private ViewPager viewPager; private LinearLayout linearLayout; private ImageView[] points; private String jsonStr = "http://www.imooc.com/api/teacher?type=4&num=30"; private List<NewsBean.DataBean> listData = new ArrayList<>(); private String[] imagesUrl = { "http://bbs.uc.cn/data/attachment/forum/201302/17/163510xsv14x35i9ix41i0.jpg", "http://p3.so.qhimg.com/t0121ddd5bc66dcc9e8.jpg", "http://p3.so.qhimg.com/t0181e8d7355386f79d.jpg", "http://bbs.liebao.cn/data/attachment/forum/201210/25/182447qee2e922myyw8y8m.jpg"}; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); handler.sendEmptyMessageDelayed(1, 3000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initPoint(); viewPager.setCurrentItem(Integer.MAX_VALUE / 2); handler.sendEmptyMessageDelayed(1, 3000); viewPager.setAdapter(new MyViewPagerAdapter(this, imagesUrl)); requestNet(); //viewpager頁面滑動監聽 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < points.length; i++) { if (position % imagesUrl.length == i) { points[i].setImageResource(R.drawable.point_focus); } else { points[i].setImageResource(R.drawable.point_noraml); } } } @Override public void onPageScrollStateChanged(int state) { } }); listView.setOnRefreshListener(new PullToRefreshListView.onRefreshListener() { @Override public void onRefresh() { //要求服務器獲得數據 } @Override public void onLandmore() { //要求服務器獲得數據 } }); //listview中item的點事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //listview真實的position position = position-listView.getHeaderViewsCount(); Toast.makeText(MainActivity.this, "當前位置:"+position, Toast.LENGTH_SHORT).show(); } }); } /** * 摹擬要求服務器數據 */ private void requestNet() { RequestParams params = new RequestParams(jsonStr); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String s) { NewsBean news = new Gson().fromJson(s,NewsBean.class); listData.addAll(news.getData()); //1要求完數據就更新UI listView.setAdapter(new MyBaseAdapter(MainActivity.this,listData)); } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } }); } /** * 初始化小圓點 */ private void initPoint() { points = new ImageView[imagesUrl.length]; for(int i=0;i<points.length;i++){ points[i] = (ImageView) linearLayout.getChildAt(i); } points[0].setImageResource(R.drawable.point_focus); } private void initView() { listView = (PullToRefreshListView) findViewById(R.id.listview); View view = View.inflate(this,R.layout.headerview,null); linearLayout = (LinearLayout) view.findViewById(R.id.linearlayout); viewPager = (ViewPager) view.findViewById(R.id.viewpager); listView.addHeaderView(view); } }

ListView的數據Adapter代碼:

package 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 com.example.lenovo.pulltorefreshlistview.R; import org.xutils.x; import java.util.List; import Bean.NewsBean; /** * Created by shan on 2016/6/28. */ public class MyBaseAdapter extends BaseAdapter { private Context context; private List<NewsBean.DataBean> listData; public MyBaseAdapter(Context context, List<NewsBean.DataBean> listData) { this.context = context; this.listData = listData; } @Override public int getCount() { return listData.size(); } @Override public Object getItem(int position) { return listData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.news_item,parent,false); holder.news_icon = (ImageView) convertView.findViewById(R.id.news_icon); holder.news_title = (TextView) convertView.findViewById(R.id.news_title); holder.news_description = (TextView) convertView.findViewById(R.id.news_description); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } x.image().bind(holder.news_icon,listData.get(position).getPicBig()); holder.news_title.setText(listData.get(position).getName()); holder.news_description.setText(listData.get(position).getDescription()); return convertView; } private class ViewHolder{ private ImageView news_icon; private TextView news_title,news_description; } }

ViewPager的數據Adapter代碼:

package Adapter; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import org.xutils.x; import Utils.ToastUtil; /** * Created by shan on 2016/6/28. */ public class MyViewPagerAdapter extends PagerAdapter { private Context context; private String[] imagesUrl; public MyViewPagerAdapter(Context context, String[] imagesUrl) { this.context = context; this.imagesUrl = imagesUrl; } @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, final int position) { ImageView view = new ImageView(context); view.setScaleType(ImageView.ScaleType.CENTER_CROP); container.addView(view); //輪播圖中圖片的監聽 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch(position%imagesUrl.length){ case 0: ToastUtil.showShort(context,"點擊了圖片1"); break; case 1: ToastUtil.showShort(context,"點擊了圖片2"); break; case 2: ToastUtil.showShort(context,"點擊了圖片3"); break; case 3: ToastUtil.showShort(context,"點擊了圖片4"); break; } } }); x.image().bind(view,imagesUrl[position%imagesUrl.length]); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }

ListView的新聞列表item樣式代碼:

<?xml version="1.0" encoding="utf⑻"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:paddingRight="10dp"> <ImageView android:id="@+id/news_icon" android:layout_width="100dp" android:layout_height="70dp" android:scaleType="centerCrop" android:layout_marginLeft="10dp" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/news_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新聞標題" android:maxLines="1" android:textSize="16sp" android:layout_marginLeft="5dp" android:textColor="#000" android:layout_toRightOf="@+id/news_icon"/> <TextView android:id="@+id/news_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/news_icon" android:layout_below="@+id/news_title" android:layout_alignLeft="@+id/news_title" android:layout_marginTop="3dp" android:maxLines="2" android:text="新聞內容介紹"/> </RelativeLayout>

ListView的HeaderView(ViewPager+圓點)的xml代碼:

<?xml version="1.0" encoding="utf⑻"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="150dp"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/linearlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_marginTop="130dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/point_noraml"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/point_noraml"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/point_noraml"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/point_noraml"/> </LinearLayout> </RelativeLayout>

接下來是刷新頭view和加載腳view的xml代碼:
刷新頭xml:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pull_to_refresh_header" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:paddingTop="8dp" android:paddingBottom="8dp" android:gravity="center_horizontal"> <RelativeLayout android:id="@+id/refresh_progress" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ProgressBar android:id="@+id/pulldown_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/custom_progressbar" android:visibility="invisible"/> <ImageView android:id="@+id/arrow_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/arrow" android:layout_centerInParent="true" android:visibility="visible" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_centerInParent="true" android:layout_toRightOf="@+id/refresh_progress"> <TextView android:id="@+id/pulldown_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下拉刷新" android:textColor="#969696" android:textSize="16sp" /> <TextView android:id="@+id/pulldown_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="最近更新:12:00" android:layout_below="@+id/pulldown_text"/> </RelativeLayout> </RelativeLayout> </LinearLayout>

加載腳view代碼:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pull_to_refresh_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/custom_progressbar" android:visibility="visible"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加載..." android:layout_marginLeft="15dp" android:textColor="#969696" android:textSize="16sp" /> </LinearLayout>

說明1下:這里用的自定義的Progressbar,代碼以下:

<?xml version="1.0" encoding="utf⑻"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"> <shape android:shape="ring" android:useLevel="false" android:innerRadius="10dp" android:thickness="3dp"> <!--sweep橫掃而過的方式--> <gradient android:startColor="#fff" android:endColor="#00ffff" android:centerColor="#9f00" android:type="sweep"></gradient> </shape> </rotate>

援用方式:android:indeterminateDrawable=”@drawable/custom_progressbar

最后,PullToRefreshListView:

package com.example.lenovo.pulltorefreshlistview; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.AbsListView; import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by shan on 2016/6/26. * * 下拉刷新的listview */ public class PullToRefreshListView extends ListView implements AbsListView.OnScrollListener{ private static final int STATE_PULL_TO_REFRESH = 1; private static final int STATE_RELEASE_TO_REFRESH = 2; private static final int STATE_REFRESHING = 3; private int mCurrentState = STATE_PULL_TO_REFRESH;//當前狀態 //下拉刷新的頭view private View headerview; private int headerviewHeight;//下拉刷新view的高度 private int startY = -1;//起始Y坐標 private int endY;//終點坐標 //下拉刷新布局中的子view private TextView pulldown_text; private TextView pulldown_time; private ImageView arrow_down; private ProgressBar pulldown_progressbar; private RotateAnimation animUp; //向上的箭頭 private RotateAnimation animDown;//向下的箭頭 //上拉加載更多的腳view private View footerview; private int footerviewHeight;//下拉刷新view的高度 //上拉加載布局中的子view public PullToRefreshListView(Context context) { super(context); initHeaderView(); initFooterView(); } public PullToRefreshListView(Context context, AttributeSet attrs) { super(context, attrs); initHeaderView(); initFooterView(); } public PullToRefreshListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initHeaderView(); initFooterView(); } public PullToRefreshListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initHeaderView(); initFooterView(); } /** * 初始化頭布局 */ private void initHeaderView(){ headerview = View.inflate(getContext(), R.layout.refresh_header, null); this.addHeaderView(headerview); pulldown_text = (TextView) headerview.findViewById(R.id.pulldown_text); pulldown_time = (TextView) headerview.findViewById(R.id.pulldown_time); arrow_down = (ImageView) headerview.findViewById(R.id.arrow_down); pulldown_progressbar = (ProgressBar) headerview.findViewById(R.id.pulldown_progressbar); //隱藏下拉刷新布局 headerview.measure(0, 0); //取得下拉刷新view的高度 headerviewHeight = headerview.getMeasuredHeight(); //隱藏下拉刷新view headerview.setPadding(0, -headerviewHeight, 0, 0); //初始化箭頭的動畫 initAnimation(); setCurrentTime(); } /** * 初始化腳view */ private void initFooterView(){ footerview = View.inflate(getContext(), R.layout.refresh_footer, null); this.addFooterView(footerview); footerviewHeight = getMeasuredHeight(); footerview.measure(0, 0); footerview.setPadding(0, -footerviewHeight, 0, 0); this.setOnScrollListener(this);//滑動監聽 } @Override public boolean onTouchEvent(MotionEvent ev) { switch(ev.getAction()){ case MotionEvent.ACTION_DOWN: startY = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE: //當用戶按住viewpager進行下拉時,ACTION_DOWN會被viewpager消費掉,致使startY沒有賦值, // 此處需要重新獲得1下 if(startY == -1){ startY = (int) ev.getY(); } if(mCurrentState==STATE_REFRESHING){ //如果是正在刷新,跳出循環 break; } endY = (int) ev.getY(); //計算滑動【偏移量 int dy = endY - startY; int firstVisiblePositin = getFirstVisiblePosition();//當前顯示的第1個item的位置 //如果當前顯示的位置是ListView的第1項并且偏移量>0 再履行下拉操作 if(dy>0&&firstVisiblePositin==0){ //計算下拉刷新view被拉下來的高度 int padding = dy-headerviewHeight; headerview.setPadding(0,padding,0,0); if(padding>0&&mCurrentState!=STATE_RELEASE_TO_REFRESH){ //改成松開刷新 mCurrentState = STATE_RELEASE_TO_REFRESH; refreshState(); }else if(padding<0&&mCurrentState!=STATE_PULL_TO_REFRESH){ //改成下拉刷新 mCurrentState = STATE_PULL_TO_REFRESH; refreshState(); } super.onTouchEvent(ev); return true; } break; case MotionEvent.ACTION_UP: startY = -1; if(mCurrentState==STATE_RELEASE_TO_REFRESH){ mCurrentState = STATE_REFRESHING; refreshState(); //完全展現頭view headerview.setPadding(0, 0, 0, 0); //4.進行回調 if(mListener!=null){ mListener.onRefresh(); } }else if(mCurrentState==STATE_PULL_TO_REFRESH){ headerview.setPadding(0,-headerviewHeight,0,0); } break; } return super.onTouchEvent(ev); } /** * 根據當前狀態刷新界面 */ private void refreshState() { switch(mCurrentState){ case STATE_PULL_TO_REFRESH: pulldown_text.setText("下拉刷新"); pulldown_progressbar.setVisibility(View.INVISIBLE); arrow_down.setVisibility(View.VISIBLE); arrow_down.startAnimation(animDown); break; case STATE_RELEASE_TO_REFRESH: pulldown_text.setText("釋放立即刷新"); pulldown_progressbar.setVisibility(View.INVISIBLE); arrow_down.setVisibility(View.VISIBLE); arrow_down.startAnimation(animUp); break; case STATE_REFRESHING: pulldown_text.setText("正在刷新..."); arrow_down.clearAnimation();//清除箭頭動畫,否則沒法隱藏 pulldown_progressbar.setVisibility(View.VISIBLE); arrow_down.setVisibility(View.INVISIBLE); break; } } /** * 設置刷新當前時間 */ private void setCurrentTime(){ SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm"); String time = format.format(new Date()); pulldown_time.setText("上次更新:"+time); } /** * 初始化箭頭動畫 */ private void initAnimation(){ animUp = new RotateAnimation(0,-180, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); animUp.setDuration(200); animUp.setFillAfter(true); animDown = new RotateAnimation(-180,0, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); animDown.setDuration(200); animDown.setFillAfter(true); } /** * 刷新終了,收起刷新view,在網絡要求結束刷新終了后調用該方法,復原狀態 * * 即便網絡要求失敗致使刷新失敗,也要履行此步驟,以便再刷新 * * */ public void onRefreshComplete(boolean success){ if(!isLoadingMore){ headerview.setPadding(0,-headerviewHeight,0,0); mCurrentState = STATE_PULL_TO_REFRESH; pulldown_text.setText("下拉刷新"); pulldown_progressbar.setVisibility(View.INVISIBLE); arrow_down.setVisibility(View.VISIBLE); //刷新success了再更新上次刷新時間,刷新失敗的話,不需要更新本次刷新時間 if(success){ setCurrentTime(); } }else{ //加載更多 footerview.setPadding(0,-footerviewHeight,0,0);//隱藏腳view isLoadingMore = false; } } public onRefreshListener mListener;//3.定義成員變量,接收監聽對象 /** * 2.暴露接口,設置監聽 */ public void setOnRefreshListener(onRefreshListener listener){ mListener = listener; } private boolean isLoadingMore;//標記是不是正在加載動作 /** * 滑動狀態產生變化 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(scrollState==SCROLL_STATE_IDLE){//空閑狀態 int lastVisiblePositin = getLastVisiblePosition(); //表示滑到底,而且沒有正在加載更多 if(lastVisiblePositin==getCount()-1&&!isLoadingMore){ isLoadingMore = true; footerview.setPadding(0,0,0,0);//到底了,顯示腳view setSelection(getCount()-1);//將footerview顯示在最后的1個位置上 //通知主界面加載下1頁 if(mListener!=null){ mListener.onLandmore(); } } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } /** * 1.下拉刷新 上拉加載回調接口 */ public interface onRefreshListener{ public void onRefresh(); public void onLandmore(); } }
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 黄色在线网站视频 | 国产精品第一页爽爽影院 | 欧美在线播放成人免费 | 在线观看 a国v | 黄色h视频| 国产综合久久久久 | 国产女人精品性视频 | 真实国产乱人伦在线视频播放 | 亚洲日本视频在线 | 免费看h | 久久久精品久久 | wwwwww日本| freexx性欧美hd | 成人看片又黄又爽 | 国产人成亚洲第一网站在线播放 | 国产自产c区 | 国产成人精品视频 | 日本一级黄色大片 | 亚洲午夜久久久久中文字幕久 | 国产二区三区毛片 | 牛和人交videos欧美 | 波多野结衣在线观看网址 | 一级在线 | 欧洲 | 在线亚洲欧洲福利视频 | 国产激情久久久久久影院 | 欧美性天天 | 午夜亚洲一区二区福利 | 日本特黄视频 | 自由成熟的性色视频 | 中文字幕一区二区三区亚洲精品 | 亚洲天堂网视频 | 国产一区二区三区在线看 | 国产成人三级视频在线观看播放 | 狠狠色噜噜狠狠狠狠五月婷 | 婷婷激情五月 | 一级毛片免费一级直接观看 | 欧美成人观看免费完全 | 精品视频一区二区三区在线观看 | 欧美日韩国产一区二区三区不卡 | 沈樵在线观看福利 | 男女男精品视频站 |