Android 循环ListView

Android  循环ListView    之前做个iOS下的,实现起来比较复杂,后来在网上找了一个例子,其实现起来比较容易,思路很好,这个我做个整理


这个类就是个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<String> list;
	private LayoutInflater mInflater;
	
	public ListAdapter (Context context,List<String> 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<String> 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<String> getList() {
		List<String> list = new ArrayList<String>();
		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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="26dp" >
    </ListView>

</RelativeLayout>

adapter_list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
  <TextView 
        android:id="@+id/adapter_list_layout_tv"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:textSize="40dp"
        android:gravity="center"
        android:text="11111"
        />
    
</LinearLayout>




Android 循环ListView,布布扣,bubuko.com

Android 循环ListView

上一篇:iOS ARC下循环引用的问题


下一篇:【DataGuard】10GR2 DataGuard的几个redo log相关问题