Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 循環ListView

Android 循環ListView

編輯:關於Android編程

Android 循環ListView 之前做個iOS下的,實現起來比較復雜,後來在網上找了一個例子,其實現起來比較容易,思路很好,這個我做個整理

代碼下載:http://pan.baidu.com/s/1bnEKxd1

這個類就是個ListView的Adapter沒什麼特別的。

ListAdapter.java

package com.pactera.infinitescrollview;

import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{

	private List list;
	private LayoutInflater mInflater;
	
	public ListAdapter (Context context,List list) {
		
		this.list  = list;
		this.mInflater = LayoutInflater.from(context);
	}
	
	
	
	
	
	
	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		
		if (list != null) {
			//這個內容要是顯示內容的3倍
			return list.size() * 3;
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0 % list.size());
	}
	


	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0 % list.size();
	}
	
	

	@Override
	public View getView(int postition, View converView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHoler viewHoler = null;
		
		if (converView == null) {
			viewHoler = new ViewHoler();
			converView = mInflater.inflate(R.layout.adapter_list_layout, null);
			viewHoler.tvText = (TextView)converView.findViewById(R.id.adapter_list_layout_tv);
			converView.setTag(viewHoler);
		}else {
			viewHoler = (ViewHoler)converView.getTag();
			
		}
		viewHoler.tvText.setText(list.get(postition % list.size()));
		return converView;
	}
	
	
	static class ViewHoler{
		TextView tvText;
	}
	
}

MainActivity.java

package com.pactera.infinitescrollview;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class MainActivity extends Activity implements OnScrollListener {

	private ListView listView;
	private List list;
	private ListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listView1);
		list = getList();
		adapter = new ListAdapter(this, list);
		listView.setAdapter(adapter);
		listView.setOnScrollListener(this);
		listView.setSelection(list.size());
	}

	public List getList() {
		List list = new ArrayList();
		for (int i = 0; i < 10; i++) {
			list.add(String.valueOf(i));
		}
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		Log.d("ff", ""+ firstVisibleItem);     
		
		if (firstVisibleItem <= 2) {
			listView.setSelection(list.size() + 2);
		}else if (firstVisibleItem + visibleItemCount > adapter.getCount() - 2) {
			listView.setSelection(firstVisibleItem - list.size());
		}

	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
		// TODO Auto-generated method stub

	}

}


重點在上面的onScroll方法 的if語句



activity_main.xml



    
    



adapter_list_layout.xml



    
  
    





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved