首先上图:
recycler_item.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="69px" android:clickable="true" android:id="@+id/layout"> <TextView android:layout_width="80px" android:layout_height="69px" android:background="@drawable/gv_grid_selector" android:text="Medium Text" android:layout_gravity="center" android:gravity="center" android:id="@+id/textView" /> </LinearLayout>
recyclerview的设置
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_marginTop="200px" android:layout_width="350px" android:layout_height="230px"/>
fragment代码:
package com.example.navigation; import android.content.Context; import android.graphics.Rect; import android.net.Uri; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.navigation.Navigation; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class FragmentOne extends Fragment { private View mRootView; private RecyclerView recyclerView; private PlayListAdapter adapter; private List<Playlist_Info> list = new ArrayList<>(); String[] data = {"1", "2", "3", "0", "4", "5", "6", "7", "8", "9"}; private DividerItemDecoration itemDecoration; private int height_80 = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment mRootView = inflater.inflate(R.layout.fragment_one, container, false); height_80 = getResources().getDimensionPixelOffset(R.dimen.height_80); //找到 RecyclerView RecyclerView recylcer = mRootView.findViewById(R.id.recyclerView); //ListView效果的 LinearLayoutManager //LinearLayoutManager mgr = new LinearLayoutManager(this); //VERTICAL纵向,类似ListView,HORIZONTAL横向,类似Gallery //mgr.setOrientation(LinearLayoutManager.HORIZONTAL); //GridLayout 3列 //GridLayoutManager mgr=new GridLayoutManager(this,3); //3列 方向垂直 StaggeredGridLayoutManager mgr = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL); SpacesItemDecoration spacesItemDecoration = new SpacesItemDecoration(new Rect(0,13,0,0)); spacesItemDecoration.setCalculator(1); recylcer.addItemDecoration(spacesItemDecoration); recylcer.setLayoutManager(mgr); //设置适配器 recylcer.setAdapter(new MyAdapter()); return mRootView; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button fragment_two = view.findViewById(R.id.btn_go_to_fragment_two); fragment_two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Navigation.findNavController(view).navigate(R.id.action_fragmentOne_to_fragmentTwo); } }); } //继承自 RecyclerView.Adapter class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { //RecyclerView显示的子View //该方法返回是ViewHolder,当有可复用View时,就不再调用 @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = getLayoutInflater().inflate(R.layout.recycler_item, null); return new ViewHolder(v); } //将数据绑定到子View @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.textView.setText(data[i]); if(data[i].equals("0")){ LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 233); viewHolder.textView.setLayoutParams(lp); } } //RecyclerView显示数据条数 @Override public int getItemCount() { return data.length; } //自定义的ViewHolder,减少findViewById调用次数 class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.textView); } } } }
SpacesItemDecoration.java
package com.example.navigation;
import android.graphics.Rect;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int left;
private int right;
private int top;
private int bottom;
private int status;
public SpacesItemDecoration(Rect rect)
{
this.left = rect.left;
this.right = rect.right;
this.top = rect.top;
this.bottom = rect.bottom;
}
public void setCalculator(int status)
{
this.status = status;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.left = this.left;
outRect.bottom = this.bottom;
int childcount = parent.getAdapter().getItemCount();
//由于每行都只有5个,所以第一个都是5的倍数,把左边距设为0
if (parent.getChildLayoutPosition(view) %4==0) {
outRect.left = 0;
}
if(this.status == 1)
{
if(parent.getChildLayoutPosition(view) >3)
{
outRect.top = 13;
}
}
}
}