Pull-down refresh and pull-up loading of ListView of Android custom controls

 In the process of developing the project, the pull-down refresh and slide-up loading of listView are basically used. In order to facilitate the rewritten ListView to achieve pull-down refresh, the function of pull-up and automatic loading is added.

 

Android pull-to-refresh can be divided into two situations:

 

1. Get more data and store it in the server database in chronological order. At this moment, we are getting the data that is displayed in our application earlier. This is also the most common situation. For example (to obtain more information on Weibo is to obtain more earlier information, and then dynamically add it to the bottom of the existing data);

2. Obtaining more up-to-date data is actually a way of obtaining more. But here mainly consider the user's operating habits. Generally, there are two types of user's operating habits.

 

sample code

public class MainActivity extends Activity implements IXListViewListener {

	private XListView listView;
	private int in = 6;
	private Adapter adapter;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		listView = (XListView) findViewById(R.id.list);
		listView.setPullLoadEnable(true);
		adapter = new Adapter(MainActivity.this);
		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		mHandler = new Handler();

	}

	private void onLoad() {
		listView.stopRefresh();
		listView.stopLoadMore();
		listView.setRefreshTime("Just now");
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				in += 4;
				onLoad();
			}
		}, 2000);
	}

	private static String[] string = new String[] { "Civil Affairs Bureau, please bring relevant documents",
			"The Ministry of Finance issued an announcement that due to incomplete information on capital adjustment, wages will be paid next week, please forgive me",
			"Information department held a meeting in such and such place on 2016-11-11, hereby inform you, please bring relevant documents",
			"The Party and Government Office issued a notice regarding poverty alleviation policies in poor areas, hoping that each department will make a corresponding plan and discuss it at a meeting on 2016-11-11" };

	public class Adapter extends BaseAdapter {

		private Context context;
		private LayoutInflater inflater;

		public Adapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return in;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			final int index = position;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_news_mass, null);
				holder = new ViewHolder(convertView);
				convertView.setTag (holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mName.setText(string[(position % 4)]);

			return convertView;
		}

		class ViewHolder {
			private ImageView mImageView;
			private TextView mName;

			public ViewHolder(View view) {
				mName = (TextView) view.findViewById(R.id.item_news_msg);

			}
		}

	}
}

 

 

<com.example.pullablerefresh.XListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#FFFFFF"
        android:cacheColorHint="#00000000"
        android:divider="#FFFFFF"
        android:dividerHeight="5dp"
        android:drawSelectorOnTop="false"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:scrollingCache="false" />

 Due to too much code, the complete code is not given, the source code can be downloaded directly

 

Source code click to download

 

Guess you like

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