package run.yang.com.baseadapterdemo;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.LinkedList;
import java.util.List;
publicclassMainActivityextendsAppCompatActivityimplementsAdapterView.OnItemClickListener {private List<Animal> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal;
private LinearLayout ly_content;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/private GoogleApiClient client;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
list_animal = (ListView) findViewById(R.id.list_animal);
//动态加载顶部View和底部Viewfinal LayoutInflater inflater = LayoutInflater.from(this);
View headView = inflater.inflate(R.layout.view_header, null, false);
View footView = inflater.inflate(R.layout.view_footer, null, false);
mData = new LinkedList<Animal>();
mData.add(new Animal("狗说", "你是狗么?", R.drawable.ic_icon_dog));
mData.add(new Animal("牛说", "你是牛么?", R.drawable.ic_icon_cow));
mData.add(new Animal("鸭说", "你是鸭么?", R.drawable.ic_icon_duck));
mData.add(new Animal("鱼说", "你是鱼么?", R.drawable.ic_icon_fish));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mData.add(new Animal("马说", "你是马么?", R.drawable.ic_icon_horse));
mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
//添加表头和表尾需要写在setAdapter方法调用之前!!!
list_animal.addHeaderView(headView);
list_animal.addFooterView(footView);
list_animal.setAdapter(mAdapter);
list_animal.setOnItemClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, "你点击了第" + position + "项", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type."Main Page", // TODO: Define a title for the content shown.// TODO: If you have web page content that matches this app activity's content,// make sure this auto-generated web page URL is correct.// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://run.yang.com.baseadapterdemo/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@OverridepublicvoidonStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type."Main Page", // TODO: Define a title for the content shown.// TODO: If you have web page content that matches this app activity's content,// make sure this auto-generated web page URL is correct.// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://run.yang.com.baseadapterdemo/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
Android自定义BaseAdapter看看小动物们在说啥ListView自带上下滑动这里也是根据大神学习的就不装逼了布局文件:activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml