日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 水平方向瀑布流,Android RecyclerView(瀑布流)水平/垂直方向分割线

發布時間:2024/4/11 Android 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 水平方向瀑布流,Android RecyclerView(瀑布流)水平/垂直方向分割线 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



Android RecyclerView(瀑布流)水平/垂直方向分割線

Android RecyclerView不像過去的ListView那樣隨意的設置水平方向的分割線,如果要實現RecyclerView的水平/垂直分割線,則需要繼承自RecyclerView.ItemDecoration重寫getItemOffsets方法,從而增加水平/垂直分割線。

寫一個例子。

MainActivity.java:

package zhangphil.app;

import android.content.Context;

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.StaggeredGridLayoutManager;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

// 兩列

int spanCount = 2;

// StaggeredGridLayoutManager管理RecyclerView的布局。

StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(mLayoutManager);

//為RecyclerView增加分割線,水平和垂直方向都有。增加分割線值比如為32。

RecyclerViewItemDecoration decoration = new RecyclerViewItemDecoration(32);

mRecyclerView.addItemDecoration(decoration);

RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(this);

mRecyclerView.setAdapter(mAdapter);

}

private class ItemViewHolder extends RecyclerView.ViewHolder {

private TextView text;

public ItemViewHolder(View itemView) {

super(itemView);

text = (TextView) itemView.findViewById(android.R.id.text1);

text.setTextColor(Color.WHITE);

}

}

public class RecyclerViewAdapter extends RecyclerView.Adapter {

private Context context;

public RecyclerViewAdapter(Context context) {

super();

this.context = context;

}

@Override

public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

View view = View.inflate(context, android.R.layout.simple_list_item_1, null);

view.setBackgroundColor(Color.RED);

ItemViewHolder holder = new ItemViewHolder(view);

return holder;

}

@Override

public void onBindViewHolder(ItemViewHolder viewHolder, int pos) {

viewHolder.text.setText(String.valueOf(pos));

}

@Override

public int getItemCount() {

return 15;

}

}

}

布局文件,很簡單,就放一個RecyclerView,注意背景顏色的設置:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_orange_light">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent">

最關鍵的RecyclerViewItemDecoration.java:

package zhangphil.app;

/**

* Created by Phil on 2016/10/8.

*/

import android.graphics.Rect;

import android.support.v7.widget.RecyclerView;

import android.view.View;

/**

* 為RecyclerView增加間距

* 預設2列,如果是3列,則左右值不同

*/

public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {

private int space = 0;

private int pos;

public RecyclerViewItemDecoration(int space) {

this.space = space;

}

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

outRect.top = space;

//該View在整個RecyclerView中位置。

pos = parent.getChildAdapterPosition(view);

//取模

//兩列的左邊一列

if (pos % 2 == 0) {

outRect.left = space;

outRect.right = space / 2;

}

//兩列的右邊一列

if (pos % 2 == 1) {

outRect.left = space / 2;

outRect.right = space;

}

}

}

代碼運行結果:

總結

以上是生活随笔為你收集整理的android 水平方向瀑布流,Android RecyclerView(瀑布流)水平/垂直方向分割线的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。