build.gradle
implementation 'com.android.support:recyclerview-v7:27.1.1'
.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_marginTop="50dp"
android:layout_width="match_parent
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
.activity
private RecyclerView recyclerview;
recyclerview.setAdapter(ma);
//添加分割线
recyclerview.addItemDecoration(new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL));
//list列表展示
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(linearLayoutManager);
//grid列表展示
GridLayoutManager manager2 = new GridLayoutManager(this,3);
recyclerview.setLayoutManager(manager2);
/瀑布流展示
StaggeredGridLayoutManager manager1 = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerview.setLayoutManager(manager1);
.adapter
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
private SecondActivity mSecondActivity;
private ArrayList<Goods.DataBean> list;
public GoodsAdapter(SecondActivity secondActivity) {
this.mSecondActivity = secondActivity;
list=new ArrayList<>();
}
//更新适配器数据
public void setData(ArrayList<Goods.DataBean> dataBeans) {
for (int i = 0; i < dataBeans.size(); i++) {
list.add(dataBeans.get(i));
}
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder holder = new ViewHolder(View.inflate(mSecondActivity,R.layout.item_goods,null));
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),holder.iv);
holder.name.setText(list.get(position).getName());
holder.price.setText(list.get(position).getAverage_price_tip());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView iv;
TextView name,price;
public ViewHolder(View itemView){
super(itemView);
iv = itemView.findViewById(R.id.goods_iv);
name = itemView.findViewById(R.id.goods_name);
price = itemView.findViewById(R.id.goods_price);
}
}
.AFragment
public class AFragment extends Fragment {
private RecyclerView recyclerview;
private MAdapter ma;
private ArrayList<News.DataBean> dataBeans;
String Path = "http://www.xieast.com/api/news/news.php?coun=10&page=1";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
recyclerview = view.findViewById(R.id.recyclerview);
ma = new MAdapter(getActivity());
recyclerview.setAdapter(ma);
//设置分割线
recyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.HORIZONTAL));
recyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
//瀑布流
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerview.setLayoutManager(manager);
OkHttpUtil.getInstance().doGet(Path, new OkHttpUtil.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String string) {
News news = new Gson().fromJson(string, News.class);
dataBeans = (ArrayList<News.DataBean>) news.getData();
ma.setData(dataBeans);
}
});
return view;
}
}