当我们在找工作时,肯定要注册简历。简历在创建的时候会有工作地点的选择,如下图:
当往上拉时,”热门城市”会取代”当前城市”并且不消失。当点击”上海”时,还有更精确地地点可以选择
我在github上找到了相关的demo(https://github.com/beworker/pinned-section-listview),并且对其进行了实现。
首先定义一个实体类
代码语言:javascript复制/**
* Listview对应的每个条目
*/
public class Item {
public final static int ITEM = 0;
public static final int SECTION = 1;
/** listitem类型。SECTION大标题,ITEM小标题 */
public final int type;
/** listitem文本内容 */
public final String text;
/** listitem所在的SECTION */
public int sectionPosition;
/** listitem在整个Listview中的位置 */
public int listPosition;
/** 相对于某个section的位置 */
public int sectionlistPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
@Override
public String toString() {
return text;
}
}
接下来是自定义的Listview
PinnedSectionListView.java 里面有英文注释,理解起来并不困难
然后是PinnedSectionListActivity,也是重点 首先定义了一个适配器
代码语言:javascript复制/**
* 适配器
*/
class SimpleAdapter extends ArrayAdapter<Item> implements
PinnedSectionListAdapter {
private final int[] COLORS = new int[] { R.color.green_light,
R.color.orange_light, R.color.blue_light, R.color.red_light };
public SimpleAdapter(Context context, int resource,
int textViewResourceId) {
super(context, resource, textViewResourceId);
generateDataset();
}
/**
* 生成数据集
*/
public void generateDataset() {
SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(dst, null);
Cursor cursor = db1.query("provinces", null, null, null, null,
null, null);
while (cursor.moveToNext()) {
String pro = cursor.getString(cursor.getColumnIndex("name"));
proset.add(pro);
int ID = cursor.getInt(cursor.getColumnIndex("_id")) - 1;
// -1是因为db文件里面数据的原因
proIDset.add(ID);
}
cursor.close();
db1.close();
int sectionPosition = 0, listPosition = 0;
// 省份 sectionPosition = 0
Item section = new Item(Item.SECTION, "省份");
section.sectionPosition = sectionPosition;
section.listPosition = listPosition ;
add(section);
final int itemsNumber = proset.size();
for (int j = 0; j < itemsNumber; j ) {
Item item = new Item(Item.ITEM, proset.get(j));
item.sectionPosition = sectionPosition;
item.listPosition = listPosition ;
item.sectionlistPosition = j;
add(item);
}
sectionPosition ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView,
parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" position);
Item item = getItem(position);
if (item.type == Item.SECTION) {
view.setBackgroundColor(parent.getResources().getColor(
COLORS[item.sectionPosition % COLORS.length]));
}
return view;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return getItem(position).type;
}
@Override
public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
这个适配器的功能就是从db中加载数据(generateDataset),并且通过getView显示出来。 然后是响应的onListItemClick 当点击的item属于省份,下面还有城市的话(例如江苏省),那么就加载一个listview显示城市。通过设置setVisibility来隐藏和显示
代码语言:javascript复制 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item.type == Item.ITEM) {
Toast.makeText(this, "Item " position ": " item.text,
Toast.LENGTH_SHORT).show();
if (item.sectionPosition == 0) {// 属于"省份"的ITEM
cityset.clear();
SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(dst,
null);
// 注意这个province_id究竟应该等于哪个值
Cursor cursor = db1.query("citys", null, "province_id="
item.sectionlistPosition, null, null, null, null);
while (cursor.moveToNext()) {
String city = cursor.getString(cursor
.getColumnIndex("name"));
if (!city.contains(".")) {
cityset.add(city);
}
}
cursor.close();
db1.close();
if (cityset.size() == 1) {// 直辖市
return;
}
// 非直辖市再加载一个listview显示
getListView().setVisibility(View.GONE);
adapter = new ArrayAdapter<String>(
PinnedSectionListActivity.this,
android.R.layout.simple_list_item_1, cityset);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
PinnedSectionListActivity.this,
"Item " position ": "
adapter.getItem(position),
Toast.LENGTH_SHORT).show();
}
});
Log.i("cityset", cityset.toString());
}
} else {
Toast.makeText(this, "Item " position, Toast.LENGTH_SHORT).show();
}
}
最后通过重写onBackPressed来进行两个listview的切换
代码语言:javascript复制 @Override
public void onBackPressed() {
if (listView.getVisibility() == View.VISIBLE
&& getListView().getVisibility() == View.GONE) {
listView.setVisibility(View.GONE);
getListView().setVisibility(View.VISIBLE);
} else if (listView.getVisibility() == View.GONE
&& getListView().getVisibility() == View.VISIBLE) {
finish();
}
}
我的代码地址:http://download.csdn.net/detail/lxj1137800599/9558528