安卓一年新人,陆陆续续完成一些基础控件,是进步的首要条件.今天带来的是一个侧边栏的实现.
此方法也是从之前的一个listview的setselection方法上得到的启发来完成的.(注意:listview定位到某一个地方就是用的setselection方法).直接上代码!!
三个关键的地方:
1.右侧选项栏的高度平分的item的高度对应的item的个数
2.listview的item与选项栏对应的listview的位置
3.LinearLayout的触摸监听的位置
基本上只要清楚这三个关键点,就很好理解了.用通俗的话再复述一遍:得到listview的A-Z的每一个item的位置position.触摸LinearLayout对应的位置设在listview到某一位置.
以下是完整代码:注释的非常详细,有任何疑问可以相互交流或百度.
public class MainActivity extends Activity {
private ListView listview;//listview
private LinearLayout linear;//linear加载右侧选项栏
private String[] indexStr = { "↑","☆","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#" };//右侧选项栏的字段
private String[] indexStr11 = { "↑","☆","A", "B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P","Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#" };///listview加载的数据标题 与之对应 的选项栏的字段
private int height;// 字体高度
private boolean flag = false;//获取高度的判断条件
private HashMap<String, Integer> selector;// 存放含有索引字母的位置
private List<Map<String, String>>list = new ArrayList<Map<String,String>>();//list集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ID
setViews();
//设置list集合数据和listview
setListViewlist();
//设置右边栏
setHeightLinear();
}
//初始化ID
private void setViews() {
listview = (ListView) findViewById(R.id.listview);//找listview的id
linear = (LinearLayout) findViewById(R.id.linear);//找linear的id
}
//设置list集合数据和listview
private void setListViewlist() {
list = new ArrayList<Map<String,String>>();//新建一个list集合
for (int i = 0; i < indexStr11.length; i++) {//遍历listview加载的数据标题 与之对应 的选项栏的字段
Map<String, String>map = new HashMap<String, String>();//新建一个map集合
map.put("1", indexStr11[i]);//往map集合里面添加字段
list.add(map);//把map集合添加到list集合里面
for (int k = 0; k < 3; k++) {//给对应的字段里添加内容,如A下面有联系人 小白 小白 小白
Map<String, String>map1 = new HashMap<String, String>();//新建一个map集合
map1.put("1", "小白");//往map集合里面添加字段
list.add(map1);//把map集合添加到list集合里面
}
}
String a[] = {"1"};//simpleadapter 加载对应的key
int b[] = {R.id.tv};//simpleadapter 加载对应的布局id
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, a, b);//simpleadapter
listview.setAdapter(adapter);//listview加载adapter
listview.setOnItemClickListener(new OnItemClickListener() {//listview的点击方法
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, list.get(arg2).get("1"),0).show();//对应item上面的值得Toast
}
});
}
//此方法被执行起,用户可以与应用进行交互了
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// 在oncreate里面执行下面的代码没反应,因为oncreate里面得到的getHeight=0
if (!flag) {// 这里为什么要设置个flag进行标记,我这里不先告诉你们,请读者研究,因为这对你们以后的开发有好处 //不判断 会重复获取,用户体验不好
height = linear.getMeasuredHeight() / indexStr.length;
setHeightLinear();
flag = true;
}
}
//设置右边栏
private void setHeightLinear() {
selector = new HashMap<String, Integer>(); //初始化map集合
for (int j = 0; j < indexStr.length; j++) {// 循环字母表,找出newPersons中对应字母的位置
for (int i = 0; i < list.size(); i++) { //循环list集合,找到与indexStr数据相对应的集合位置
if (list.get(i).get("1").equals(indexStr[j])) {
selector.put(indexStr[j], i); //将对应的key 和 list集合对应的数据 添加到select集合里
}
}
}
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, height);//获取layout params
for (int i = 0; i < indexStr.length; i++) {//遍历indexStr数据
final TextView tv = new TextView(this); //new一个textView
tv.setLayoutParams(params); //设置布局
tv.setText(indexStr[i]); //设置文本
tv.setPadding(10, 0, 10, 0); //设置外边距
linear.addView(tv); //添加到linearlayout布局里
linear.setOnTouchListener(new OnTouchListener() { //linearlayout的触摸手势
@Override
public boolean onTouch(View v, MotionEvent event)
{
float y = event.getY();//得到linearlayout的高度
int index = (int) (y / height); //根据高度除以item的高度 获得个数
if (index > -1 && index < indexStr.length) {// 防止越界
String key = indexStr[index]; //获得对应的数组的key值
if (selector.containsKey(key)) { //集合判断是否有此key
int pos = selector.get(key); //有则取到key--value
if (listview.getHeaderViewsCount() > 0) {// 防止ListView有标题栏.
listview.setSelectionFromTop(
pos + listview.getHeaderViewsCount(), 0); //定位到listview的响应位置
} else {
listview.setSelectionFromTop(pos, 0);// 滑动到第一项
}
}
}
return true;
}
});
}
}
}
两个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" >
<ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:divider="@null" android:scrollbars="@null" ></ListView>
<LinearLayout android:id="@+id/linear" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:orientation="vertical" android:gravity="center" ></LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<RelativeLayout android:layout_width="match_parent" android:layout_height="66dp" >
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="18dp" android:text="A" android:textColor="#2a2a2a" android:textSize="16dp" />
<TextView android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_alignParentBottom="true" android:background="#dcdcdc" />
</RelativeLayout>
</RelativeLayout>
个人觉得还是很好理解的.
我是android一年新人,请指教!