效果圖:上面是輪播廣告,下面是recycleview瀑布流
首先說一下佈局
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:fitsSystemWindows="true"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.daimajia.slider.library.SliderLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
要做到滑動效果並且和recycleview聯動
1.添加頭佈局,可是很遺憾 recycleview並沒有提供addheadview addfootview的方法,雖然沒有提供,不過我們可以通過多條目recycleview 在adapter裏 重新 getItemViewType方法添加多條目來增加頭佈局或腳佈局
2.外面用可以滾動的控件包着,自己解決滑動衝突
這裏沒有ScrollView而是用5.0新出的 NestedScrollView
然後我們首先要寫一個adapter繼承recycleviewadapter,這裏還有一點需要注意,就是recycleview雖然佈局特別方面(這裏說的是實現橫向縱向列表或者瀑布流)但是並沒有自帶item點擊事件,也就是我們寫adapter要考慮到這一點,自己寫個接口回調實現條目點擊事件,感覺好坑爹啊啥也不帶
Adapter代碼:
package com.panghaha.it.mymusicplayerdemo;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/***
* ━━━━ Code is far away from ━━━━━━
* () ()
* ( ) ( )
* ( ) ( )
* ┏┛┻━━━┛┻┓
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┻ ┃
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ━━━━ bug with the more protecting ━━━
* <p/>
* Created by PangHaHa12138 on 2017/7/3.
*/
public class MeiziAdapter extends RecyclerView.Adapter<MeiziAdapter.ViewHolder> implements View.OnClickListener{
private List<meinv> mlist;
private Context context;
private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;
private View headView;
private View footView;
private boolean isAddFoot = false;
private boolean isAddHead = false;
private int headViewSize = 0;
private int footViewSize = 0;
private OnItemClickListener mOnItemClickListener = null;
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public MeiziAdapter(List<meinv> list,Context context){
this.mlist = list;
this.context = context;
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView meizi;
CardView cardView;
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView;
textView = (TextView) itemView.findViewById(R.id.hahtextt);
meizi = (ImageView) itemView.findViewById(R.id.meizitu);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycleitem,parent,false);
view.setOnClickListener(this);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
meinv meinv = mlist.get(position);
holder.meizi.setImageResource(meinv.getImageid());
holder.textView.setText(meinv.getMsg());
holder.itemView.setTag(position);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意這裏使用getTag方法獲取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
//define interface
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public int getItemCount() {
return mlist.size();
}
}
然後item佈局外面用Cardview包着,這也是最常見的效果
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_margin="5dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/meizitu"
android:src="@drawable/yutong1"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/hahtextt"
android:text="天王蓋地虎"
android:textSize="16sp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
在fragment使用
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mview = inflater.inflate(R.layout.fragmenta,container,false);
recyclerView = (RecyclerView) mview.findViewById(R.id.recycleview);
StaggeredGridLayoutManager layoutManager =
//這裏 3 代表三列 後面代表瀑布流朝向
new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
initlist();//初始化模擬數據
mDemoSlider = (SliderLayout) mview.findViewById(R.id.slider);
MeiziAdapter adapter = new MeiziAdapter(list,getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
SpcaceDecoration sc = new SpcaceDecoration(1);//設置間距
recyclerView.addItemDecoration(sc);
recyclerView.setNestedScrollingEnabled(false);
//NesterScrollView不允許滑動
recyclerView.setHasFixedSize(true);
//RecycleView固定大小會實現優化
adapter.setOnItemClickListener(new MeiziAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getActivity(),"我被點擊了"+position,Toast.LENGTH_SHORT).show();
}
});
setloopconfig();
return mview;
}
還有一點就是繼承RecyclerView.ItemDecoration 設置item間距
package com.panghaha.it.mymusicplayerdemo;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/***
* ━━━━ Code is far away from ━━━━━━
* () ()
* ( ) ( )
* ( ) ( )
* ┏┛┻━━━┛┻┓
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┻ ┃
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
* ━━━━ bug with the more protecting ━━━
* <p/>
* Created by PangHaHa12138 on 2017/7/4.
*/
public class SpcaceDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpcaceDecoration(int space) {
this.space=space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.left=space;
outRect.right=space;
outRect.bottom=space;
if(parent.getChildAdapterPosition(view)==0){
outRect.top=space;
}
}
}
最後還有一個小知識點,就是recycleview搶佔焦點問題
Recycleview的父容器加上
android:descendantFocusability="blocksDescendants"
這個就可以了
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
感謝閱讀 ~have a nice day~