scrollview 多层嵌套的问题 、冲突解、刷新数据后自动滚动导致卡顿、
外层设置scrollview即可
内部listview需要使用自定义listview,且listview和他的父级目录需要设置成为match——parent,否则会产生滑动卡顿
1、最外层scrollview布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_health_ask_user" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white" tools:context="www.kang.com.activity.HealthProblemDetailsActivity"> <include layout="@layout/title_bar"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:id="@+id/health_problem_scroll_view" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdge="none" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_f6f6f8" android:padding="10dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="26sp" android:textStyle="bold" android:textColor="@color/color_289644" android:id="@+id/problem_details_first_item_ask" android:text="@string/health_ask_user_ask"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <www.kang.com.view.CircleImageView android:layout_width="@dimen/dp45" android:layout_height="@dimen/dp45" android:id="@+id/problem_details_first_item_head" android:layout_marginTop="5dp" style="@style/CircleImageView_normal" android:src="@mipmap/personal_loading_head"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="16sp" android:gravity="center" android:textColor="@color/color_666666" android:layout_toRightOf="@+id/problem_details_first_item_head" android:layout_alignTop="@+id/problem_details_first_item_head" android:layout_alignBottom="@+id/problem_details_first_item_head" android:id="@+id/problem_details_first_item_name" android:text="@string/health_ask_user_name"/> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@color/color_333333" android:layout_marginTop="5dp" android:text="@string/health_ask_user_problem_body" android:id="@+id/problem_details_first_item_body"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:textColor="@color/color_999999" android:text="@string/health_ask_user_problem_time" android:id="@+id/problem_details_first_item_time"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_alignParentRight="true" android:id="@+id/health_community_item_problem_see_layout" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_look"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:textSize="13sp" android:textColor="@color/color_999999" android:id="@+id/problem_details_first_item_see_count" android:text="@string/health_community_item_problem_see_count"/> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white"> <www.kang.com.view.MyListView android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/transparent" android:dividerHeight="1dp" android:fadingEdge="none" android:listSelector="#00000000" android:overScrollMode="never" android:id="@+id/health_ask_first_reply_listview"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:padding="5dp" android:textSize="14sp" android:textColor="@color/black" android:text="@string/health_ask_user_ask_more" android:dividerHeight="1dp" android:divider="@color/transparent" android:visibility="gone" android:id="@+id/problem_details_first_item_ask_more"/> <www.kang.com.view.MyListView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:layout_marginTop="5dp" android:dividerHeight="1dp" android:divider="@color/transparent" android:fadingEdge="none" android:listSelector="#00000000" android:overScrollMode="never" android:id="@+id/health_ask_more_listview"/> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="#EEEEEE" android:visibility="gone" android:id="@+id/health_ask_user_problem_edittext_layout" android:orientation="horizontal" android:padding="4dp"> <EditText android:id="@+id/health_ask_user_problem_edittext" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="4dp" android:layout_weight="5" android:paddingLeft="4dp" android:textSize="14sp" android:background="@color/white" /> <TextView android:id="@+id/health_ask_user_problem_send" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="4dp" android:layout_weight="1" android:background="@drawable/btn_login_btn_background" android:gravity="center" android:padding="6dp" android:text="发送" android:textColor="#FFFFFF" android:textSize="10sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="@dimen/dp45" android:gravity="center" android:textColor="@color/white" android:textSize="14sp" android:background="@color/theme_green" android:text="@string/health_ask_user_problem_reply" android:id="@+id/health_ask_user_problem_reply"/> </LinearLayout> </LinearLayout>
2、内部listview布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/regimen_disease_layout_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:fadingEdge="none" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="5dp" android:layout_marginTop="15dp" android:layout_marginLeft="10dp" android:background="@color/color_f6f6f8" android:id="@+id/problem_details_first_item_ask_more"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <www.kang.com.view.CircleImageView android:layout_width="@dimen/dp45" android:layout_height="@dimen/dp45" android:id="@+id/problem_details_first_item_head" android:layout_marginTop="5dp" style="@style/CircleImageView" android:src="@mipmap/personal_loading_head"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="14sp" android:singleLine="true" android:ellipsize="end" android:gravity="center" android:layout_alignTop="@+id/problem_details_first_item_head" android:layout_alignBottom="@+id/problem_details_first_item_head" android:layout_toRightOf="@+id/problem_details_first_item_head" android:textColor="@color/color_666666" android:id="@+id/problem_details_first_item_name" android:text="@string/health_ask_user_name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:layout_marginLeft="10dp" android:textColor="@color/color_333333" android:layout_below="@+id/problem_details_first_item_name" android:layout_toRightOf="@+id/problem_details_first_item_head" android:text="@string/health_ask_user_problem_body" android:id="@+id/problem_details_first_item_body"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="13sp" android:textColor="@color/color_999999" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/problem_details_first_item_head" android:layout_below="@+id/problem_details_first_item_body" android:text="@string/health_ask_user_problem_time" android:id="@+id/problem_details_first_item_time"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="15dp" android:layout_toRightOf="@+id/problem_details_first_item_head" android:layout_below="@+id/problem_details_first_item_time"> <View android:layout_width="match_parent" android:layout_height="2dp" android:layerType="software" android:layout_centerVertical="true" android:background="@drawable/problem_details_dash_line"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/padding_style" android:textSize="11sp" android:layout_alignParentRight="true" android:textColor="@color/white" android:id="@+id/problem_details_first_item_ask_click" android:text="@string/health_ask_user_problem_click" android:background="@drawable/btn_login_btn_background"/> </RelativeLayout> </RelativeLayout> <www.kang.com.view.MyListView android:id="@+id/problem_details_first_item_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:numColumns="1" android:paddingLeft="8dp" android:layout_marginLeft="@dimen/dp45" android:footerDividersEnabled="false" android:divider="@color/transparent" android:fadingEdge="none" android:overScrollMode="never" android:dividerHeight="1dp" android:listSelector="@android:color/transparent" android:scrollbars="none"/> </LinearLayout>
3、二级内部item布局
<?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" android:orientation="vertical" android:background="@color/white" android:id="@+id/problem_details_second_item_layout"> <www.kang.com.view.CircleImageView android:layout_width="@dimen/dp45" android:layout_height="@dimen/dp45" android:id="@+id/problem_details_second_item_head" style="@style/CircleImageView_normal" android:src="@mipmap/personal_loading_head"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:id="@+id/problem_details_second_item_title_layout" android:layout_toRightOf="@+id/problem_details_second_item_head" android:layout_alignTop="@+id/problem_details_second_item_head" android:layout_alignBottom="@+id/problem_details_second_item_head"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="14sp" android:textColor="@color/color_333333" android:id="@+id/problem_details_second_item_name" android:text="@string/health_ask_user_name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="13sp" android:layout_alignParentBottom="true" android:paddingLeft="3dp" android:paddingRight="5dp" android:singleLine="true" android:ellipsize="end" android:maxEms="5" android:text="@string/health_ask_user_problem_reply_technical" android:background="@mipmap/ic_personal_detail_level" android:id="@+id/problem_details_second_item_technical"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:visibility="gone" android:src="@mipmap/ic_personal_detaisl_adopt" android:id="@+id/problem_details_second_item_adopt"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="15dp" android:layout_below="@+id/problem_details_second_item_title_layout" android:layout_toRightOf="@+id/problem_details_second_item_head" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:textColor="@color/color_666666" android:text="@string/health_ask_user_problem_reply_body" android:id="@+id/problem_details_second_item_body"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="@color/color_999999" android:textSize="13sp" android:text="@string/health_ask_user_problem_time" android:id="@+id/problem_details_second_item_time"/> </LinearLayout> </RelativeLayout>
4、一级适配器布局
package www.kang.com.adapter; import android.content.Context; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import www.kang.com.R; import www.kang.com.application.GlobalApplication; import www.kang.com.bean.ProblemDetailsBean; import www.kang.com.utils.CommonUtil; import www.kang.com.view.CircleImageView; import www.kang.com.view.MyGridView; import www.kang.com.view.MyListView; /** * @author kangkang * @date 2018-06-23 下午7:44:27 * @version 健康社区 问题详情 一层适配器 * @description 问题详情 */ public class ProblemDetailsChildAdapter extends BaseGroupAdapter<ProblemDetailsBean.DataEntity.ChildsEntity> { private Context mContext; private ProblemDetailsChildSecondAdapter.onChildSecondProblemItemListener childProblemItemListener; private ProblemDetailsChildAdapter.onChildProblemItemListener detailsClickListener; private List<ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity> childAnswerDatas = new ArrayList<>(); private ScrollView mScrollView; public ProblemDetailsChildAdapter(Context context, ProblemDetailsChildAdapter.onChildProblemItemListener detailsClickListener,ScrollView mScrollView) { super(context); this.mContext = context; this.detailsClickListener = detailsClickListener; this.mScrollView = mScrollView; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder ; if (null == convertView) { holder = new ViewHolder(); convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.problem_details_item_child_first_view, parent, false); holder.nameView = (TextView) convertView.findViewById(R.id.problem_details_first_item_name); holder.headView = (CircleImageView) convertView.findViewById(R.id.problem_details_first_item_head); holder.problemBodyView = (TextView) convertView.findViewById(R.id.problem_details_first_item_body); holder.timeView = (TextView) convertView.findViewById(R.id.problem_details_first_item_time); holder.reply = (TextView) convertView.findViewById(R.id.problem_details_first_item_ask_click); holder.listview = (MyListView) convertView.findViewById(R.id.problem_details_first_item_listview); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } final ProblemDetailsBean.DataEntity.ChildsEntity bean = getItem(position); childProblemItemListener = new ProblemDetailsChildSecondAdapter.onChildSecondProblemItemListener() { @Override public void onProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity bean) { detailsClickListener.onChildProblemItemClickListener(bean); } }; if (bean.getChildsAnswer()!=null){ mScrollView.requestDisallowInterceptTouchEvent(true); ProblemDetailsChildSecondAdapter childAnswerViewAdapter = new ProblemDetailsChildSecondAdapter(mContext, childProblemItemListener); childAnswerDatas = bean.getChildsAnswer(); childAnswerViewAdapter.setGroup(childAnswerDatas); holder.listview.setAdapter(childAnswerViewAdapter); }else { holder.listview.setVisibility(View.GONE); } holder.listview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { mScrollView.requestDisallowInterceptTouchEvent(true); return false; } }); if (!TextUtils.isEmpty(bean.getNickname())){ holder.nameView.setText(bean.getNickname()); } if (!TextUtils.isEmpty(bean.getContent())){ holder.problemBodyView.setText(bean.getContent()); } if (!TextUtils.isEmpty(bean.getPub_time())){ holder.timeView.setText(CommonUtil.getTimestampString(Long.parseLong(bean.getPub_time()))); } if (!TextUtils.isEmpty(bean.getAvatar())){ GlobalApplication.getApp().getImageLoader().displayImage(bean.getAvatar(),holder.headView, CommonUtil.getOptions(R.mipmap.default_head)); } holder.reply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!TextUtils.isEmpty(bean.getQuestion_id())){ detailsClickListener.onProblemItemClickListener(bean); } } }); return convertView; } class ViewHolder { /**用户名*/ TextView nameView; /**头像*/ CircleImageView headView; /**问题内容*/ TextView problemBodyView; /**时间*/ TextView timeView; /**点我回复*/ TextView reply; /**回复列表*/ MyListView listview; } public interface onChildProblemItemListener{ public void onProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity bean); public void onChildProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity bean); } }
5、二级适配器布局
package www.kang.com.adapter; import android.content.Context; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import www.kang.com.R; import www.kang.com.application.GlobalApplication; import www.kang.com.bean.ProblemDetailsBean; import www.kang.com.utils.CommonUtil; /** * @author kangkang * @date 2018-06-23 下午7:44:27 * @version 健康社区 问题详情 一层适配器 * @description 问题详情 */ public class ProblemDetailsChildSecondAdapter extends BaseGroupAdapter<ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity> { private Context mContext; private onChildSecondProblemItemListener listener; public ProblemDetailsChildSecondAdapter(Context context, onChildSecondProblemItemListener listener) { super(context); this.mContext = context; this.listener = listener; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder ; if (null == convertView) { holder = new ViewHolder(); convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.problem_details_item_child_second_view, parent, false); holder.layout = (RelativeLayout) convertView.findViewById(R.id.problem_details_second_item_layout); holder.iv = (ImageView) convertView.findViewById(R.id.problem_details_second_item_head); holder.name = (TextView) convertView.findViewById(R.id.problem_details_second_item_name); holder.technical = (TextView) convertView.findViewById(R.id.problem_details_second_item_technical); holder.adopt = (ImageView) convertView.findViewById(R.id.problem_details_second_item_adopt); holder.time = (TextView) convertView.findViewById(R.id.problem_details_second_item_time); holder.body = (TextView) convertView.findViewById(R.id.problem_details_second_item_body); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } final ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity bean = getItem(position); if (!TextUtils.isEmpty(bean.getAvatar())){ GlobalApplication.getApp().getImageLoader().displayImage(bean.getAvatar(),holder.iv, CommonUtil.getOptions(R.mipmap.default_head)); } if (!TextUtils.isEmpty(bean.getName())){ holder.name.setText(bean.getName()); } if (!TextUtils.isEmpty(bean.getTechnical())){ holder.technical.setText(bean.getTechnical()); } if (!TextUtils.isEmpty(bean.getAdopt())&&bean.getAdopt().equals("1")){///1为采纳,0为不采纳 holder.adopt.setVisibility(View.VISIBLE); }else { holder.adopt.setVisibility(View.GONE); } if (!TextUtils.isEmpty(bean.getPub_time())){ holder.time.setText(CommonUtil.getTimestampString(Long.parseLong(bean.getPub_time()))); } if (!TextUtils.isEmpty(bean.getContent())){ holder.body.setText(bean.getContent()); } holder.layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!TextUtils.isEmpty(bean.getAnswer_id())){ listener.onProblemItemClickListener(bean); } } }); return convertView; } class ViewHolder { RelativeLayout layout; /**头像*/ ImageView iv; /**用户名称*/ TextView name; TextView technical; /**是否采纳*/ ImageView adopt; /**时间*/ TextView time; /**内容*/ TextView body; } public interface onChildSecondProblemItemListener{ public void onProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity bean); } }
6、activity代码
package www.kang.com.activity; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.IBinder; import android.os.Bundle; import android.os.Message; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; import www.kang.com.R; import www.kang.com.adapter.ProblemDetailsFirstAnswerAdapter; import www.kang.com.adapter.ProblemDetailsChildAdapter; import www.kang.com.application.BasicActivity; import www.kang.com.application.GlobalApplication; import www.kang.com.bean.ProblemDetailsBean; import www.kang.com.http.OpDefine; import www.kang.com.http.base.DataResult; import www.kang.com.http.base.Pair; import www.kang.com.http.base.ResponseBase; import www.kang.com.task.GetProblemDetailsTask; import www.kang.com.task.GetReplyProblemTask; import www.kang.com.utils.CommonUtil; import www.kang.com.utils.DensityUtils; import www.kang.com.utils.SPUtil; import www.kang.com.utils.ToastUtils; import www.kang.com.view.CircleImageView; import www.kang.com.view.MyScrollView; /** * @author kangkang * @date 2018-06-23 下午7:44:27 * @version 健康社区 问题列表 问题详情 * @description 问题详情 */ public class HealthProblemDetailsActivity extends BasicActivity implements View.OnClickListener{ public static final String INTENT_QUESTION_ID_FLAG="intent_question_id_flag"; public static final int LOGIN = 1035; // 登录 @Bind(R.id.health_problem_scroll_view) ScrollView scrollView; @Bind(R.id.problem_details_first_item_name) TextView askNameView; @Bind(R.id.problem_details_first_item_see_count) TextView askProblemSeeCountView; @Bind(R.id.problem_details_first_item_head) CircleImageView askHeadView; @Bind(R.id.problem_details_first_item_body) TextView askProblemBodyView; @Bind(R.id.problem_details_first_item_time) TextView askProblemTimeView; @Bind(R.id.health_ask_user_problem_reply) TextView replyView; @Bind(R.id.health_ask_user_problem_edittext) EditText editText; @Bind(R.id.health_ask_user_problem_send) TextView sendView; @Bind(R.id.health_ask_user_problem_edittext_layout) LinearLayout replyLayout; @Bind(R.id.health_ask_first_reply_listview) ListView firstReplyListView; @Bind(R.id.health_ask_more_listview) ListView listView; /***进入时传递到问题id*/ private String question_id_first=""; /***选择的问题id*/ private String question_id_choose=""; private List<ProblemDetailsBean.DataEntity.ChildsEntity> problemList = new ArrayList<>(); private ProblemDetailsChildAdapter problemDetailsdapter; private ProblemDetailsBean problemDetailsBean = new ProblemDetailsBean(); private GetProblemDetailsTask getProblemDetailsTask; private ProblemDetailsChildAdapter.onChildProblemItemListener problemItemListener; private ProblemDetailsFirstAnswerAdapter firstReplyAdapter; private List<ProblemDetailsBean.DataEntity.FirstAnswerEntity> firstReplyList = new ArrayList<>(); private GetReplyProblemTask replyProblemTask; private Handler mHandler; private boolean isFirst = true; private LocalBroadcastManager mLocalBroadcastManager; private boolean isShowTips = false; private DataResult dataResult =new DataResult() { @SuppressWarnings("unchecked") @Override public void onResult(int op, Object result, Exception e) throws Exception { // TODO Auto-generated method stub if (result !=null) { Pair<ResponseBase, Object> basepair=(Pair<ResponseBase, Object>) result; ResponseBase baseData=(ResponseBase)basepair.first; if (CommonUtil.judgeServerError(baseData,HealthProblemDetailsActivity.this)) { switch (op) { case OpDefine.OP_GET_HEALTH_COMMUNITY_QUESTIONS_DETAILS_FLAG:{ //获取问题详情 ProblemDetailsBean bean = (ProblemDetailsBean) basepair.second; if (bean!=null&&bean.getData()!=null){ problemDetailsBean = bean; scrollView.requestDisallowInterceptTouchEvent(true); if (problemDetailsBean.getData().getFirstQuestion()!=null){ initFirstQuestionData(problemDetailsBean.getData().getFirstQuestion()); } if (problemDetailsBean.getData().getFirstAnswer()!=null){ firstReplyList = bean.getData().getFirstAnswer(); firstReplyAdapter.setGroup(firstReplyList); } if (problemDetailsBean.getData().getChilds()!=null){ problemList = problemDetailsBean.getData().getChilds(); problemDetailsdapter.setGroup(problemList); } // scrollView.smoothScrollTo(0,(int)scrollView.getY()); // scrollView.fullScroll(ScrollView.FOCUS_FORWARD); // if (!isFirst){//首次进来不滚动到底部 // mHandler.post(new Runnable() { // @Override // public void run() { // //设置ScrollView滚动到顶部 //// scrollView.fullScroll(ScrollView.FOCUS_UP); // // //设置ScrollView滚动到顶部 // scrollView.fullScroll(ScrollView.FOCUS_DOWN); // } // }); // } if (isShowTips){ isShowTips = false; if (!TextUtils.isEmpty(bean.getMsg())){ ToastUtils.TextToast(HealthProblemDetailsActivity.this,"恭喜您已成功回复了哟",0); } } } break; } case OpDefine.OP_GET_HEALTH_COMMUNITY_QUESTIONS_DETAILS_REPLY_QUESTION_FLAG:{ //回答问题 isFirst = false; ResponseBase bean = (ResponseBase) basepair.second; editText.setText(""); mHandler.postDelayed(mRunnable,200); isShowTips = true; startGetProblemDetailsTask(); break; } default: break; } } }else { ToastUtils.ToastNewError(mContext); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent().getExtras()!=null&&getIntent().getExtras().containsKey(INTENT_QUESTION_ID_FLAG) &&!TextUtils.isEmpty(getIntent().getStringExtra(INTENT_QUESTION_ID_FLAG))){ question_id_first = getIntent().getStringExtra(INTENT_QUESTION_ID_FLAG); }else { finish(); return; } initServices(); mHandler = new Handler(); setContentView(R.layout.activity_health_problem_view); ButterKnife.bind(this); setTitleBarView(true,"问题详情",null); problemItemListener = new ProblemDetailsChildAdapter.onChildProblemItemListener() { @Override public void onProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity bean) { if (!TextUtils.isEmpty(bean.getQuestion_id())){ question_id_choose = bean.getQuestion_id(); replyView.performClick(); } } @Override public void onChildProblemItemClickListener(ProblemDetailsBean.DataEntity.ChildsEntity.ListEntity bean) { } }; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,DensityUtils.dp2px(HealthProblemDetailsActivity.this,45)); replyView.setLayoutParams(params); startGetProblemDetailsTask(); replyView.setOnClickListener(this); sendView.setOnClickListener(this); problemDetailsdapter = new ProblemDetailsChildAdapter(HealthProblemDetailsActivity.this,problemItemListener,scrollView); problemDetailsdapter.setGroup(problemList); listView.setAdapter(problemDetailsdapter); firstReplyAdapter = new ProblemDetailsFirstAnswerAdapter(HealthProblemDetailsActivity.this); firstReplyAdapter.setGroup(firstReplyList); firstReplyListView.setAdapter(firstReplyAdapter); } private void initServices() { mLocalBroadcastManager=LocalBroadcastManager.getInstance(HealthProblemDetailsActivity.this); } private void initFirstQuestionData(ProblemDetailsBean.DataEntity.FirstQuestionEntity bean){ if (!TextUtils.isEmpty(bean.getPub_time())){ askProblemTimeView.setText(CommonUtil.getTimestampString(Long.parseLong(bean.getPub_time()))); } if (!TextUtils.isEmpty(bean.getAvatar())){ GlobalApplication.getApp().getImageLoader().displayImage(bean.getAvatar(),askHeadView, CommonUtil.getOptions(R.mipmap.default_head)); } if (!TextUtils.isEmpty(bean.getContent())){ askProblemBodyView.setText(bean.getContent()); } if (!TextUtils.isEmpty(bean.getVisits())){ askProblemSeeCountView.setText(bean.getVisits()); } if (!TextUtils.isEmpty(bean.getNickname())){ askNameView.setText(bean.getNickname()); } } private void startGetProblemDetailsTask(){ if (!CommonUtil.cancelTask(getProblemDetailsTask)){ ToastUtils.TextToast(HealthProblemDetailsActivity.this,"正在执行上一次请求,请稍后",0); } getProblemDetailsTask = new GetProblemDetailsTask(dataResult,HealthProblemDetailsActivity.this,question_id_first); getProblemDetailsTask.executeN(); } private void startGetReplyProblemTask(){ if (!CommonUtil.cancelTask(replyProblemTask)){ ToastUtils.TextToast(HealthProblemDetailsActivity.this,"正在执行上一次请求,请稍后",0); } String sid = SPUtil.getInstance(HealthProblemDetailsActivity.this).getLoginInfo().getData().getSid(); String content = editText.getEditableText().toString(); String question_id = ""; if (!TextUtils.isEmpty(question_id_choose)){ question_id = question_id_choose; }else { question_id = question_id_first; } replyProblemTask = new GetReplyProblemTask(dataResult,HealthProblemDetailsActivity.this,question_id,sid,content); replyProblemTask.executeN(); } @Override public void onClick(View v) { super.onClick(v); switch (v.getId()){ case R.id.health_ask_user_problem_reply:{ //弹起弹框 boolean islogin = SPUtil.getInstance(HealthProblemDetailsActivity.this).getIsLogin(); if (islogin){ replyView.setVisibility(View.GONE); replyLayout.setVisibility(View.VISIBLE); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText,0); // showSoftInputFromWindow(HealthProblemDetailsActivity.this,editText); }else { startActivityForResult(new Intent(HealthProblemDetailsActivity.this, EMLoginActivity.class), LOGIN); } break; } case R.id.health_ask_user_problem_send:{ //答复问题 if (!TextUtils.isEmpty(editText.getEditableText().toString())){ startGetReplyProblemTask(); }else { ToastUtils.TextToast(HealthProblemDetailsActivity.this,"请输入内容",0); } break; } default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case LOGIN://登录成功后 if (resultCode == RESULT_OK) { //登录成功 mLocalBroadcastManager.sendBroadcast(new Intent(GlobalApplication.BROAD_PERSON_LOGIN_BROCASTRECEIVER)); } break; default: break; } } // /** // * EditText获取焦点并显示软键盘 // */ // public static void showSoftInputFromWindow(Activity activity, EditText editText) { // editText.setFocusable(true); // editText.setFocusableInTouchMode(true); // editText.requestFocus(); // activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); // } //重写触摸,触摸到EditView之外的地方使其失去焦点 @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点) View v = getCurrentFocus(); if (isShouldHideInput(v, ev)) { hideSoftInput(v.getWindowToken()); mHandler.postDelayed(mRunnable,200); } } return super.dispatchTouchEvent(ev); } private Runnable mRunnable = new Runnable() { @Override public void run() { replyView.setVisibility(View.VISIBLE); replyLayout.setVisibility(View.GONE); } }; /** * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时没必要隐藏 * * @param v * @param event * @return */ private boolean isShouldHideInput(View v, MotionEvent event) { if (v != null && (v instanceof EditText)) { int[] l = {0, 0}; v.getLocationInWindow(l); int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // 点击EditText的事件,忽略它。 return false; } else { return true; } } // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点 return false; } /** * 多种隐藏软件盘方法的其中一种 * * @param token */ private void hideSoftInput(IBinder token) { if (token != null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(token,InputMethodManager.HIDE_NOT_ALWAYS); } } @Override protected void onDestroy() { super.onDestroy(); ButterKnife.unbind(this); } }
7、自定义工具类MyListview
package www.kang.com.view; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。 @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
以上就是设置scrollview嵌套listview,listview内部再嵌套listview大大致思路
二、解决scrollview在listview一级或二级内容变化时自动滑动的方法
在一级listview的适配器中对二级listview进行焦点的声明即可