自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android實(shí)現(xiàn)社交應(yīng)用中的評(píng)論和回復(fù)功能

移動(dòng)開發(fā) Android
在Android的日常開發(fā)中,評(píng)論與回復(fù)功能是我們經(jīng)常遇到的需求之一,其中評(píng)論與回復(fù)列表的展示一般在功能模塊中占比較大。對(duì)于需求改動(dòng)和迭代較頻繁的公司來說,如何快速開發(fā)一個(gè)二級(jí)界面來適應(yīng)我們的功能需求無疑優(yōu)先級(jí)更高一些。

在Android的日常開發(fā)中,評(píng)論與回復(fù)功能是我們經(jīng)常遇到的需求之一,其中評(píng)論與回復(fù)列表的展示一般在功能模塊中占比較大。對(duì)于需求改動(dòng)和迭代較頻繁的公司來說,如何快速開發(fā)一個(gè)二級(jí)界面來適應(yīng)我們的功能需求無疑優(yōu)先級(jí)更高一些。首先我們來看看其他社交類app的評(píng)論與回復(fù)列表如何展示的:

Android實(shí)現(xiàn)社交應(yīng)用中的評(píng)論和回復(fù)功能

Android實(shí)現(xiàn)社交應(yīng)用中的評(píng)論和回復(fù)功能

Twitter不用說了,全球知名社交平臺(tái),上億用戶量,他們的評(píng)論回復(fù)都只展示一級(jí)數(shù)據(jù)(評(píng)論數(shù)據(jù)),其他更多內(nèi)容(回復(fù)內(nèi)容),是需要頁面跳轉(zhuǎn)去查看,知乎也類似。第一張圖是我們?cè)O(shè)計(jì)給我找的,他說要按照這個(gè)風(fēng)格來,盡量將評(píng)論和回復(fù)內(nèi)容在一個(gè)頁面展示。好吧,沒辦法,畢竟我們做前端的,UI要看設(shè)計(jì)臉色,數(shù)據(jù)要看后臺(tái)臉色??吹皆O(shè)計(jì)圖,我們腦??隙ǖ谝粫r(shí)間聯(lián)想一下解決方案:用recyclerview?listview?不對(duì),分析一下它的層級(jí)發(fā)現(xiàn),評(píng)論是一個(gè)列表,里面的回復(fù)又是一個(gè)列表,難道用recyclerview或者listview的嵌套?抱著不確定的態(tài)度,立馬去網(wǎng)上查一下,果不其然,搜到的實(shí)現(xiàn)方式大多都是用嵌套實(shí)現(xiàn)的,來公司之前,其中一個(gè)項(xiàng)目里的評(píng)論回復(fù)功能就是用的嵌套listview,雖然處理了滑動(dòng)沖突問題,但效果不佳,而且時(shí)??D,所以,這里我肯定要換個(gè)思路。

網(wǎng)上還有說用自定義view實(shí)現(xiàn)的,但我發(fā)現(xiàn)大多沒有處理view的復(fù)用,而且開發(fā)成本大,暫時(shí)不予考慮。那怎么辦?無意中看到expandable這個(gè)關(guān)鍵詞,我突然想到谷歌很早之前出過一個(gè)擴(kuò)展列表的控件 - ExpandableListView,但聽說比較老,存在一些問題。算了,試試再說,順便熟悉一下以前基礎(chǔ)控件的用法。

先來看一下最終的效果圖吧:

Android實(shí)現(xiàn)社交應(yīng)用中的評(píng)論和回復(fù)功能

這只是一個(gè)簡(jiǎn)單的效果圖,你可以在此基礎(chǔ)上來完善它。好了,廢話不多說,下面讓我們來看看效果具體如何實(shí)現(xiàn)的吧。大家應(yīng)該不難看出來,頁面整體采用了CoordinatorLayout來實(shí)現(xiàn)詳情頁的頂部視差效。同時(shí),這里我采用ExpandableListView來實(shí)現(xiàn)多級(jí)列表,然后再解決它們的嵌套滑動(dòng)問題。OK,我們先從ExpandableListView開始動(dòng)手。

ExpandableListView

