package com.gzqol.fjsharedlibrary.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
import android.widget.ListView;
/**
* 解决ScrollView嵌套ExpandableListView,ExpandableListView显示不全的问题
* 注意:ScrollView嵌套ExpandableListView时,ExpandableListView很长超出了屏幕的高度,那么ScrollView会自动滚动到底部,
* 但是我们需要默认在顶部,所以在初始化的时候就让ScrollView获得焦点,滚动条自然就显示到顶部了。
* scrollView.setFocusable(true);
* scrollView.setFocusableInTouchMode(true);
* scrollView.requestFocus();
*/
public class ExpandableListViewForScrollView extends ExpandableListView {
public ExpandableListViewForScrollView(Context context) {
super(context);
}
public ExpandableListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 重写onMeasure方法,重新计算高度,达到使ExpandableListView适应ScrollView的效果
*
* @param widthMeasureSpec 宽度测量规则
* @param heightMeasureSpec 高度测量规则
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Integer.MAX_VALUE:表示int类型能够表示的最大值,值为2的31次方-1
//>>2:右移N位相当于除以2的N的幂
//MeasureSpec.AT_MOST:子布局可以根据自己的大小选择任意大小的模式
int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
}
}
scrollView.setFocusable(true);
scrollView.setFocusableInTouchMode(true);
scrollView.requestFocus();
3.创建adapter处理数据及逻辑
/**
* 初始化listview
*/
private void initListView() {
BorrowQueryBrowseAdapter borrowQueryBrowseAdapter = new BorrowQueryBrowseAdapter(context);
elv.setAdapter(borrowQueryBrowseAdapter);
//展开所有组
for (int i = 0; i < borrowQueryBrowseAdapter.getGroupCount(); i++) {
elv.expandGroup(i);
}
//组点击无效果
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true;
}
});
}
3.1数据适配器adapter的代码
package com.gzqol.fjsharedlibrary.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.gzqol.fjsharedlibrary.R;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
*/
public class BorrowQueryBrowseAdapter extends BaseExpandableListAdapter {
private final Context context;
public BorrowQueryBrowseAdapter(Context context) {
this.context = context;
}
@Override
public int getGroupCount() {
return 5;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_borrow_query_browse_group, null);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
if (groupPosition == 0){
groupViewHolder.viewFirst.setVisibility(View.GONE);
}else {
groupViewHolder.viewFirst.setVisibility(View.VISIBLE);
}
return convertView;
}
static class GroupViewHolder {
@InjectView(R.id.view_first)
View viewFirst;
@InjectView(R.id.tv_jieshudanhao)
TextView tvJieshudanhao;
@InjectView(R.id.tv_zongceshu)
TextView tvZongceshu;
@InjectView(R.id.tv_zongmayang)
TextView tvZongmayang;
GroupViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
//------------------------------------------------------------------------------------------------
@Override
public int getChildrenCount(int groupPosition) {
return 2;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_borrow_query_browse_child, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
if (childPosition == 1){
childViewHolder.viewLast.setVisibility(View.GONE);
}else {
childViewHolder.viewLast.setVisibility(View.VISIBLE);
}
return convertView;
}
static class ChildViewHolder {
@InjectView(R.id.iv_photo)
ImageView ivPhoto;
@InjectView(R.id.tv_title)
TextView tvTitle;
@InjectView(R.id.tv_author)
TextView tvAuthor;
@InjectView(R.id.tv_price)
TextView tvPrice;
@InjectView(R.id.tv_jiechushijian)
TextView tvJiechushijian;
@InjectView(R.id.tv_yinghuanshijian)
TextView tvYinghuanshijian;
@InjectView(R.id.tv_jieshumendian)
TextView tvJieshumendian;
@InjectView(R.id.tv_number)
TextView tvNumber;
@InjectView(R.id.view_last)
View viewLast;
ChildViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
//-----------------------------------------------------------------------------------------------
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public boolean hasStableIds() {
return false;
}
}
详细使用Demo(https://blog.csdn.net/qq941263013/article/details/80901277)
---------------------------------------------------------------------------------------------------------------------------
早计划,早准备,早完成。 欢迎关注!交流!Star!
GitHub:https://github.com/wangyang0313
微信公众号:一个灵活的胖子MrWang
简书:https://www.jianshu.com/u/e5e733d79b96