FlexboxLayoutManager+ RecyclerView實現流式佈局,樓層導航欄,摺疊導航欄,導航欄和列表聯動
好久不寫安卓相關的文章了,最近專案需要就做了一個recylerview和頂部導航欄聯動,並且導航欄要有摺疊效果,樓層效果,大體效果如下(手機錄屏,解析度不是很高)
下面來講解具體實現
一,先看佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"/>
<com.example.qcl.demo.floorview.FloorView
android:id="@+id/widget_show_all_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:visibility="visible"/>
</RelativeLayout>
佈局檔案裡用到了FloorView這個自定義控制元件。
二,來看自定義控制元件FloorView
package com.example.qcl.demo.floorview;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.example.qcl.demo.R;
import com.google.android.flexbox.FlexboxLayoutManager;
import java.util.ArrayList;
import java.util.List;
public class FloorView extends LinearLayout {
private Context context;
private View widgetView;
private RecyclerView recyclerView;
MyAdapter myAdapter;
private boolean isArrowDown;
private ImageView ivArrow;
VirtualLayoutManager vLayoutManager;
private int selectNum = 0;
private List<String> listAll;
private List<String> listFirstLine;
public FloorView(Context context) {
this(context, null);
}
public FloorView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public FloorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
widgetView = inflate(context, R.layout.widget_indicator_floor_view, this);
initRecylerView();
initArrow();
}
private void initArrow() {
ivArrow = widgetView.findViewById(R.id.iv_arrow);
ivArrow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isArrowDown) {
} else {
}
isArrowDown = !isArrowDown;
setArrow(isArrowDown);
}
});
}
private void initRecylerView() {
recyclerView = widgetView.findViewById(R.id.rv_list);
//設定flexbox
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
// layoutManager.setFlexDirection(FlexDirection.ROW);
// layoutManager.setJustifyContent(JustifyContent.FLEX_END);
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter();
recyclerView.setAdapter(myAdapter);
}
public void addDisplayItem(ArrayList<String> lists, int num) {
if (lists == null) {
return;
}
listAll = lists;
myAdapter.setSelectPosition(num);
if (lists.size() > 4) {
listFirstLine = lists.subList(0, 4);
ivArrow.setVisibility(View.VISIBLE);
setArrow(true);
myAdapter.setListData(listFirstLine);
myAdapter.notifyDataSetChanged();
} else {
ivArrow.setVisibility(View.GONE);
myAdapter.setListData(listAll);
myAdapter.notifyDataSetChanged();
}
}
//展開或者摺疊的箭頭
public void setArrow(boolean isDown) {
if (isDown) {
ivArrow.setImageResource(R.drawable.arrow_down);
myAdapter.setListData(listAll);
myAdapter.notifyDataSetChanged();
} else {
ivArrow.setImageResource(R.drawable.arrow_up);
myAdapter.setListData(listFirstLine);
myAdapter.notifyDataSetChanged();
}
}
public void setRecyclerView(RecyclerView outRecyclerView, VirtualLayoutManager
vLayoutManager2) {
this.vLayoutManager = vLayoutManager2;
outRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int firstVisibleItemPosition = vLayoutManager.findFirstVisibleItemPosition();
myAdapter.setSelectPosition((firstVisibleItemPosition / 5) % 8);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
public void selectPosition(int position) {
if (vLayoutManager == null) {
return;
}
LinearSmoothScroller smoothScroller = new LinearSmoothScroller
(context) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
//設定滑動1px所需時間
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
//縮短每px的滑動時間
float MILLISECONDS_PER_INCH = getResources().getDisplayMetrics()
.density * 0.03f;
return MILLISECONDS_PER_INCH / displayMetrics.density;
//返回滑動一個pixel需要多少毫秒
}
};
smoothScroller.setTargetPosition(position * 5);
vLayoutManager.startSmoothScroll(smoothScroller);
myAdapter.setSelectPosition(position);
}
/**
* 用內部類來寫adapter
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> contents;
public void setListData(List<String> lists) {
this.contents = lists;
}
public void setSelectPosition(int num) {
selectNum = num;
notifyDataSetChanged();
}
//建立新View,被LayoutManager所呼叫
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_floor, viewGroup, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
//將資料與介面進行繫結的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.mTextView.setText(contents.get(position));
if (selectNum == position) {
viewHolder.mTextView.setBackgroundResource(R.drawable.shape_show_all_indicator_rectangle_active);
viewHolder.mTextView.setTextColor(0xff12CFC9);
} else {
viewHolder.mTextView.setBackgroundResource(R.drawable.shape_show_all_indicator_rectangle);
viewHolder.mTextView.setTextColor(0xff666666);
}
viewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setSelectPosition(position);
selectPosition(position);
}
});
}
//獲取資料的數量
@Override
public int getItemCount() {
return contents.size();
}
//自定義的ViewHolder,持有每個Item的的所有介面元素
class ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
ViewHolder(View view) {
super(view);
mTextView = view.findViewById(R.id.tv);
}
}
}
}
是不是感覺程式碼挺多的,其實原理很簡單
- 1,FlexboxLayoutManager+ RecyclerView實現流式佈局
- 2,RecyclerView的MyAdapter來實現摺疊或者展開效果
//展開或者摺疊的箭頭
public void setArrow(boolean isDown) {
if (isDown) {
ivArrow.setImageResource(R.drawable.arrow_down);
myAdapter.setListData(listAll);
myAdapter.notifyDataSetChanged();
} else {
ivArrow.setImageResource(R.drawable.arrow_up);
myAdapter.setListData(listFirstLine);
myAdapter.notifyDataSetChanged();
}
}
```
通過往myAdapter裡傳入第一行4個數據,或者傳入全部資料來實現摺疊和展開。
####三,列表和導航欄聯動
outRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int firstVisibleItemPosition = vLayoutManager.findFirstVisibleItemPosition();
myAdapter.setSelectPosition((firstVisibleItemPosition / 5) % 8);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
主要通過監聽RecyclerView的第一個可見條目的位置來實現導航欄的跟隨選中
####四,點選導航欄實現列表錨點
LinearSmoothScroller smoothScroller = new LinearSmoothScroller
(context) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
//設定滑動1px所需時間
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
//縮短每px的滑動時間
float MILLISECONDS_PER_INCH = getResources().getDisplayMetrics()
.density * 0.03f;
return MILLISECONDS_PER_INCH / displayMetrics.density;
//返回滑動一個pixel需要多少毫秒
}
};
smoothScroller.setTargetPosition(position * 5);
“`