大家好,又见面了,我是你们的朋友全栈君。
实现的效果是这个样子的
看上去效果还是不错,不过现在有个刷新问题一直没能解决,刷新的时候里面的adapter进行刷新的时候总是会让里面的listview消失掉,应该是父listview先刷新完后,子listview还未刷新完成,导致测量的高度不对,就会消失,像当前组已关闭这种,现在这个问题还没有想到办法解决的,试过比较多的方法,添加接口让子listview 刷新完成后再去更新父listview,但还是没有作用,也用过ExpandableListView,但是效果达不到这种,所以没办法还是得用这种办法,有大神知道怎么解决刷新父listview时子listview消失的方法,指导下我,比较奇怪的时候刚开始初始化的时候子listview是默认不显示的,当点击父listview去张开子listview,父listview应该会再次刷新,但子listview展开是可以的,然后下一次更新数据源的时候子listview又会自动关闭,我猜应该是在刷新子listview的时候,父listview先更新完成,子listview的高度测量就没有对!
下面是我的源码
listview 嵌套listview都有个高度测量的问题,在更新的时候动态更新子listview就,下面是布局源码
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@ id/grouplinearlayout" android:background="@drawable/item_groupshape" android:orientation="vertical" >
<RelativeLayout android:id="@ id/listopen" android:layout_width="match_parent" android:layout_height="50dp" >
<TextView android:id="@ id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:layout_marginLeft="45dp" android:text="默认组" />
<TextView android:id="@ id/textView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:textSize="15sp" android:text="(2)" />
<ImageView android:id="@ id/imageView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:src="@android:drawable/ic_menu_more" />
</RelativeLayout>
<ImageView android:id="@ id/imageView2" android:layout_width="match_parent" android:layout_height="1dp" android:background="#D8D8D8" />
<!-- 默认关闭 -->
<com.baoyz.swipemenulistview.SwipeMenuListView android:visibility="gone" android:id="@ id/listView1" android:scrollbars="none" android:layout_width="match_parent" android:layout_height="match_parent" >
</com.baoyz.swipemenulistview.SwipeMenuListView>
</LinearLayout>
然后是子listview item的布局代码
代码语言:javascript复制<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="50dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" >
<RelativeLayout android:id="@ id/adddeviserelat" android:layout_width="match_parent" android:layout_height="50dp" >
<LinearLayout android:id="@ id/linearLayout25" android:layout_width="180dp" android:layout_height="match_parent" android:layout_marginLeft="46dp" android:orientation="vertical" >
<TextView android:id="@ id/device_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:text="device name" android:textColor="#000" android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@ id/device_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="addr" android:textColor="#ff6699ff" android:textSize="12dp" />
</LinearLayout>
<ImageView android:id="@ id/singal_imageview" android:layout_width="36dp" android:layout_height="36dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:src="@drawable/car" />
<TextView android:id="@ id/id_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:textColor="#000" />
<ImageView android:id="@ id/locat_imageview" android:layout_width="18dp" android:layout_height="18dp" android:layout_alignParentRight="true" android:layout_marginRight="40dp" android:layout_centerVertical="true" android:src="@drawable/dingwei" />
<ImageView android:id="@ id/path_imageview" android:layout_width="18dp" android:layout_height="18dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/guiji" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<TextView android:layout_height="match_parent" android:layout_width="50dp" android:text="删除" android:background="#ff0000" android:gravity="center"/>
</LinearLayout>
父listview 的adapter代码
代码语言:javascript复制package com.px.pxbatterymanage;
import java.util.ArrayList;
import com.px.pxbatterymanage.DevControlListAdapter.UpdateLister;
import com.px.svr.bean.GroupInfo;
import com.px.svr.data.StringChecker;
import com.px.ui.SwipeMenu;
import com.px.ui.SwipeMenuCreator;
import com.px.ui.SwipeMenuItem;
import com.px.ui.SwipeMenuListView;
import com.px.ui.SwipeMenuListView.OnMenuItemClickListener;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class GroupAdapter extends BaseAdapter {
private ArrayList<GroupInfo> mLeGroup;
private LayoutInflater mInflator;
private Context mContext;
private int mSelected;
public static Handler mHandler;
public GroupAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mLeGroup.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mLeGroup.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/** * 更新数据 * * @param messageList */
public void setGroupinfoData(ArrayList<GroupInfo> blNodeList) {
if (null != blNodeList) {
this.mLeGroup = blNodeList;
} // if (null != messageList)
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, null);
viewHolder = new ViewHolder();
viewHolder.mDevNumberTextView = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.mDevNameTextView = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.listopen = (RelativeLayout) convertView.findViewById(R.id.listopen);
viewHolder.Deviceinfo = (SwipeMenuListView) convertView.findViewById(R.id.listView1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
LinearLayout.LayoutParams listpar = (LinearLayout.LayoutParams)viewHolder.Deviceinfo.getLayoutParams();
final GroupInfo Groupinfo = mLeGroup.get(position);
viewHolder.mDevNameTextView.setText(Groupinfo.getName());
boolean isopen = GlobalVarData.GroupOpenMap.get(Groupinfo.getName()) == null?false:GlobalVarData.GroupOpenMap.get(Groupinfo.getName());
try {
viewHolder.adapter = new DevControlListAdapter(mContext);
viewHolder.adapter.mHandler = mHandler;
viewHolder.adapter.setBlNodeData(Groupinfo.getmGroupDown());
viewHolder.Deviceinfo.setAdapter(viewHolder.adapter);
//重点在这里动态修改子listview的高度,因为我这边子listview的item高度是50dp,这里可以自己修改
listpar.height = dp2px(50*viewHolder.adapter.getCount());
viewHolder.mDevNumberTextView.setText("(" viewHolder.adapter.getCount() ")");
viewHolder.adapter.notifyDataSetChanged();
viewHolder.Deviceinfo.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("error", e.toString());
}
viewHolder.adapter.setOnUpdateLister(new UpdateLister() {
@Override
public void update(int pos) {
if (pos == viewHolder.adapter.getCount() - 1) {
notifyDataSetChanged();
}
}
});
viewHolder.listopen.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (GlobalVarData.GroupOpenMap.get(Groupinfo.getName()) != null && GlobalVarData.GroupOpenMap.get(Groupinfo.getName())) {
viewHolder.Deviceinfo.setVisibility(View.GONE);
GlobalVarData.GroupOpenMap.put(Groupinfo.getName(),false);
}else{
viewHolder.Deviceinfo.setVisibility(View.VISIBLE);
GlobalVarData.gCurCheckdGroupInfo = Groupinfo;
GlobalVarData.GroupOpenMap.put(Groupinfo.getName(),true);
}
}
});
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
SwipeMenuItem item2 = new SwipeMenuItem(mContext);
item2.setBackground(new ColorDrawable(Color.rgb(255, 0, 0)));
item2.setWidth(dp2px(60));
item2.setTitle("删除");
item2.setTitleColor(0xffffffff);
item2.setTitleColor(Color.parseColor("#ffffff"));
item2.setIcon(R.drawable.delete);
menu.addMenuItem(item2);
}
};
viewHolder.Deviceinfo.setMenuCreator(creator);
viewHolder.Deviceinfo.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
showdelete(Groupinfo, position);
break;
}
return false;
}
});
return convertView;
}
public void setListViewHeight(ListView listView,GroupInfo groupinfo) {
//获取listView的adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0,len = Integer.valueOf(groupinfo.getCount()); i < len; i ) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight (listView.getDividerHeight() * (Integer.valueOf(groupinfo.getCount())- 1));
listView.setLayoutParams(params);
}
class ViewHolder {
TextView mDevNameTextView; // 名称
TextView mDevNumberTextView; // 在线数量
SwipeMenuListView Deviceinfo;
RelativeLayout listopen;
LinearLayout GroupLinear;
DevControlListAdapter adapter;
boolean Isopen = false;
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, GlobalVarData.gResources.getDisplayMetrics());
}
private int px2dp(int px) {
return (int) ((px*160)/GlobalVarData.gResources.getDisplayMetrics().density);
}
}
然后自己在根据要求补写个子listview的adapter就可以了
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160595.html原文链接:https://javaforall.cn