ListView 드 롭 다운 리 셋 을 실현 하여 더 많은 인 스 턴 스 코드 를 불 러 옵 니 다(직접 가 져 옵 니 다)

ListView Api bixu 잘 봐 봐.
mNewsAdapter.notifyDataSetChanged();//ListView 새로 고침
사용자 정의 RefreashListView

package com.itguang.dell_pc.myapplication.view;
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 com.itguang.dell_pc.myapplication.R;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*   :     on 2016/7/7.
*   : [email protected]
*     :
*   :
*         ┏┓   ┏┓
*        ┏┛┻━━━┛┻┓━━━━┻┓
*        ┃       ┃
*        ┃   ━   ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ... ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting
*          ┃   ┃     ,   bug
*          ┃   ┃
*          ┃   ┃
*          ┃   ┃
*          ┃   ┃
*          ┃   ┗━━━┓
*          ┃BUG     ┣┓┣┓┣┓┣┓┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
public class RefreshListView extends ListView implements AbsListView.OnScrollListener {
private static final int STATE_PULL_REFRESH = 0;//     
private static final int STATE_RELEASE_REFRESH = 1;//     
private static final int STATE_REFRESHING = 2;//     
private int mCurrrentState = STATE_PULL_REFRESH;//     
private View mHeaderView;
private TextView tvTitle;
private TextView tvTime;
private ProgressBar pbProgress;
private ImageView ivArrow;
private RotateAnimation animUp;
private RotateAnimation animDown;
private int startY = -1;//      y  
private int endY;
private int measuredHeight;
private int mFooterViewHeight;
private View footerView;
public RefreshListView(Context context) {
super(context);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView();
initFooterView();
}
public RefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initHeaderView();
initFooterView();
}
/**
*       
*/
private void initHeaderView() {
mHeaderView = View.inflate(getContext(), R.layout.refeeash_header, null);
this.addHeaderView(mHeaderView);
tvTitle = (TextView) mHeaderView.findViewById(R.id.tv_title);
tvTime = (TextView) mHeaderView.findViewById(R.id.tv_time);
pbProgress = (ProgressBar) mHeaderView.findViewById(R.id.pb_progress);
ivArrow = (ImageView) mHeaderView.findViewById(R.id.iv_arr);
mHeaderView.measure(0, 0);//          
measuredHeight = mHeaderView.getMeasuredHeight();
mHeaderView.setPadding(0, -measuredHeight, 0, 0);
initArrowAnim();
tvTime.setText("      :" + getCurrentTime());
}
/**
*       
*/
public void initFooterView() {
footerView = View.inflate(getContext(), R.layout.refreash_listview_footer, null);
this.addFooterView(footerView);
footerView.measure(0, 0);
mFooterViewHeight = footerView.getMeasuredHeight();
footerView.setPadding(0, -mFooterViewHeight, 0, 0);//       
this.setOnScrollListener(this);
}
private boolean isLoadingMOre;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_FLING || scrollState == SCROLL_STATE_IDLE) {
if (getLastVisiblePosition() == getCount() - 1 && !isLoadingMOre) {//    
System.out.println("   ......");
footerView.setPadding(0, 0, 0, 0);
setSelection(getCount() - 1);//  ListView     
isLoadingMOre = true;
if (mListener != null) {
mListener.onLoadMore();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
if (startY == -1) {//        MotionEvent.ACTION_DOWN   ,       startY  
startY = (int) ev.getRawY();
}
//        ,    ,        
if (mCurrrentState == STATE_RELEASE_REFRESH) {
break;
}
endY = (int) ev.getRawY();
int dy = endY - startY;//        
if (dy > 0 && getFirstVisiblePosition() == 0) {//             item,     
int padding = dy - measuredHeight;//  padding
mHeaderView.setPadding(0, padding, 0, 0);//    padding
if (padding > 0 && mCurrrentState != STATE_RELEASE_REFRESH) {
mCurrrentState = STATE_RELEASE_REFRESH;
refreshState();
} else if (padding < 0 && mCurrrentState != STATE_PULL_REFRESH) {//         
mCurrrentState = STATE_PULL_REFRESH;
refreshState();
}
return true;
}
break;
case MotionEvent.ACTION_UP:
startY = -1;//      
//              ,    
if (mCurrrentState == STATE_RELEASE_REFRESH) {
mCurrrentState = STATE_REFRESHING;//     
mHeaderView.setPadding(0, 0, 0, 0);//   
refreshState();
} else if (mCurrrentState == STATE_PULL_REFRESH) {
mHeaderView.setPadding(0, -measuredHeight, 0, 0);//   
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
private String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = format.format(new Date());
return currentTime;
}
private void initArrowAnim() {
//       
animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animUp.setDuration(500);
animUp.setFillAfter(true);//    
//      
animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animDown.setDuration(500);
animDown.setFillAfter(true);
}
/**
*          
*/
private void refreshState() {
switch (mCurrrentState) {
case STATE_PULL_REFRESH:
tvTitle.setText("    ");
ivArrow.setVisibility(View.VISIBLE);
pbProgress.setVisibility(View.INVISIBLE);
ivArrow.startAnimation(animDown);
break;
case STATE_RELEASE_REFRESH:
tvTitle.setText("    ");
ivArrow.setVisibility(View.VISIBLE);
pbProgress.setVisibility(View.INVISIBLE);
ivArrow.startAnimation(animUp);
break;
case STATE_REFRESHING:
tvTitle.setText("    ...");
ivArrow.clearAnimation();//        ,    
ivArrow.setVisibility(View.INVISIBLE);
pbProgress.setVisibility(View.VISIBLE);
if (mListener != null) {
mListener.onRefreash();
}
break;
default:
break;
}
}
OnRefreashListener mListener;
public void setOnRefreashListener(OnRefreashListener listener) {
mListener = listener;
}
public interface OnRefreashListener {
void onRefreash();
void onLoadMore();
}
/**
*          
*/
public void onRefreashComplete() {
if (isLoadingMOre) {
footerView.setPadding(0, -mFooterViewHeight, 0, 0);//     
isLoadingMOre = false;
} else {
mCurrrentState = STATE_PULL_REFRESH;
tvTitle.setText("    ");
ivArrow.setVisibility(View.VISIBLE);
pbProgress.setVisibility(View.INVISIBLE);
mHeaderView.setPadding(0, -measuredHeight, 0, 0);//  
}
}
public void noFooterView() {
footerView.setPadding(0, mFooterViewHeight, 0, 0);//     
}
}
코드 에서 RefreashListView 참조

package com.itguang.dell_pc.myapplication.base;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.itguang.dell_pc.myapplication.R;
import com.itguang.dell_pc.myapplication.domain.NewsData;
import com.itguang.dell_pc.myapplication.domain.TabData;
import com.itguang.dell_pc.myapplication.globe.GlobeContents;
import com.itguang.dell_pc.myapplication.view.RefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.CirclePageIndicator;
import java.util.ArrayList;
/**
*   :     on 2016/4/22.
*   : [email protected]
*     :
*   :
*         ┏┓   ┏┓
*        ┏┛┻━━━┛┻┓━━━━┻┓
*        ┃       ┃
*        ┃   ━   ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ... ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting
*          ┃   ┃     ,   bug
*          ┃   ┃
*          ┃   ┃
*          ┃   ┃
*          ┃   ┃
*          ┃   ┗━━━┓
*          ┃BUG     ┣┓┣┓┣┓┣┓┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
/**
*      
*/
public class TabDetailPager extends BaseMenuDetailPager implements ViewPager.OnPageChangeListener {
/**
* @param activity
* @param newsTabData
*/
NewsData.NewsTabData mTabdata;
private String mUrl;
private TabData mTabDetailData;
@ViewInject(R.id.vp_news)
private ViewPager mViewPager;
@ViewInject(R.id.lv_newslist)
private RefreshListView newsListView;//    
@ViewInject(R.id.tv_title)
private TextView tvTitle;//    
@ViewInject(R.id.indicator)
private CirclePageIndicator indicator;//       
private ArrayList<TabData.TopNewsData> mTopNewsList;//      
private int[] images;
private ArrayList<TabData.TabNewsData> mNewsList;//      
private String mMoreUrl;
private NewsAdapter mNewsAdapter;
public TabDetailPager(Activity activity, NewsData.NewsTabData newsTabData) {
super(activity);
mTabdata = newsTabData;
mUrl = GlobeContents.SERVER_URL + mTabdata.url;
}
@Override
public View initViews() {
images = new int[]{R.drawable.topnews1, R.drawable.topnews21, R.drawable.topnews31, R.drawable.topimage};
View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
View headerView = View.inflate(mActivity, R.layout.list_header_topnews, null);//     
// TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
//    
ViewUtils.inject(this, view);
ViewUtils.inject(this, headerView);
mViewPager.addOnPageChangeListener(this);
//               listView 
newsListView.addHeaderView(headerView);
//        
newsListView.setOnRefreashListener(new RefreshListView.OnRefreashListener() {
@Override
public void onRefreash() {
getDataFromServer();
}
@Override
public void onLoadMore() {
if (mMoreUrl != null) {
getMoreDataFromServer();
} else {
Toast.makeText(mActivity, "       ", Toast.LENGTH_SHORT).show();
newsListView.onRefreashComplete();
// newsListView.noFooterView();
}
}
});
return view;
}
@Override
public void initData() {
getDataFromServer();//       ,
}
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpRequest.HttpMethod.GET, mUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = (String) responseInfo.result;
// System.out.println("         :+++++++++++++++++++" + result);
praseData(result, false);
newsListView.onRefreashComplete();
}
@Override
public void onFailure(HttpException e, String s) {
Toast.makeText(mActivity, "  Tabdetaildata  !", Toast.LENGTH_SHORT).show();
// error.printStackTrace();
}
});
}
/**
*        
*/
private void getMoreDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpRequest.HttpMethod.GET, mMoreUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = (String) responseInfo.result;
// System.out.println("         :+++++++++++++++++++" + result);
praseData(result, true);
newsListView.onRefreashComplete();
}
@Override
public void onFailure(HttpException e, String s) {
Toast.makeText(mActivity, "  Tabdetaildata  !", Toast.LENGTH_SHORT).show();
// error.printStackTrace();
}
});
}
/**
*  json       json  
*
* @param result      json   
*/
private void praseData(String result, boolean isLoadMore) {
Gson gson = new Gson();
mTabDetailData = gson.fromJson(result, TabData.class);//  TabData  
System.out.println("     :+++++++++++++++++++++" + mTabdata);
//         
final String more = mTabDetailData.data.more;
if (!TextUtils.isEmpty(more)) {
mMoreUrl = GlobeContents.SERVER_URL + more;
} else {
mMoreUrl = null;
}
if (!isLoadMore) {
mTopNewsList = mTabDetailData.data.topnews;
mNewsList = mTabDetailData.data.news;
if (mTopNewsList != null) {
mViewPager.setAdapter(new TopNewsAdapter());
indicator.setOnPageChangeListener(this);
indicator.setViewPager(mViewPager);//        ,      
indicator.setSnap(true);//      
indicator.onPageSelected(0);//indicator     ,          
tvTitle.setText(mTopNewsList.get(0).title);
}
mNewsAdapter = new NewsAdapter();
//      
if (mNewsList != null) {
newsListView.setAdapter(mNewsAdapter);
}
} else {//        ,             
ArrayList<TabData.TabNewsData> news = mTabDetailData.data.news;
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();//  ListView
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//        
TabData.TopNewsData topNewsData = mTopNewsList.get(position);
tvTitle.setText(topNewsData.title);
}
@Override
public void onPageScrollStateChanged(int state) {
}
/**
* ListView   
*/
class NewsAdapter extends BaseAdapter {
private final BitmapUtils utils;
NewsAdapter() {
utils = new BitmapUtils(mActivity);
utils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
}
@Override
public int getCount() {
return mNewsList.size();
}
@Override
public Object getItem(int position) {
return mNewsList.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) {
convertView = View.inflate(mActivity, R.layout.list_news_item, null);
holder = new ViewHolder();
holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic);
holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
holder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
TabData.TabNewsData item = (TabData.TabNewsData) getItem(position);
holder.tvTitle.setText(item.title);
holder.tvDate.setText(item.pubdate);
utils.display(holder.ivPic, item.listimage);
return convertView;
}
}
static class ViewHolder {
public TextView tvTitle;
public TextView tvDate;
public ImageView ivPic;
}
/**
* Viewpager   
*/
class TopNewsAdapter extends PagerAdapter {
private BitmapUtils bitmapUtils;
TopNewsAdapter() {
bitmapUtils = new BitmapUtils(mActivity);
bitmapUtils.configDefaultLoadingImage(R.drawable.topnews_item_default);//      
}
@Override
public int getCount() {
return mTabDetailData.data.topnews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mActivity);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);//          
// TabData.TopNewsData topNewsData = mTopNewsList.get(position);
// bitmapUtils.display(imageView,mTabDetailData.data.topnews.get(position).url);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
위 에서 말 한 것 은 편집장 이 여러분 에 게 소개 한 ListView 가 드 롭 다운 리 셋 을 실현 하고 더 많은 인 스 턴 스 코드(직접 가 져 와 서 사용)를 불 러 오 는 모든 서술 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.편집장 은 제때에 여러분 에 게 답장 을 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기