您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么利用RecyclerView實現一個復雜的列表布局,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.tony.recyclerviewdemo.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycleView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
item_type_one.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="60dp" xmlns:tools="http://schemas.android.com/tools" android:background="@android:color/white" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/avatar" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="20dp" /> <TextView tools:text = "月滿軒尼詩" android:id="@+id/name" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
item_type_two.xml:
<?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:layout_width="match_parent" android:layout_height="60dp" android:background="@android:color/white" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/avatar" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="20dp" /> <LinearLayout android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="月滿軒尼詩" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" tools:text="月滿軒尼詩是一部好電影嗎?" /> </LinearLayout> </LinearLayout>
item_type_three.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="60dp" android:background="@android:color/white" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/avatar" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_marginLeft="20dp" /> <ImageView android:id="@+id/contentImage" android:layout_width="100dp" android:layout_height="60dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:orientation="vertical" android:layout_centerVertical="true" android:layout_toRightOf="@+id/avatar" android:layout_toEndOf="@+id/avatar" android:layout_marginStart="20dp"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="月滿軒尼詩" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" tools:text="月滿軒尼詩是一部好電影嗎?" /> </LinearLayout> </RelativeLayout>
這邊提一個小技巧,一般textview如果不設置文本內容的話是無法預覽的,這里我們在屬性中添加一個:tools:text=”abc”;
這個時候abc就會在UI上面顯示出來了,而且abc也不用加入string.xml.
Java文件:
MAinActivity.class
package com.example.tony.recyclerviewdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private DemoAdapter mAdapter; int colors[] = {android.R.color.holo_blue_light, android.R.color.holo_orange_light, android.R.color.holo_red_light}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recycleView); mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); mAdapter = new DemoAdapter(this); mRecyclerView.setAdapter(mAdapter); initData(); } private void initData() { List<DataModel> list = new ArrayList<>(); for (int i = 0; i < 20; i++) { int type = (int) (Math.random() * 3 + 1); DataModel data = new DataModel(); data.avatarColor = colors[type - 1]; data.type = type; data.name = "Name: " + i; data.content = "Content: " + i; data.contentColor = colors[(type + 1) % 3]; list.add(data); } mAdapter.addList(list); mAdapter.notifyDataSetChanged(); } }
DemoAdapter.class
package com.example.tony.recyclerviewdemo; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /* * Created by Tony on 2016/12/3. */ public class DemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private LayoutInflater mLayoutInflater; private List<DataModel> mList = new ArrayList<>(); public DemoAdapter(Context context) { mLayoutInflater = LayoutInflater.from(context); } public void addList(List<DataModel> list) { mList.addAll(list); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case DataModel.TYPE_ONE: return new TypeOneViewHolder(mLayoutInflater.inflate(R.layout.item_type_one, parent, false)); case DataModel.TYPE_TWO: return new TypeTwoViewHolder(mLayoutInflater.inflate(R.layout.item_type_two, parent, false)); case DataModel.TYPE_THREE: return new TypeThreeViewHolder(mLayoutInflater.inflate(R.layout.item_type_three, parent, false)); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((TypeAbstractViewHolder) holder).bindHolder(mList.get(position)); } @Override public int getItemViewType(int position) { return mList.get(position).type; } @Override public int getItemCount() { return mList.size(); } }
DataModel.class
package com.example.tony.recyclerviewdemo; /** * Created by Tony on 2016/12/3. */ public class DataModel { public static final int TYPE_ONE = 1; public static final int TYPE_TWO = 2; public static final int TYPE_THREE = 3; public int type; public int avatarColor; public String name; public String content; public int contentColor; }
TypeAbstractViewHolder.class
package com.example.tony.recyclerviewdemo; import android.support.v7.widget.RecyclerView; import android.view.View; /** * Created by Tony on 2016/12/3. */ public abstract class TypeAbstractViewHolder extends RecyclerView.ViewHolder{ public TypeAbstractViewHolder(View itemView) { super(itemView); } public abstract void bindHolder(DataModel model); }
TypeOneViewHolder.class
package com.example.tony.recyclerviewdemo; import android.view.View; import android.widget.ImageView; import android.widget.TextView; /** * Created by Tony on 2016/12/3. */ public class TypeOneViewHolder extends TypeAbstractViewHolder { private ImageView avatar; private TextView name; public TypeOneViewHolder(View itemView) { super(itemView); avatar = (ImageView) itemView.findViewById(R.id.avatar); name = (TextView) itemView.findViewById(R.id.name); } //ViewHolder數據和外面數據綁定起來 @Override public void bindHolder(DataModel model) { avatar.setBackgroundResource(model.avatarColor); name.setText(model.name); } }
TypeTwoViewHolder.class
package com.example.tony.recyclerviewdemo; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; /** * Created by Tony on 2016/12/3. */ public class TypeTwoViewHolder extends TypeAbstractViewHolder { private ImageView avatar; private TextView name; private TextView content; public TypeTwoViewHolder(View itemView) { super(itemView); avatar = (ImageView) itemView.findViewById(R.id.avatar); name = (TextView) itemView.findViewById(R.id.name); content = (TextView) itemView.findViewById(R.id.content); } //ViewHolder數據和外面數據綁定起來 @Override public void bindHolder(DataModel model) { avatar.setBackgroundResource(model.avatarColor); name.setText(model.name); content.setText(model.content); } }
TypeThreeViewHolder.class
package com.example.tony.recyclerviewdemo; import android.view.View; import android.widget.ImageView; import android.widget.TextView; /** * Created by Tony on 2016/12/3. */ public class TypeThreeViewHolder extends TypeAbstractViewHolder { private ImageView avatar; private TextView name; private TextView content; private ImageView contentImage; public TypeThreeViewHolder(View itemView) { super(itemView); avatar = (ImageView) itemView.findViewById(R.id.avatar); contentImage = (ImageView) itemView.findViewById(R.id.contentImage); name = (TextView) itemView.findViewById(R.id.name); content = (TextView) itemView.findViewById(R.id.content); } //ViewHolder數據和外面數據綁定起來 @Override public void bindHolder(DataModel model) { avatar.setBackgroundResource(model.avatarColor); name.setText(model.name); contentImage.setBackgroundResource(model.contentColor); content.setText(model.content); } }
關于怎么利用RecyclerView實現一個復雜的列表布局就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。