官方對(duì)于ExpandableListView給出這樣的解釋:

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

簡(jiǎn)單來說,ExpandableListView是一個(gè)用于垂直方向滾動(dòng)的二級(jí)列表視圖,ExpandableListView與listview不同之處在于,它可以實(shí)現(xiàn)二級(jí)分組,并通過ExpandableListAdapter來綁定數(shù)據(jù)和視圖。下面我們來一起實(shí)現(xiàn)上圖的效果。

布局中定義

首先,我們需要在xml的布局文件中聲明ExpandableListView:

 

  1. <ExpandableListView 
  2.     android:id="@+id/detail_page_lv_comment" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:divider="@null" 
  6.     android:layout_marginBottom="64dp" 
  7.     android:listSelector="@android:color/transparent" 
  8.     android:scrollbars="none"/> 

這里需要說明兩個(gè)問題:

  1. ExpandableListView默認(rèn)為它的item加上了點(diǎn)擊效果,由于item里面還包含了childItem,所以,點(diǎn)擊后,整個(gè)item里面的內(nèi)容都會(huì)有點(diǎn)擊效果。我們可以取消其點(diǎn)擊特效,避免其影響用戶體驗(yàn),只需要設(shè)置如上代碼中的listSelector即可。
  2. ExpandableListView具有默認(rèn)的分割線,可以通過divider屬性將其隱藏。

設(shè)置Adapter

