Android listview pull-down refresh, basic code

Source: Internet
Author: User

Some examples are found on the internet, and all functions are well implemented. Various refreshes and features are many, which means that there are many codes. If you want to customize your own pull-down refresh, you can't start with it.

I wrote the simplest example for pull-down refresh. It is as simple as only the drop-down interface changes, the time to refresh, and the time to process the changes. Please do it yourself.

This code is like writing the "Hello word" of a program. It takes you to get started, understand the basic principle, and deeply understand the code. Someone has done a good job on the Internet. We recommend a variety of refreshing source code: android-pulltorefresh-master.

Note: The following Code does not require the layout file. The layout of the code is used to generate the data required by the listview, Which is simplified as much as possible and can help readers.

package com.kevinshq.pulltorefresh;import android.app.Activity;import android.graphics.Color;import android.graphics.Typeface;import android.os.Bundle;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.TextView;public class PullToRefreshActivity extends Activity {public static final String TAG = "PullToRefreshActivity";private final static int RATIO = 2;private boolean isRecored;private int firstItemIndex = 0;private float startY = 0;private TextView headTextView = null;private ListView listView = null;private View.OnTouchListener onTouchListener = new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubfloat tempY = event.getY();firstItemIndex = listView.getFirstVisiblePosition();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:if (!isRecored && (firstItemIndex == 0)) {isRecored = true;startY = tempY;}break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:isRecored = false;break;default:break;}switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:if (tempY > startY) {listView.setPadding(0, -200, 0, 0);}break;case MotionEvent.ACTION_MOVE:if (isRecored && tempY > startY) {listView.setPadding(0, (int) ((tempY-startY)/RATIO-200), 0, 0);}break;default:break;}return false;}};private ListAdapter adapter = new BaseAdapter() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubTextView tv = (TextView) convertView;if (tv == null) {tv = new TextView(getApplicationContext());}tv.setGravity(Gravity.CENTER);tv.setMinHeight(50);tv.setText(String.valueOf(position));tv.setBackgroundColor(Color.GRAY);return tv;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 50;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);ViewGroup.LayoutParams lp_ViewGroup = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);LinearLayout linearLayout = new LinearLayout(this);linearLayout.setOrientation(LinearLayout.VERTICAL);setContentView(linearLayout, lp_ViewGroup);headTextView = new TextView(this);headTextView.setGravity(Gravity.CENTER);headTextView.setMinHeight(200);headTextView.setText("Head");headTextView.setTypeface(Typeface.DEFAULT_BOLD);headTextView.setTextSize(50);headTextView.setBackgroundColor(Color.GREEN);headTextView.invalidate();LinearLayout.LayoutParams lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);listView = new ListView(this);listView.addHeaderView(headTextView, null, false);listView.setOnTouchListener(onTouchListener);listView.setAdapter(adapter);listView.setPadding(0, -200, 0, 0);linearLayout.addView(listView, lp_LinearLayout);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.