14. Custom ListView

        After realizing the display effect, then implement some listeners (interfaces), such as pull-down refresh, pull-up loading, and Item swipe out of the screen (which will be used later), and define two interfaces:
public interface QRecyclerListener{
	void onMovedToScrapHeap(View view);//Item swipe screen callback function
}
public void setQRecyclerListener(QRecyclerListener listener){//设置
	this.rclistener=listener;
}
	
public interface QPullListener{
	void onRefresh();//Pull down refresh callback function
	void onLoad();//Pull-up loading callback function
}
public void setQPullListener(QPullListener listener){//设置
	this.pulllistener=listener;
}

        Just call pulllistener.onRefresh(); (or other) where you need it, as in the touch event in the previous section to call pull-to-refresh.
        Pull-up loading is to judge whether the end of the page is about to be reached when the page is sliding, and call pulllistener.onLoad();. For this reason, CListView needs to implement the OnScrollListener interface and implement its methods.

        Implement the OnScrollListener interface

private class CListView extends ListView implements OnScrollListener


        implement two methods
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
	// TODO Auto-generated method stub
	if(firstVisibleItem+visibleItemCount>=totalItemCount&&!isLoad&&pulllistener!=null){
		pulllistener.onLoad();
		isLoad=true;//A sign of whether it is loading
	}
}
		
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
	// TODO Auto-generated method stub
	
}

        Item draws the interface of the screen and implements the RecyclerListener interface
private class CListView extends ListView implements OnScrollListener, RecyclerListener

        implement a method
@Override
public void onMovedToScrapHeap(View view) {
	// TODO Auto-generated method stub
	if(rclistener!=null&&getFirstVisiblePosition()>1){
		rclistener.onMovedToScrapHeap(view);
	}
}


        Add functions for refresh completion, loading completion, and restoring the initial state
public void completeRefresh(int state){
	iv_refresh.clearAnimation();//Stop animation
	rl_refresh.setPadding(0, -iv_refresh.getHeight(), 0, 0);
	clv_list.isRefresh=false;
}
public void completeLoad(int state){
	clv_list.isLoad=false;
}

        Attach variable definition and animation Xml
private ImageView iv_refresh;//Refresh the prompt icon
private RelativeLayout rl_refresh;//Icon parent layout, change the icon position by changing Padding
private Animation anim_rotate;//Rotation animation when refreshing
	
private CListView clv_list;//The internal definition CListView inherits from ListView
private QRecyclerListener rclistener;//Internal Item draw screen listener
private QPullListener pulllistener;//Refresh load listener


        Create a new animation file anim_rotate_round.xml under the         rotation animation res/anim

<?xml version="1.0" encoding="utf-8"?>
<rotate
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    	android:fromDegrees="0"
    	android:toDegrees="720"
    	android:duration="1080"
    	android:repeatCount="-1"
    	android:pivotX="50%"  
    	android:pivotY="50%" >
</rotate>


        Test effect ListFragment
private void initListener(){
	adapter=new ListAdapter(getActivity(),qlv_list);
	qlv_list.setAdapter(adapter);
		
	qlv_list.setQPullListener(qplistener);
}

private QPullListener qplistener=new QPullListener() {
	
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		qlv_list.completeRefresh(0);
		System.out.println("刷新");
	}
		
	@Override
	public void onLoad() {
		// TODO Auto-generated method stub
		qlv_list.completeLoad(0);
		System.out.println("Load");
	};
}


Note: I'm lazy, I use one for many pictures, see if there is corresponding text in the output information

Always believe——2017/05/16



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326198273&siteId=291194637