正如使用listView那樣,我們需要為ExpandableListView設(shè)置一個(gè)適配器Adapter,為其綁定數(shù)據(jù)和視圖。ExpandableListView的adapter需要繼承自ExpandableListAdapter,具體代碼如下:

 

  1. public class CommentExpandAdapter extends BaseExpandableListAdapter { 
  2.     private static final String TAG = "CommentExpandAdapter"
  3.     private List<CommentDetailBean> commentBeanList; 
  4.     private Context context; 
  5.     public CommentExpandAdapter(Context context, List<CommentDetailBean> commentBeanList)               { 
  6.         this.context = context; 
  7.         this.commentBeanList = commentBeanList; 
  8.     } 
  9.     @Override 
  10.     public int getGroupCount() { 
  11.         return commentBeanList.size(); 
  12.     } 
  13.     @Override 
  14.     public int getChildrenCount(int i) { 
  15.         if(commentBeanList.get(i).getReplyList() == null){ 
  16.             return 0; 
  17.         }else { 
  18.             return commentBeanList.get(i).getReplyList().size()>0 ? commentBeanList.get(i).getReplyList().size():0; 
  19.         } 
  20.     } 
  21.     @Override 
  22.     public Object getGroup(int i) { 
  23.         return commentBeanList.get(i); 
  24.     } 
  25.     @Override 
  26.     public Object getChild(int i, int i1) { 
  27.         return commentBeanList.get(i).getReplyList().get(i1); 
  28.     } 
  29.     @Override 
  30.     public long getGroupId(int groupPosition) { 
  31.         return groupPosition; 
  32.     } 
  33.     @Override 
  34.     public long getChildId(int groupPosition, int childPosition) { 
  35.         return getCombinedChildId(groupPosition, childPosition); 
  36.     } 
  37.     @Override 
  38.     public boolean hasStableIds() { 
  39.         return true
  40.     } 
  41.     boolean isLike = false
  42.     @Override 
  43.     public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) { 
  44.         final GroupHolder groupHolder; 
  45.         if(convertView == null){ 
  46.             convertView = LayoutInflater.from(context).inflate(R.layout.comment_item_layout, viewGroup, false); 
  47.             groupHolder = new GroupHolder(convertView); 
  48.             convertView.setTag(groupHolder); 
  49.         }else { 
  50.             groupHolder = (GroupHolder) convertView.getTag(); 
  51.         } 
  52.         Glide.with(context).load(R.drawable.user_other) 
  53.                 .diskCacheStrategy(DiskCacheStrategy.RESULT) 
  54.                 .error(R.mipmap.ic_launcher) 
  55.                 .centerCrop() 
  56.                 .into(groupHolder.logo); 
  57.         groupHolder.tv_name.setText(commentBeanList.get(groupPosition).getNickName()); 
  58.         groupHolder.tv_time.setText(commentBeanList.get(groupPosition).getCreateDate()); 
  59.         groupHolder.tv_content.setText(commentBeanList.get(groupPosition).getContent()); 
  60.         groupHolder.iv_like.setOnClickListener(new View.OnClickListener() { 
  61.             @Override 
  62.             public void onClick(View view) { 
  63.                 if(isLike){ 
  64.                     isLike = false
  65.                     groupHolder.iv_like.setColorFilter(Color.parseColor("#aaaaaa")); 
  66.                 }else { 
  67.                     isLike = true
  68.                     groupHolder.iv_like.setColorFilter(Color.parseColor("#FF5C5C")); 
  69.                 } 
  70.             } 
  71.         }); 
  72.         return convertView; 
  73.     } 
  74.     @Override 
  75.     public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) { 
  76.         final ChildHolder childHolder; 
  77.         if(convertView == null){ 
  78.             convertView = LayoutInflater.from(context).inflate(R.layout.comment_reply_item_layout,viewGroup, false); 
  79.             childHolder = new ChildHolder(convertView); 
  80.             convertView.setTag(childHolder); 
  81.         } 
  82.         else { 
  83.             childHolder = (ChildHolder) convertView.getTag(); 
  84.         } 
  85.         String replyUser = commentBeanList.get(groupPosition).getReplyList().get(childPosition).getNickName(); 
  86.         if(!TextUtils.isEmpty(replyUser)){ 
  87.             childHolder.tv_name.setText(replyUser + ":"); 
  88.         } 
  89.         childHolder.tv_content.setText(commentBeanList.get(groupPosition).getReplyList().get(childPosition).getContent()); 
  90.         return convertView; 
  91.     } 
  92.     @Override 
  93.     public boolean isChildSelectable(int i, int i1) { 
  94.         return true
  95.     } 
  96.     private class GroupHolder{ 
  97.         private CircleImageView logo; 
  98.         private TextView tv_name, tv_content, tv_time; 
  99.         private ImageView iv_like; 
  100.         public GroupHolder(View view) { 
  101.             logo =  view.findViewById(R.id.comment_item_logo); 
  102.             tv_content = view.findViewById(R.id.comment_item_content); 
  103.             tv_name = view.findViewById(R.id.comment_item_userName); 
  104.             tv_time = view.findViewById(R.id.comment_item_time); 
  105.             iv_like = view.findViewById(R.id.comment_item_like); 
  106.         } 
  107.     } 
  108.     private class ChildHolder{ 
  109.         private TextView tv_name, tv_content; 
  110.         public ChildHolder(View view) { 
  111.             tv_name = (TextView) view.findViewById(R.id.reply_item_user); 
  112.             tv_content = (TextView) view.findViewById(R.id.reply_item_content); 
  113.         } 
  114.     } 

一般情況下,我們自定義自己的ExpandableListAdapter后,需要實(shí)現(xiàn)以下幾個(gè)方法:

  • 構(gòu)造方法,這個(gè)應(yīng)該無需多說了,一般用來初始化數(shù)據(jù)等操作。
  • getGroupCount,返回group分組的數(shù)量,在當(dāng)前需求中指代評(píng)論的數(shù)量。
  • getChildrenCount,返回所在group中child的數(shù)量,這里指代當(dāng)前評(píng)論對(duì)應(yīng)的回復(fù)數(shù)目。
  • getGroup,返回group的實(shí)際數(shù)據(jù),這里指的是當(dāng)前評(píng)論數(shù)據(jù)。
  • getChild,返回group中某個(gè)child的實(shí)際數(shù)據(jù),這里指的是當(dāng)前評(píng)論的某個(gè)回復(fù)數(shù)據(jù)。
  • getGroupId,返回分組的id,一般將當(dāng)前group的位置傳給它。
  • getChildId,返回分組中某個(gè)child的id,一般也將child當(dāng)前位置傳給它,不過為了避免重復(fù),可以使用getCombinedChildId(groupPosition, childPosition);來獲取id并返回。
  • hasStableIds,表示分組和子選項(xiàng)是否持有穩(wěn)定的id,這里返回true即可。
  • isChildSelectable,表示分組中的child是否可以選中,這里返回true。
  • getGroupView,即返回group的視圖,一般在這里進(jìn)行一些數(shù)據(jù)和視圖綁定的工作,一般為了復(fù)用和高效,可以自定義ViewHolder,用法與listview一樣,這里就不多說了。
  • getChildView,返回分組中child子項(xiàng)的視圖,比較容易理解,第一個(gè)參數(shù)是當(dāng)前group所在的位置,第二個(gè)參數(shù)是當(dāng)前child所在位置。

