欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊功能(在之前的微信頁面基礎(chǔ)之上)

 更新時間:2020年03月29日 16:04:50   作者:qq_41206634  
這篇文章主要介紹了Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊(在之前的微信頁面基礎(chǔ)之上),本文通過截圖實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下

Android中RecyclerView點(diǎn)擊item展開列表詳細(xì)內(nèi)容

效果如下:

折疊效果

展開效果

依然是xml文件的設(shè)計(jì),使用了兩個RelativeLayout,zu作為主布局和副布局,里面都加入textview顯示內(nèi)容,在副布局里加入一個imageview在這里插入圖片描述作為子內(nèi)容的背景圖,代碼如下:

tab01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 xmlns:tools="http://schemas.android.com/tools"
 android:orientation="vertical">
<RelativeLayout
  android:id="@+id/rl_parent"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="#00574B">

  <TextView
   android:id="@+id/textViewparent"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="30dp"
   android:layout_centerInParent="true"
   android:textColor="@android:color/white"
   tools:text="主布局" />
 </RelativeLayout>

 <RelativeLayout
  android:id="@+id/rl_child"
  android:layout_width="match_parent"
  android:layout_height="100dp"
  android:background="#ffffff"
  android:visibility="gone">

  <ImageView
   android:id="@+id/imageview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

  <TextView
   android:id="@+id/textViewchild"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:textSize="25dp"
   android:textColor="#000000"
   tools:text="副部局" />
 </RelativeLayout>
</LinearLayout>

activity_main.xml

<?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">
 <include layout="@layout/top" />
 <FrameLayout
  android:id="@+id/id_content"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1">
  <android.support.v7.widget.RecyclerView
   android:id="@+id/recyclerView"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
  </android.support.v7.widget.RecyclerView>
 </FrameLayout>
 <include layout="@layout/bottom" />
</LinearLayout>

核心代碼Adapter:

public class adapter extends RecyclerView.Adapter <adapter.myviewholder>{
 private List<String> mlist=new ArrayList<>();
 private Context context;
 private View inflater;
 private static final String tag=adapter.class.getSimpleName();
 private int expandedPosition=1;
 private myviewholder mViewHolder;
 public adapter(Context context) {//,List<String> list
  this.context=context;
 }
 public void setExpandCollapseDataList(List<String> list){
  mlist=list;
  notifyDataSetChanged();
 }
 @Override
 public adapter.myviewholder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  inflater= LayoutInflater.from(context).inflate(R.layout.tab01,viewGroup,false);
  myviewholder myviewholder=new myviewholder(inflater);
  return myviewholder;
 }
 @Override
 public void onBindViewHolder(final adapter.myviewholder myviewholder, final int j) {
  myviewholder.textviewparent.setText(mlist.get(j));
  myviewholder.imageview.setImageResource(R.drawable.p1);
  myviewholder.textviewchild.setText(mlist.get(j+15));
  final boolean isExpanded=j==expandedPosition;
  myviewholder.rlChild.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
  myviewholder.rlParent.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if (mViewHolder != null) {
     mViewHolder.rlChild.setVisibility(View.GONE);
     notifyItemChanged(expandedPosition);
    }
    expandedPosition = isExpanded ? -1 : myviewholder.getAdapterPosition();
    mViewHolder = isExpanded ? null : myviewholder;
    notifyItemChanged(myviewholder.getAdapterPosition());
   }
  });
 }
 @Override
 public int getItemCount() {
  return mlist == null ? 0 : mlist.size()/2;
 }
 class myviewholder extends RecyclerView.ViewHolder{
  private ImageView imageview;
  RelativeLayout rlParent, rlChild;
  TextView textviewparent, textviewchild;
  public myviewholder(View itemView) {
   super(itemView);
   rlParent = itemView.findViewById(R.id.rl_parent);
   rlChild = itemView.findViewById(R.id.rl_child);
   textviewparent = itemView.findViewById(R.id.textViewparent);
   textviewchild = itemView.findViewById(R.id.textViewchild);
   imageview=itemView.findViewById(R.id.imageview);
  }
 }
}

主要的代碼是 ViewHolder 中 bindView() , onClick() 這兩個方法對變量的操作。將list中前一半作為主布局?jǐn)?shù)據(jù),后一半作為副布局的數(shù)據(jù)生成。

數(shù)據(jù)和執(zhí)行

