1:dataBinding配置
首先在gradle配置databinding ,
android {
...
dataBinding {
enabled = true
}
...
}
2:dataBinding使用
activity的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="presenter"
type="com.zj.quickindex.MainActivity"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expanded_menu"
></ExpandableListView>
<android.support.v7.widget.RecyclerView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_gravity="center_vertical|right"
android:id="@+id/recycle_list"
></android.support.v7.widget.RecyclerView>
</FrameLayout>
</layout>
然后是调用。
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil. setContentView(this,R.layout.activity_main);
binding.setPresenter(this);
}
其中ActivityMainBinding 命名是布局 activity_main 去除下划线后+binding;
最后调用ActivityMainBinding 的时候,可先make project 。
3:字母索引
字母索引这块,根据布局很明显我这里用的是recycleListview;
adapter这块就不写了,十分简单,
主要是recycleListView 点击以及滑动时的处理;这块没有中间部分选中字母的展示,所以不做赘述。
binding.recycleList.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//判断adapter是否为空
if (coinBarAdapter.getDataCount()>0){
//每个字母item的高度
float height=binding.recycleList.getHeight()/coinBarAdapter.getDataCount();
float y=0;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
y=Math.abs(event.getY());
int downPos= (int) (y/height);//计算下标
//expanlistview跳转对应的位置
binding.expandedMenu.setSelectedGroup(downPos);
break;
case MotionEvent.ACTION_MOVE:
y=Math.abs(event.getY());
int pos= (int) (y/height);
binding.expandedMenu.setSelectedGroup(pos);
// showToast(coinBarAdapter.getDataList().get(pos));
break;
case MotionEvent.ACTION_UP:
break;
}
}
return false;
}
});
代码如上,主要是ontouchListener的处理,首先计算出每个item的高度,记录手指按下的位置,算出对应的pos下标;
手指移动时的处理相同;
4:联系人列表
这块直接用了expanlistview,
adapter代码直接贴出
package com.zj.quickindex;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.List;
/**
* create by zj on 2018/11/5
*/
public class ExpandCoinAdapter extends BaseExpandableListAdapter {
private List<String> parentList;
private List<List<PairInfoBean>> childList;
private Context context;
public ExpandCoinAdapter(Context context, List<String> parentList, List<List<PairInfoBean>> childList){
this.context=context;
this.parentList=parentList;
this.childList=childList;
}
@Override
public int getGroupCount() {
return parentList==null?0:parentList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList==null?0:childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return parentList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(R.layout.item_coin_sel_parent,null);
TextView textView=convertView.findViewById(R.id.tv_parent);
textView.setText(parentList.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(R.layout.item_coin_sel_child,null);
TextView textView=convertView.findViewById(R.id.tv_coin_name);
textView.setText(childList.get(groupPosition).get(childPosition).pairName);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public static class ViewHolderParent{
}
}
view中没有使用viewholder,可优化
另外两个小问题
//去除expanlistview父布局的箭头
binding.expandedListview.setGroupIndicator(null);
//默认展开所有
for (int i=0;i<expandCoinAdapter.getGroupCount();i++){
binding.expandedListView.expandGroup(i);
}