這里的數(shù)據(jù)是我自己做的模擬數(shù)據(jù),不過應(yīng)該算是較為通用的格式了,大體格式如下:

Android實(shí)現(xiàn)社交應(yīng)用中的評(píng)論和回復(fù)功能

一般情況下,我們后臺(tái)會(huì)通過接口返回給我們一部分?jǐn)?shù)據(jù),如果想要查看更多評(píng)論,需要跳轉(zhuǎn)到“更多頁面”去查看,這里為了方便,我們只考慮加載部分?jǐn)?shù)據(jù)。

Activity中使用

接下來,我們就需要在activity中顯示評(píng)論和回復(fù)的二級(jí)列表了:

 

  1. private ExpandableListView expandableListView; 
  2. private CommentExpandAdapter adapter; 
  3. private CommentBean commentBean; 
  4. private List<CommentDetailBean> commentsList; 
  5. ... 
  6. @Override 
  7.     protected void onCreate(Bundle savedInstanceState) { 
  8.         super.onCreate(savedInstanceState); 
  9.         setContentView(R.layout.activity_main); 
  10.         initView(); 
  11.     } 
  12.     private void initView() { 
  13.         expandableListView = findViewById(R.id.detail_page_lv_comment); 
  14.         initExpandableListView(commentsList); 
  15.     } 
  16.     /** 
  17.      * 初始化評(píng)論和回復(fù)列表 
  18.      */ 
  19.     private void initExpandableListView(final List<CommentDetailBean> commentList){ 
  20.         expandableListView.setGroupIndicator(null); 
  21.         //默認(rèn)展開所有回復(fù) 
  22.         adapter = new CommentExpandAdapter(this, commentList); 
  23.         expandableListView.setAdapter(adapter); 
  24.         for(int i = 0; i<commentList.size(); i++){ 
  25.             expandableListView.expandGroup(i); 
  26.         } 
  27.         expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
  28.             @Override 
  29.             public boolean onGroupClick(ExpandableListView expandableListView, View viewint groupPosition, long l) { 
  30.                 boolean isExpanded = expandableListView.isGroupExpanded(groupPosition); 
  31.                 Log.e(TAG, "onGroupClick: 當(dāng)前的評(píng)論id>>>"+commentList.get(groupPosition).getId()); 
  32. //                if(isExpanded){ 
  33. //                    expandableListView.collapseGroup(groupPosition); 
  34. //                }else { 
  35. //                    expandableListView.expandGroup(groupPosition, true); 
  36. //                } 
  37.                 return true
  38.             } 
  39.         }); 
  40.         expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
  41.             @Override 
  42.             public boolean onChildClick(ExpandableListView expandableListView, View viewint groupPosition, int childPosition, long l) { 
  43.                 Toast.makeText(MainActivity.this,"點(diǎn)擊了回復(fù)",Toast.LENGTH_SHORT).show(); 
  44.                 return false
  45.             } 
  46.         }); 
  47.         expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
  48.             @Override 
  49.             public void onGroupExpand(int groupPosition) { 
  50.                 //toast("展開第"+groupPosition+"個(gè)分組"); 
  51.             } 
  52.         }); 
  53.     } 
  54.     /** 
  55.      * func:生成測(cè)試數(shù)據(jù) 
  56.      * @return 評(píng)論數(shù)據(jù) 
  57.      */ 
  58.     private List<CommentDetailBean> generateTestData(){ 
  59.         Gson gson = new Gson(); 
  60.         commentBean = gson.fromJson(testJson, CommentBean.class); 
  61.         List<CommentDetailBean> commentList = commentBean.getData().getList(); 
  62.         return commentList; 
  63.     } 

