RecyclerView(3)瀑布式佈局
阿新 • • 發佈:2018-12-12
效果圖
activity
package com.example.recyclerview.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import com.example.recyclerview.R; import com.example.recyclerview.User; import com.example.recyclerview.adapter.ThreeAdapter; public class ThreeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RecyclerView recyclerView = findViewById(R.id.recyclerview); StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(staggeredGridLayoutManager); ThreeAdapter threeAdapter = new ThreeAdapter(this); String[] avatarArray = new String[]{ "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544442373811&di=848a4182d8f35674317cd145fa09fa88&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Fface%2Faf5068ab80526f8d4a9acb8907277b534c633d07.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545037250&di=866ab32daebc69e66bcb041b97340fb7&imgtype=jpg&er=1&src=http%3A%2F%2Fhellorfimg.zcool.cn%2Fpreview%2F293300162.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544452449351&di=9063f14cfc8d124e7ed08c635f677d19&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Ff9dcd100baa1cd11a438a2e9b312c8fcc2ce2d69.jpg"}; for (int i = 0; i < 30; i++) { User user = new User(); user.setName("張"+i); user.setAvatar(avatarArray[i%avatarArray.length]); threeAdapter.addItem(user); } recyclerView.setAdapter(threeAdapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); } }
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.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
adapter程式碼
package com.example.recyclerview.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.recyclerview.R; import com.example.recyclerview.User; import java.util.ArrayList; import java.util.List; public class ThreeAdapter extends RecyclerView.Adapter<ThreeAdapter.MyViewholder> { private List<User> mDatas; private Context context; public ThreeAdapter(Context context) { this.mDatas = new ArrayList<>(); this.context = context; } public void addItem(User user){ if(user != null){ mDatas.add(user); } } @NonNull @Override public MyViewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.three,viewGroup,false); return new MyViewholder(view); } @Override public void onBindViewHolder(@NonNull MyViewholder myViewholder, int i) { User user = mDatas.get(i); myViewholder.title.setText(user.getName()); Glide.with(context).load(user.getAvatar()).into(myViewholder.avatar); myViewholder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } @Override public int getItemCount() { return mDatas.size(); } public class MyViewholder extends RecyclerView.ViewHolder { public final TextView title; public final ImageView avatar; public MyViewholder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.title); avatar = itemView.findViewById(R.id.avatar); } } }
xml程式碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/avatar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"/>
</LinearLayout>