private void initmyData() {
 list.add("路德維?!し病へ惗喾?);
 list.add("蕭友梅");
 list.add("阿炳");
 list.add("冼星海");
 list.add("聶耳");
 list.add("施光南");
 list.add("弗里德里克·肖邦");
 list.add("羅伯特·舒曼");
 list.add("莫扎特");
 list.add("約瑟夫·海頓");
 list.add("舒伯特");
 list.add("巴赫");
 list.add("弗侖茲·李斯特");
 list.add("約翰奈斯·勃拉姆斯");
 list.add("門德爾松");
 list.add("德國作曲家、鋼琴家、指揮家,被稱為樂圣。");
 list.add("中國專業(yè)音樂教育的奠基人和開拓者、音樂理論家、作曲家。");
 list.add("民間音樂家、二胡演奏家,譽(yù)為演奏能手。");
 list.add("中國近代作曲家、鋼琴家--人民音樂家。");
 list.add("中國音樂家--時代歌手。");
 list.add("譽(yù)為時代歌手,現(xiàn)代抒情歌曲作曲家。");
 list.add("譽(yù)為鋼琴詩人,波蘭作曲家、鋼琴家。");
 list.add("德國著名作曲家、音樂評論家。");
 list.add("奧地利作曲家,被譽(yù)為神童。");
 list.add("奧地利作曲家,維也納古典派奠基者之一。");
 list.add("奧地利作曲家--前所未有的最富詩意的音樂家。");
 list.add("德國最偉大的古典作曲家之一,管風(fēng)琴演奏家。");
 list.add("天才的匈牙利作曲家、鋼琴家、指揮家和音樂活動家。");
 list.add("德國十九世紀(jì)后半葉最卓越的、古典樂派最后的一位作曲家。");
 list.add("德國著名作曲家。");
}

private void initView1() {
 adapter adapter = new adapter(this);
 RecyclerView recyclerView = findViewById(R.id.recyclerView);
 recyclerView.setLayoutManager(new LinearLayoutManager(this));
 recyclerView.setHasFixedSize(true);
 recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
 recyclerView.setAdapter(adapter);
 adapter.setExpandCollapseDataList(list);
}

話不多說,上代碼。

github源碼地址:
鏈接: https://github.com/YL009/yl.

總結(jié)

到此這篇關(guān)于Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊(在之前的微信頁面基礎(chǔ)之上)的文章就介紹到這了,更多相關(guān)android studio recyclerview實(shí)現(xiàn)展開折疊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android自定義View模仿QQ討論組頭像效果

    Android自定義View模仿QQ討論組頭像效果

    最近發(fā)現(xiàn)QQ討論組的頭像非常不錯,正好最近又有時間,所有就動手實(shí)踐了下,所以下面這篇文章主要給大家介紹了Android自定義View模仿QQ討論組頭像效果的相關(guān)資料,文中給出了詳細(xì)的介紹的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。
    2017-04-04
  • 利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載詳解

    利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載詳解

    這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • Android中網(wǎng)絡(luò)框架簡單封裝的實(shí)例方法

    Android中網(wǎng)絡(luò)框架簡單封裝的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于Android中網(wǎng)絡(luò)框架簡單封裝的實(shí)例方法,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼

    Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼

    本文通過實(shí)例代碼給大家介紹了android將文件下載到指定目錄的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-06-06
  • Android解析JSON數(shù)據(jù)的方法分析

    Android解析JSON數(shù)據(jù)的方法分析

    這篇文章主要介紹了Android解析JSON數(shù)據(jù)的方法,結(jié)合實(shí)例形式演示了Android解析json格式數(shù)據(jù)的原理與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android Camera2 實(shí)現(xiàn)預(yù)覽功能

    Android Camera2 實(shí)現(xiàn)預(yù)覽功能

    最近在做一些關(guān)于人臉識別的項(xiàng)目,需要用到 Android 相機(jī)的預(yù)覽功能。今天小編通過本文給大家分享Android Camera2 實(shí)現(xiàn)預(yù)覽功能,感興趣的朋友跟隨小編一起看看吧
    2018-11-11
  • Android入門之Fragment嵌套Fragment的用法詳解

    Android入門之Fragment嵌套Fragment的用法詳解

    這篇文章主要為大家詳細(xì)介紹了Android中如何實(shí)現(xiàn)Fragment嵌套Fragment的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考一下
    2023-02-02
  • Android自定義ViewGroup實(shí)現(xiàn)右滑進(jìn)入詳情

    Android自定義ViewGroup實(shí)現(xiàn)右滑進(jìn)入詳情

    這篇文章主要為大家詳細(xì)介紹了Android如何通過自定義ViewGroup實(shí)現(xiàn)右滑進(jìn)入詳情效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01
  • 淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明

    淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明

    本篇文章是對AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android線程的優(yōu)先級設(shè)置方法技巧

    Android線程的優(yōu)先級設(shè)置方法技巧

    對于Android平臺上的線程優(yōu)先級設(shè)置來說可以處理很多并發(fā)線程的阻塞問題,比如很多無關(guān)緊要的線程會占用大量的CPU時間,雖然通過了MultiThread來解決慢速I/O但是合理分配優(yōu)先級對于并發(fā)編程來說十分重要
    2016-02-02

最新評論