就以上代碼作一下簡(jiǎn)單說明:

ExpandableListView在默認(rèn)情況下會(huì)為我們自帶分組的icon(),當(dāng)前需求下,我們根本不需要展示,可以通過expandableListView.setGroupIndicator(null)來隱藏。

一般情況下,我們可能需要默認(rèn)展開所有的分組,我就可以通過循環(huán)來調(diào)用expandableListView.expandGroup(i);方法。

ExpandableListView為我們提供了group和child的點(diǎn)擊事件,分別通過setOnGroupClickListener和setOnChildClickListener來設(shè)置。值得注意的是,group的點(diǎn)擊事件里如果我們返回的是false,那么我們點(diǎn)擊group就會(huì)自動(dòng)展開,但我這里碰到一個(gè)問題,當(dāng)我返回false時(shí),第一條評(píng)論數(shù)據(jù)會(huì)多出一條。通過百度查找方法,雖然很多類似問題,但終究沒有解決,最后我返回了ture,并通過以下代碼手動(dòng)展開和收縮就可以了:

 

  1. if(isExpanded){ 
  2.     expandableListView.collapseGroup(groupPosition); 
  3. }else { 
  4.     expandableListView.expandGroup(groupPosition, true); 

此外,我們還可以通過setOnGroupExpandListener和setOnGroupCollapseListener來監(jiān)聽ExpandableListView的分組展開和收縮的狀態(tài)。

評(píng)論和回復(fù)功能

為了模擬整個(gè)評(píng)論和回復(fù)功能,我們還需要手動(dòng)插入收據(jù)并刷新數(shù)據(jù)列表。這里我就簡(jiǎn)單做一下模擬,請(qǐng)忽略一些UI上的細(xì)節(jié)。

插入評(píng)論數(shù)據(jù)

插入評(píng)論數(shù)據(jù)比較簡(jiǎn)單,只需要在list中插入一條數(shù)據(jù)并刷新即可:

 

  1. String commentContent = commentText.getText().toString().trim(); 
  2. if(!TextUtils.isEmpty(commentContent)){ 
  3.     //commentOnWork(commentContent); 
  4.     dialog.dismiss(); 
  5.     CommentDetailBean detailBean = new CommentDetailBean("小明", commentContent,"剛剛"); 
  6.     adapter.addTheCommentData(detailBean); 
  7.     Toast.makeText(MainActivity.this,"評(píng)論成功",Toast.LENGTH_SHORT).show(); 
  8. }else { 
  9.     Toast.makeText(MainActivity.this,"評(píng)論內(nèi)容不能為空",Toast.LENGTH_SHORT).show(); 

adapter中的addTheCommentData方法如下:

 

  1. public void addTheCommentData(CommentDetailBean commentDetailBean){ 
  2.     if(commentDetailBean!=null){ 
  3.         commentBeanList.add(commentDetailBean); 
  4.         notifyDataSetChanged(); 
  5.     }else { 
  6.         throw new IllegalArgumentException("評(píng)論數(shù)據(jù)為空!"); 
  7.     } 

代碼比較容易理解,就不多做說明了。

插入回復(fù)數(shù)據(jù)

首先,我們需要實(shí)現(xiàn)點(diǎn)擊某一條評(píng)論,然后@ta,那么我們需要在group的點(diǎn)擊事件里彈起回復(fù)框:

 

  1. expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
  2.         @Override 
  3.         public boolean onGroupClick( 
  4.             ExpandableListView expandableListView, View viewint groupPosition, long l) { 
  5.             showReplyDialog(groupPosition); 
  6.             return true
  7.         } 
  8.     }); 
  9. ...... 
  10. /** 
  11.  * func:彈出回復(fù)框 
  12.  */ 
  13. private void showReplyDialog(final int position){ 
  14.     dialog = new BottomSheetDialog(this); 
  15.     View commentView = LayoutInflater.from(this).inflate(R.layout.comment_dialog_layout,null); 
  16.     final EditText commentText = (EditText) commentView.findViewById(R.id.dialog_comment_et); 
  17.     final Button bt_comment = (Button) commentView.findViewById(R.id.dialog_comment_bt); 
  18.     commentText.setHint("回復(fù) " + commentsList.get(position).getNickName() + " 的評(píng)論:"); 
  19.     dialog.setContentView(commentView); 
  20.     bt_comment.setOnClickListener(new View.OnClickListener() { 
  21.         @Override 
  22.         public void onClick(View view) { 
  23.             String replyContent = commentText.getText().toString().trim(); 
  24.             if(!TextUtils.isEmpty(replyContent)){ 
  25.                 dialog.dismiss(); 
  26.                 ReplyDetailBean detailBean = new ReplyDetailBean("小紅",replyContent); 
  27.                 adapter.addTheReplyData(detailBean, position); 
  28.                 Toast.makeText(MainActivity.this,"回復(fù)成功",Toast.LENGTH_SHORT).show(); 
  29.             }else { 
  30.                 Toast.makeText(MainActivity.this,"回復(fù)內(nèi)容不能為空",Toast.LENGTH_SHORT).show(); 
  31.             } 
  32.         } 
  33.     }); 
  34.     commentText.addTextChangedListener(new TextWatcher() { 
  35.         @Override 
  36.         public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
  37.         } 
  38.         @Override 
  39.         public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
  40.             if(!TextUtils.isEmpty(charSequence) && charSequence.length()>2){ 
  41.                 bt_comment.setBackgroundColor(Color.parseColor("#FFB568")); 
  42.             }else { 
  43.                 bt_comment.setBackgroundColor(Color.parseColor("#D8D8D8")); 
  44.             } 
  45.         } 
  46.         @Override 
  47.         public void afterTextChanged(Editable editable) { 
  48.         } 
  49.     }); 
  50.     dialog.show(); 

