结果展示:
关键分析:根据屏幕比例算出每一页放多少个Item,然后把每一页的Item push进工作数组中,然后传给adapter,利用adapter的notifyDataSetChanged更新即可;
注意:getCount返回的是工作数组的size,这样的话最后一页才不会出错(往往是抛出空指针)
关键代码:
MainActivity.java
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
initView();
InputStream input = getResources().openRawResource(R.drawable.android);
BitmapDrawable android = new BitmapDrawable(input);
Bitmap bitmap = android.getBitmap();
for(int i=0;i<30;i++){
Picture tmp = new Picture();
tmp.setImage(bitmap);
tmp.setTitle("Name "+String.valueOf(i));
pictureInfo.add(tmp);
}
cl = new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnLeft:
leftView();
break;
case R.id.btnRight:
rightView();
break;
}
}
};
btnLeft.setOnClickListener(cl);
btnRight.setOnClickListener(cl);
checkButton();
initWorkArray();
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(MainActivity.this, "pic" + (position+1), Toast.LENGTH_SHORT).show();
}
});
}
public void initWorkArray(){
workArray = new ArrayList<Picture>();
for(int i=0;i<VIEW_COUNT;i++){
Picture tmp = new Picture();
tmp.setImage(pictureInfo.get(i).getImage());
tmp.setTitle(pictureInfo.get(i).getTitle());
workArray.add(tmp);
}
for(int i=0;i<VIEW_COUNT;i++){
System.out.print("1-----"+workArray.get(i).getTitle());
}
adapter = new PictureAdapter( this,workArray);
gridView.setAdapter(adapter);
}
public void undateWorkArray(){
workArray = new ArrayList<Picture>();
int begin=(index-1)*VIEW_COUNT;
int end=index*VIEW_COUNT;
if(pictureInfo.size()<index * VIEW_COUNT)
end=pictureInfo.size();
for(int i=begin;i<end;i++){
Picture tmp = new Picture();
tmp.setImage(pictureInfo.get(i).getImage());
tmp.setTitle(pictureInfo.get(i).getTitle());
workArray.add(tmp);
}
adapter.updateMusicInfoBean(workArray);
}
public void initView() {
btnLeft = (Button) findViewById(R.id.btnLeft);
btnRight = (Button) findViewById(R.id.btnRight);
}
public void leftView() {
index--;
undateWorkArray();
adapter.notifyDataSetChanged();
checkButton();
}
public void rightView() {
index++;
undateWorkArray();
adapter.notifyDataSetChanged();
checkButton();
}
public void checkButton() {
if(index<=1){
btnLeft.setEnabled(false);
System.out.println("----btnLeft.setEnabled(false)---");
}
else{
btnLeft.setEnabled(true);
System.out.println("----btnLeft.setEnabled(true)---");
}
if(pictureInfo.size()<index * VIEW_COUNT){
System.out.println("----btnRight.setEnabled(false)---");
btnRight.setEnabled(false);
}
else{
System.out.println("----btnRight.setEnabled(true)---");
btnRight.setEnabled(true);
}
}
adapter的设计
public class PictureAdapter extends BaseAdapter{
private final static String TAG="MainActivity";
private Context context;//上下文
private ArrayList<Picture> pictureInfo;//音乐信息集合
private LayoutInflater inflater;
public static int printTimes=0;
public PictureAdapter(Context context,ArrayList<Picture> pictureInfo)
{
super();
this.context = context;
this.pictureInfo = pictureInfo;
//实例化LayoutInflater
inflater = LayoutInflater.from(context);
}
public void updateMusicInfoBean(ArrayList<Picture> pictureInfo){
this.pictureInfo = pictureInfo;
}
@Override
public int getCount()
{
if (null != pictureInfo)
{
return pictureInfo.size();
//return 8;
} else
{
return 0;
}
}
@Override
public Object getItem(int position)
{
return pictureInfo.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
printTimes++;
System.out.print("the result:----------------------------------------------------------");
System.out.println(printTimes);
Log.i(TAG, "the getview is called at "+String.valueOf(printTimes)+" time ");
ViewHolder viewHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.grid_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(pictureInfo.get(position).getTitle());
viewHolder.image.setImageBitmap(pictureInfo.get(position).getImage());
return convertView;
}
}
picture类
public class Picture
{
private String title;
private Bitmap image;
public Picture()
{
super();
}
public Picture(String title, Bitmap image)
{
super();
this.title = title;
this.image = image;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public Bitmap getImage()
{
return image;
}
public void setImage(Bitmap image)
{
this.image = image;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnLeft"
android:text="lastPage" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnRight"
android:text="nextPage" />
</LinearLayout>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="70dp"
android:descendantFocusability="blocksDescendants"
android:gravity="center"
android:horizontalSpacing="10dp"
android:listSelector="@drawable/list_selector"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:padding="4dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
/>
</LinearLayout>