说明:最近碰到一个需求,弄一个类似国家或省份列表,样式参照微信联系人
文件列表:
step1:主界面 加载列表数据~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java
step2:右侧列表数据排序~\app\src\com\example\iosdialogdemo\CountryPinyinComparator.java
step3:适配器~\app\src\main\java\com\example\iosdialogdemo\CountryAdapter.java
step4:地区bean类~\app\src\main\java\com\example\iosdialogdemo\Country.java
step5:自定义控件~\app\src\main\java\com\example\iosdialogdemo\SideBar.java
step6:item布局~\app\src\main\res\layout\item_country.xml
step7:主界面布局ui~\app\src\main\res\layout\activity_main.xml
效果图:

step1:~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java
package com.example.iosdialogdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
/**
* 国家代码
* Created by donkor
*/
public class MainActivity extends Activity {
private SideBar sideBar;
private TextView dialog;
private ListView sortListView;
private CountryAdapter countryAdapter;
private Button btnBack;
private ArrayList<Country> countryList;
/**
* 根据拼音来排列ListView里面的数据类
*/
private CountryPinyinComparator pinyinComparator;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
pinyinComparator = new CountryPinyinComparator();
sideBar = (SideBar) findViewById(R.id.sidrbar);
dialog = (TextView) findViewById(R.id.dialog);
sideBar.setTextView(dialog);
sortListView = (ListView) findViewById(R.id.country_lvcountry);
btnBack = (Button) findViewById(R.id.btnBack);
countryList = new ArrayList<>();
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
}
});
/*
public Country(String country, String countryCode, String sortLetters) {
*
* */
countryList.add(new Country("美国","1001","A"));
countryList.add(new Country("宋国","1001","S"));
countryList.add(new Country("赵国","1001","Z"));
countryList.add(new Country("扶余国","1001","F"));
countryList.add(new Country("夜郎国","1001","Y"));
countryList.add(new Country("天启国","1001","T"));
countryList.add(new Country("启明国","1001","Q"));
countryList.add(new Country("俄国","1001","E"));
countryList.add(new Country("英吉利国","1001","Y"));
countryList.add(new Country("法兰西国","1001","F"));
countryList.add(new Country("西班牙国","1001","X"));
countryList.add(new Country("葡萄牙国","1001","P"));
countryList.add(new Country("匈牙利国","1001","X"));
countryList.add(new Country("塞尔维亚国","1001","S"));
countryList.add(new Country("索马里国","1001","S"));
countryList.add(new Country("埃及国","1001","A"));
countryList.add(new Country("苏丹国","1001","S"));
countryList.add(new Country("哈萨克国","1001","H"));
countryList.add(new Country("伊朗国","1001","Y"));
// Log.e("asd", "zone.size(): " + zone.size());
// 根据a-z进行排序源数据
Collections.sort(countryList, pinyinComparator);
// adapter = new SortAdapter(getActivity(), SourceDateList);
countryAdapter = new CountryAdapter(MainActivity.this, countryList);
sortListView.setAdapter(countryAdapter);
// 设置右侧触摸监听
sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {

276

被折叠的 条评论
为什么被折叠?