插入回復(fù)的數(shù)據(jù)與上面插入評(píng)論類似,這里貼一下adapter中的代碼:

 

  1. /** 
  2.  * by moos on 2018/04/20 
  3.  * func:回復(fù)成功后插入一條數(shù)據(jù) 
  4.  * @param replyDetailBean 新的回復(fù)數(shù)據(jù) 
  5.  */ 
  6. public void addTheReplyData(ReplyDetailBean replyDetailBean, int groupPosition){ 
  7.     if(replyDetailBean!=null){ 
  8.         Log.e(TAG, "addTheReplyData: >>>>該刷新回復(fù)列表了:"+replyDetailBean.toString() ); 
  9.         if(commentBeanList.get(groupPosition).getReplyList() != null ){ 
  10.             commentBeanList.get(groupPosition).getReplyList().add(replyDetailBean); 
  11.         }else { 
  12.             List<ReplyDetailBean> replyList = new ArrayList<>(); 
  13.             replyList.add(replyDetailBean); 
  14.             commentBeanList.get(groupPosition).setReplyList(replyList); 
  15.         } 
  16.         notifyDataSetChanged(); 
  17.     }else { 
  18.         throw new IllegalArgumentException("回復(fù)數(shù)據(jù)為空!"); 
  19.     } 

需要注意一點(diǎn),由于不一定所有的評(píng)論都有回復(fù)數(shù)據(jù),所以在插入數(shù)據(jù)前我們要判斷ReplyList是否為空,如果不為空,直接獲取當(dāng)前評(píng)論的回復(fù)列表,并插入數(shù)據(jù);如果為空,需要new一個(gè)ReplyList,插入數(shù)據(jù)后還要為評(píng)論set一下ReplyList。

4、解決CoordinatorLayout與ExpandableListView嵌套問題

如果你不需要使用CoordinatorLayout或者NestedScrollView,可以跳過本小節(jié)。一般情況下,我們產(chǎn)品為了更好的用戶體驗(yàn),還需要我們加上類似的頂部視差效果或者下拉刷新等,這就要我們處理一些常見的嵌套滑動(dòng)問題了。

由于CoordinatorLayout實(shí)現(xiàn)NestedScrollingParent接口,RecycleView實(shí)現(xiàn)了NestedScrollingChild接口,所以就可以在NestedScrollingChildHelper的幫助下實(shí)現(xiàn)嵌套滑動(dòng),那么我們也可以通過自定義的ExpandableListView實(shí)現(xiàn)NestedScrollingChild接口來達(dá)到同樣的效果:

 

  1. /** 
  2.  * Desc: 自定義ExpandableListView,解決與CoordinatorLayout滑動(dòng)沖突問題 
  3.  */ 
  4. public class CommentExpandableListView extends ExpandableListView implements NestedScrollingChild{ 
  5.     private NestedScrollingChildHelper mScrollingChildHelper; 
  6.     public CommentExpandableListView(Context context, AttributeSet attrs) { 
  7.         super(context, attrs); 
  8.         mScrollingChildHelper = new NestedScrollingChildHelper(this); 
  9.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
  10.             setNestedScrollingEnabled(true); 
  11.         } 
  12.     } 
  13.     @Override 
  14.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  15.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
  16.         super.onMeasure(widthMeasureSpec, expandSpec); 
  17.     } 
  18.     @Override 
  19.     public void setNestedScrollingEnabled(boolean enabled) { 
  20.         mScrollingChildHelper.setNestedScrollingEnabled(enabled); 
  21.     } 
  22.     @Override 
  23.     public boolean isNestedScrollingEnabled() { 
  24.         return mScrollingChildHelper.isNestedScrollingEnabled(); 
  25.     } 
  26.     @Override 
  27.     public boolean startNestedScroll(int axes) { 
  28.         return mScrollingChildHelper.startNestedScroll(axes); 
  29.     } 
  30.     @Override 
  31.     public void stopNestedScroll() { 
  32.         mScrollingChildHelper.stopNestedScroll(); 
  33.     } 
  34.     @Override 
  35.     public boolean hasNestedScrollingParent() { 
  36.         return mScrollingChildHelper.hasNestedScrollingParent(); 
  37.     } 
  38.     @Override 
  39.     public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, 
  40.                                         int dyUnconsumed, int[] offsetInWindow) { 
  41.         return mScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, 
  42.                 dxUnconsumed, dyUnconsumed, offsetInWindow); 
  43.     } 
  44.     @Override 
  45.     public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { 
  46.         return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); 
  47.     } 
  48.     @Override 
  49.     public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { 
  50.         return mScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed); 
  51.     } 
  52.     @Override 
  53.     public boolean dispatchNestedPreFling(float velocityX, float velocityY) { 
  54.         return mScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY); 
  55.     } 

