这是效果图,刚开始我是使用scrollview嵌套线性布局,布局中嵌套上下两个布局,中间是listview,listview的适配器中还有一个listview和一个线性布局添加显示图片。
这样做就出现滑动过程出现冲突,在滑动过程中出现抖动,因为没找到具体的原因,所以换了一种写法,通过listview添加头尾布局,中间只有一个listview,这样代码也比较简洁,也不会出现滑动冲突。
接下来就是我的代码:include布局是中最上面的一部分和最下边的一部分,一些不重要代码就不贴出来了。
首先是Activity布局:
<?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="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/relative_no_data" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="gone"> <include layout="@layout/activity_sign_up_without_header" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:drawablePadding="@dimen/dp_10" android:drawableTop="@drawable/img_city_no_data" android:text="您所在的城市,还未开业,敬请期待" android:textColor="@color/tc_gray_9" android:textSize="@dimen/dimen_12sp" /> <include layout="@layout/activity_sign_up_without_footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout> <ListView android:id="@+id/lv_sign_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/transparent" android:divider="@color/transparent" android:dividerHeight="@dimen/dp_10" android:visibility="gone" /> </LinearLayout>
其次是Activity的适配器代码:
/** * @author lijianguo * @version 4100 * @desc 未报名班制列表适配器 * @date 2019/4/15 */ public class SignListviewAdapter extends AbstractAdapter<ResidueNumBean.CourseCfgList> { public SignListviewAdapter(Context mContext, List<ResidueNumBean.CourseCfgList> dataList) { super(mContext, dataList); } @Override public int getListViewItemId() { return R.layout.item_sigup_listview; } @Override public void convert(final int position, View convertView, final ResidueNumBean.CourseCfgList dataItem) { ImageView ivSchoolLogo = convertView.findViewById(R.id.iv_school_logo); ImageView ivBackground = convertView.findViewById(R.id.iv_background); TextView tvSchoolName = convertView.findViewById(R.id.tv_school_name); TextView tvShifts = convertView.findViewById(R.id.tv_shifts); TextView tvMoneyBack = convertView.findViewById(R.id.tv_money_back); TextView tvSurplusQuota = convertView.findViewById(R.id.tv_surplus_quota); TextView tvGoSignUp = convertView.findViewById(R.id.tv_go_sign_up); AutoHeightListView listLable = convertView.findViewById(R.id.list_lable); LinearLayout drivingDynamic = convertView.findViewById(R.id.ll_driving_dynamic); //班制背景为1比1比例 ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams(); //屏幕宽减listview左右边距 layoutParams.height = StaticFeild.width - DisplayUtil.dip2px(24); convertView.setLayoutParams(layoutParams); ImageLoader.getInstance().displayImage(dataItem.getImgUrl(), ivBackground, DisplayOption.getBannerOptionno()); ImageLoader.getInstance().displayImage(dataItem.getIcon(), ivSchoolLogo);//驾校Logo tvSchoolName.setText(dataItem.getSchoolName());//驾校名称 tvShifts.setText(dataItem.getCourseName() + dataItem.getCourseType() + " ¥" + dataItem.getPresentPrice() + "");//班制名,班制类型与班制价格 tvMoneyBack.setText("限时返现" + dataItem.getAllMoney() + "元");//返现金额 tvSurplusQuota.setText(dataItem.getResidueNum() + "");//剩余名额 //班制标签适配器 ListLableAdapter adapter = new ListLableAdapter(mContext, dataItem.getLabList()); listLable.setAdapter(adapter); //班制标签图片添加 if (!ListUtil.isNullOrEmpty(dataItem.getLabListCfg())) { drivingDynamic.removeAllViews(); for (int i = 0; i < dataItem.getLabListCfg().size(); i++) { String img = dataItem.getLabListCfg().get(i); if (img != null) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_list_lable_img, null); ImageView imageView = view.findViewById(R.id.iv_saves); ImageLoader.getInstance().displayImage(img, imageView, DisplayOption.getBannerOptions()); drivingDynamic.addView(view); } } } //确认报名 tvGoSignUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (dataItem.getResidueNum() <= 0) { ToastUtil.showToast("当前班制报名人数已满,请报名其他班制"); } else { onOperClickListener.onSignUpClick(dataItem.getCourseId()); } } }); //班制详情 convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (dataItem.getResidueNum() <= 0) { ToastUtil.showToast("当前班制报名人数已满,请报名其他班制"); } else { onOperClickListener.onSignClick(dataItem.getCourseId()); } } }); } private class ListLableAdapter extends AbstractAdapter<String> { ListLableAdapter(Context mContext, List<String> dataList) { super(mContext, dataList); } @Override public int getListViewItemId() { return R.layout.item_list_lable; } @Override public boolean isEnabled(int position) { return false; } public void convert(final int position, View convertView, String dataItem) { TextView tvLabel = convertView.findViewById(R.id.tv_label); tvLabel.setText(dataItem); } } private OnOperClickListener onOperClickListener; public void setOnOperClickListener(OnOperClickListener onOperClickListener) { this.onOperClickListener = onOperClickListener; } public interface OnOperClickListener { void onSignUpClick(int id); void onSignClick(int id); } }
最后重点是Activity代码:
/** * @author lijianguo * @version 3698 * @desc 首页未报名界面 * @date 2018/12/21 */ public class SignUpWithoutActivity extends TitleFragmentActivity { private SignListviewAdapter signdpter;//数据列表条目适配器 private ListView mLvSignList; private RelativeLayout relativeNoData; private CustomPopWindow popWindow; //首页活动配置列表实体 private HomeEnrollBackBean.Data data; @Override protected void initTitileBar() { titleLayout.showAll("九悦百味楼", R.drawable.find_circle_img, new View.OnClickListener() { @Override public void onClick(View v) { showPopWindow(); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addLayout(R.layout.activity_sign_up_without); initView(); reqData(); } /** * 属性id,刷新加载 */ private void initView() { data = (HomeEnrollBackBean.Data) getIntent().getSerializableExtra("signUpEntity"); relativeNoData = findViewById(R.id.relative_no_data); mLvSignList = findViewById(R.id.lv_sign_list); //listview添加头布局 mLvSignList.addHeaderView(LayoutInflater.from(this).inflate(R.layout.activity_sign_up_without_header, null, false)); //listview添加尾布局 mLvSignList.addFooterView(LayoutInflater.from(this).inflate(R.layout.activity_sign_up_without_footer, null, false)); signdpter = new SignListviewAdapter(mContext, null); mLvSignList.setAdapter(signdpter); //去报名跳转 signdpter.setOnOperClickListener(new SignListviewAdapter.OnOperClickListener() { @Override public void onSignUpClick(int id) { //确认报名 reqData2(id); } @Override public void onSignClick(int id) { //班制详情 reqData1(id); } }); } /** * 点击显示弹出层:分享以及帮助说明 */ private void showPopWindow() { View view = LayoutInflater.from(this).inflate(R.layout.pop_sigup_menu, null); view.findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (data != null) ShareUtils.share(mContext, data.getShareTitle(), data.getShareContent(), data.getShareUrl(), data.getShareImg()); popWindow.dissmiss(); } }); view.findViewById(R.id.tv_desc).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (data != null) HtmlActivity.toMe(mContext, data.getContent(), "帮助说明"); popWindow.dissmiss(); } }); popWindow = new CustomPopWindow.PopupWindowBuilder(this) .setView(view) .setFocusable(true) .setOutsideTouchable(true) .enableBackgroundDark(true) //弹出popWindow时,背景是否变暗 .setBgDarkAlpha(0.7f) // 控制亮度 .create(); popWindow.showAsDropDown(titleLayout.getRightIconView(), 0, 0); } /** * 未报名活动班制接口请求 * mid 会员id * activeId 活动id */ private void reqData() { if (data != null) { String path = HostStudentNew.ENROLL_RESIDUE + "?activeId=" + data.getId() + "&mid=" + UserHelper.getUUUID(); DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) { @Override public void onSuccess(String response) { ResidueNumBean numBean = JsonUtils.fromJson(response, ResidueNumBean.class); if (numBean != null) { if (numBean.getResult() == 0) { ResidueNumBean.Data data = numBean.getData(); if (data != null) { List<ResidueNumBean.CourseCfgList> attrList = data.getCourseCfgList(); if (!ListUtil.isNullOrEmpty(attrList)) { relativeNoData.setVisibility(View.GONE); mLvSignList.setVisibility(View.VISIBLE); signdpter.setDataList(attrList); } else { relativeNoData.setVisibility(View.VISIBLE); mLvSignList.setVisibility(View.GONE); } } } else { showToast(numBean.getMessage()); } } } }); req.initLoadingDialog(mContext); VolleyUtil.getInstance().addToRequestQueue(req); } } /** * 网络请求:剩余人数 * 跳转班制详情 * @param id 学制id */ private void reqData1(final int id) { String path = HostStudentNew.RESIDUE_NUM + "?courseId=" + id; DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) { @Override public void onSuccess(String response) { ResidueBean numBean = JsonUtils.fromJson(response, ResidueBean.class); if (numBean != null) { if (numBean.getResult() == 0) { ResidueBean.DataEntity datanum = numBean.getData(); if (datanum != null) { if (datanum.getState() == 0) { Intent mIntent = new Intent(SignUpWithoutActivity.this, EdusysDetailsNewActivity.class); mIntent.putExtra(KeyStudent.EDUSYS_ID, id); startActivity(mIntent); } else if (datanum.getState() == 1) { ToastUtil.showToast("当前班制报名人数已满,请报名其他班制"); } } } else { showToast(numBean.getMessage()); } } } }); req.initLoadingDialog(mContext); VolleyUtil.getInstance().addToRequestQueue(req); } /** * 网络请求:剩余人数 * 跳转到去报名 * @param id 学制id */ private void reqData2(final int id) { String path = HostStudentNew.RESIDUE_NUM + "?courseId=" + id; DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) { @Override public void onSuccess(String response) { ResidueBean numBean = JsonUtils.fromJson(response, ResidueBean.class); if (numBean != null) { if (numBean.getResult() == 0) { ResidueBean.DataEntity datanum = numBean.getData(); if (datanum != null) { if (datanum.getState() == 0) { if (UserHelper.isLogined()) { MobclickAgent.onEvent(mContext, AnalyzeBase.APPLY_GO); Intent mIntent = new Intent(mContext, ApplyConfirmActivity.class); mIntent.putExtra(KeyStudent.EDUSYS_ID, id); startActivity(mIntent); } else { startActivity(LoginAcitvity.class); } } else if (datanum.getState() == 1) { ToastUtil.showToast("当前班制报名人数已满,请报名其他班制"); } } } else { showToast(numBean.getMessage()); } } } }); req.initLoadingDialog(mContext); VolleyUtil.getInstance().addToRequestQueue(req); } } 因为代码是写到项目中,所以贴出来重点部分,其他的有一部分没贴出来,感兴趣的可以找我,流程大概就是这样。