1. 程式人生 > >Recyclerview 瀑布流效果

Recyclerview 瀑布流效果

1.Activity

public class SecondActivity extends Activity {


    private RecyclerView recyclerView;
    private ArrayList<String> list;
    private WaterFallAdapter waterFallAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


        recyclerView = findViewById(R.id.recycler_view);


        //資料
        list = new ArrayList<>();
        for (int i=0; i<1000;i++){
            list.add("這是條目"+i);
        }


        //設定佈局管理器..staggered錯位的 錯亂的
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));


        //設定介面卡
        waterFallAdapter = new WaterFallAdapter(SecondActivity.this, list);
        recyclerView.setAdapter(waterFallAdapter);
    }
}

2.Adapter配置

public class WaterFallAdapter extends RecyclerView.Adapter<WaterHolder> {
    private ArrayList<String> list;
    private Context context;
    private final List<Integer> heightList;


    public WaterFallAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;


        heightList = new ArrayList<>();
        for (int i= 0;i<list.size();i++){
            heightList.add((int) (Math.random()*100+200));
        }


    }


    @Override
    public WaterHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        //1.打氣筒建立檢視
        View view = LayoutInflater.from(context).inflate(R.layout.item_water_layout, parent, false);
        //2.建立檢視繫結的holder
        WaterHolder holder = new WaterHolder(view);


        return holder;
    }


    @Override
    public void onBindViewHolder(WaterHolder holder, int position) {
        //先獲取到textView身上的佈局引數
        ViewGroup.LayoutParams layoutParams = holder.textView.getLayoutParams();
        layoutParams.height = heightList.get(position);


        //設定背景顏色
        holder.textView.setBackgroundColor(Color.rgb((int)(Math.random()*100+155),(int)(Math.random()*100+155),(int)(Math.random()*100+155)));


        holder.textView.setText(list.get(position));
    }

3.Holder

public class WaterHolder extends RecyclerView.ViewHolder {


    public TextView textView;


    public WaterHolder(View itemView) {
        super(itemView);


        Log.i("---","=======");
        //holder中只做找到控制元件的操作
        textView = itemView.findViewById(R.id.text_view);


    }
}

4.主頁面佈局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dash.projectone_1510a.activity.SecondActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </android.support.v7.widget.RecyclerView>


</android.support.constraint.ConstraintLayout>

5.子頁面佈局 textview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/text_view"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>