代碼就不解釋了,畢竟這不是本篇文章等重點(diǎn),大家可以去網(wǎng)上查閱NestedScrollView相關(guān)文章或者源碼去對(duì)照理解。

完整的布局代碼比較多,篇幅有限,大家可以去github上面查看:https://github.com/Moosphan/CommentWithReplyView-master

責(zé)任編輯:未麗燕 來源: 簡(jiǎn)書
相關(guān)推薦

2013-05-14 10:07:13

谷歌

2013-03-08 09:54:52

回復(fù)

2013-02-19 14:22:07

BT CONTACT

2010-07-19 10:16:24

ibmdwWeb2.0

2022-08-18 14:37:39

人工智能社交媒體數(shù)字化

2013-02-27 10:43:55

IBM大型主機(jī)移動(dòng)

2016-08-11 08:24:39

AndroidIntentShareTestDe

2022-08-01 00:01:36

網(wǎng)絡(luò)安全信息安全隱私

2013-01-10 16:32:39

Google開發(fā)者Google Play

2023-10-26 17:56:04

AI小快大規(guī)模語言模型

2021-04-18 10:24:35

WhatsAppAndroid惡意軟件

2014-05-04 17:51:11

應(yīng)用匯升級(jí)互動(dòng)平臺(tái)

2011-10-08 11:19:55

甲骨文云計(jì)算公有云

2021-09-21 10:51:42

iOS蘋果系統(tǒng)

2011-10-11 10:10:57

2014-06-19 14:43:22

2023-12-14 16:16:03

Django應(yīng)用程序Python

2024-04-15 00:00:01

GoogleAndroid機(jī)器學(xué)習(xí)

2024-01-15 08:21:12

Android應(yīng)用方式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)