Android skillfully uses XListView to realize universal drop down refresh control

  • 2021-11-01 04:30:01
  • OfStack

Absrtact: Presumably, when you do development, you will use drop-down refresh controls, and now there are numerous drop-down refresh controls of the third party. Of course, the most NB is XListView. There are other drop-down refresh controls that override GridView, ScrollView, and LinearLayout. In this paper, xListView to take a clever way to achieve a variety of control pull-down refresh.

This clever thinking might have been thought of, because ListView has its own addHeaderView method, with which we can add View of any layout. Therefore, the idea of this article is to add our custom layout file to the header of xListView.

However, it should be noted here that if the header custom layout file contains such controls as ListView, GridView, etc., its onMeasure method should be overridden (PS, which you often see when nesting ListView in ScrollView).


 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
 int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
 MeasureSpec.AT_MOST);
 super.onMeasure(widthMeasureSpec, expandSpec);
 }

However, there are many models of Android, and some mobile phone manufacturers will force ListView, ScrollView and other controls to add rebound effect, which is similar to the elastic effect of IOS, such as Meizu. Therefore, it is best to override its dispatchTouchEvent method and prohibit it from sliding.


 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
 // TODO Auto-generated method stub
 if (ev.getAction() == MotionEvent.ACTION_MOVE) {
 return true;
 }
 return super.dispatchTouchEvent(ev);
 }

Create a new Android program, and write down the related operations of xListView in its MainActivity. The code is as follows:


public class MainActivity extends Activity implements Callback{
 
 private XListView mXListView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mXListView = (XListView)findViewById(R.id.main_xlv);
  View header = LayoutInflater.from(this).inflate(R.layout.view_header, null);
  TextView headerTv = (TextView)header.findViewById(R.id.tv);
  headerTv.setTextSize(28);
  mXListView.setAdapter(new EmptyAdapter());
 mXListView.addHeaderView(header);
 mXListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
 mXListView.showHeader(true);
 mXListView.setIsAutoLoadMore(false);
 mXListView.setCallback(this);
 }
 
 /** 
 * @see com.dighammer.xlistview.XListView.Callback#onHeaderTriggerd()
 */
 @Override
 public void onHeaderTriggerd() {
 // TODO Auto-generated method stub
 mXListView.headerFinished();
 }
 
 
 /** 
 * @see com.dighammer.xlistview.XListView.Callback#onFooterTriggerd()
 */
 @Override
 public void onFooterTriggerd() {
 // TODO Auto-generated method stub
 
 }
 
}

The key code part is the XListView related operation of onCreate method. Among them, 1 must write an empty BaseAdapter, otherwise UI cannot be displayed.

With this approach, we don't need to import all kinds of drop-down refreshed third-party controls in the future. As long as we have XListView, we can do a lot of things.

Source code download: Android skillfully uses XListView to realize drop-down refresh control


Related articles: