1414
1515package apijson .demo .client .activity_fragment ;
1616
17+ import java .util .ArrayList ;
18+ import java .util .HashMap ;
1719import java .util .List ;
20+ import java .util .Map ;
1821
1922import zuo .biao .apijson .JSON ;
2023import zuo .biao .apijson .JSONResponse ;
3538import android .content .Context ;
3639import android .content .Intent ;
3740import android .os .Bundle ;
41+ import android .os .Handler ;
3842import android .view .View ;
3943import android .view .View .OnClickListener ;
4044import android .widget .AdapterView ;
@@ -176,7 +180,7 @@ private void setHead(final MomentItem momentItem) {
176180 @ Override
177181 public void run () {
178182 momentView .bindView (momentItem );
179-
183+
180184 if (showKeyboard ) {//在etMomentInput被绑定前调用showInput崩溃 //{
181185 showKeyboard = false ;
182186
@@ -210,6 +214,13 @@ public void refreshAdapter() {
210214 * @param toCommentItem
211215 */
212216 public void showInput (CommentItem toCommentItem_ ) {
217+ showInput (toCommentItem_ , -1 );
218+ }
219+ /**显示输入评论
220+ * @param toCommentItem
221+ * @param position
222+ */
223+ public void showInput (CommentItem toCommentItem_ , final int position ) {
213224 this .toCommentItem = toCommentItem_ ;
214225 final long toCommentId = toCommentItem == null ? 0 : toCommentItem .getComment ().getId ();
215226 runUiThread (new Runnable () {
@@ -222,6 +233,18 @@ public void run() {
222233 etMomentInput .setHint ("回复:" + StringUtil .getTrimedString (toCommentItem .getUser ().getName ()));
223234 }
224235 EditTextManager .showKeyboard (context , etMomentInput );//, toGetWindowTokenView);
236+
237+ if (position >= 0 ) {
238+ new Handler ().postDelayed (new Runnable () {
239+
240+ @ Override
241+ public void run () {
242+ if (isAlive ()) {
243+ lvBaseList .setSelection (position + lvBaseList .getHeaderViewsCount ());
244+ }
245+ }
246+ }, 500 );
247+ }
225248 }
226249 });
227250 }
@@ -335,7 +358,52 @@ public void getListAsync(final int page) {
335358
336359 @ Override
337360 public List <CommentItem > parseArray (String json ) {
338- return JSON .parseArray (new JSONResponse (json ).getArray (CommentItem .class .getSimpleName ()), CommentItem .class );
361+ List <CommentItem > list = JSON .parseArray (new JSONResponse (json ).getArray (
362+ CommentItem .class .getSimpleName ()), CommentItem .class );
363+ if (list != null && list .isEmpty () == false ) {
364+ //parent和child分类
365+ Map <Long , CommentItem > parentMap = new HashMap <>();//added
366+ List <CommentItem > allChildList = new ArrayList <>();
367+ long id ;
368+ long toId ;
369+ for (CommentItem item : list ) {
370+ id = item == null ? 0 : item .getId ();
371+ if (id <= 0 ) {
372+ continue ;
373+ }
374+
375+ toId = item .getComment ().getParentId ();
376+ if (toId <= 0 ) {//parent
377+ parentMap .put (id , item );
378+ } else {//child
379+ allChildList .add (item );
380+ }
381+ }
382+
383+ //child放到parent的childList中
384+ CommentItem parent ;
385+ List <CommentItem > childList ;
386+ for (CommentItem child : allChildList ) {
387+ toId = child == null ? 0 : child .getComment ().getParentId ();
388+ parent = toId <= 0 ? null : parentMap .get (toId );
389+ if (parent != null ) {
390+ childList = parent .getChildList ();
391+ if (childList == null ) {
392+ childList = new ArrayList <>();
393+ }
394+ child .setToUser (parent .getUser ());
395+ childList .add (child );
396+
397+ parent .setChildList (childList );
398+ parentMap .put (toId , parent );
399+ }
400+ }
401+
402+ //转为list
403+ list = new ArrayList <>(parentMap .values ());
404+ }
405+
406+ return list ;
339407 }
340408
341409 @ Override
@@ -437,7 +505,7 @@ private void onComemntItemClick(int position,boolean isLong) {
437505 }
438506 new AlertDialog (context , null , "删除这条评论?" , true , DIALOG_DELETE_COMMENT , MomentActivity .this ).show ();
439507 } else {
440- showInput (item );
508+ showInput (item , position );
441509 }
442510 }
443511
0 commit comments