1. 程式人生 > >OKGO 和 RecycleView 瀑布流

OKGO 和 RecycleView 瀑布流

1.導依賴

//OKGO
implementation 'com.lzy.net:okgo:3.0.4'
//RecycleView
implementation 'com.android.support:design:28.0.0'

2.佈局

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"></android.support.v7.widget.RecyclerView>

3.在Activity中寫

public class AnimationActivity extends AppCompatActivity {

Context context;
@BindView(R.id.recycler_view)
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animation);
    ButterKnife.bind(this);
    context = AnimationActivity.this;
    //網路請求
    OkGo.<String>get(Contract.BETAY_URL).execute(new StringCallback() {
        @Override
        public void onSuccess(Response<String> response) {
            String responseData = response.body().toString();
            //資料解析
            Gson gson = new Gson();
            BeautifulBean beautifulBean = gson.fromJson(responseData, BeautifulBean.class);
            //1.資料來源
            ArrayList<BeautifulBean.ResultsBean> beanList = (ArrayList<BeautifulBean.ResultsBean>) beautifulBean.getResults();
            //2.佈局管理器
            //線性  網格  瀑布流
            LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(manager);
            //3.介面卡
            BeautyAdapter beautyAdapter = new BeautyAdapter();
            beautyAdapter.setData(beanList, context);
            //設定介面卡
            recyclerView.setAdapter(beautyAdapter);
            beautyAdapter.notifyDataSetChanged();
        }
    });


}}

4.寫介面常量 和bean類
5.建立RecycleView 的介面卡

public class BeautyAdapter extends RecyclerView.Adapter<BeautyAdapter.SubViewHolder> {

ArrayList<BeautifulBean.ResultsBean> beanList;
Context context;


public void setData(ArrayList<BeautifulBean.ResultsBean> beanList, Context context) {
    this.beanList = beanList;
    this.context = context;
}

@NonNull
@Override
public SubViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    //填充子佈局
    View rootView = LayoutInflater.from(context).inflate(R.layout.beauty_layout, null);
    //設定ViewHolder
    SubViewHolder subViewHolder = new SubViewHolder(rootView);
    return subViewHolder;
}

@Override
public void onBindViewHolder(@NonNull SubViewHolder subViewHolder, int position) {
    subViewHolder.tv_title.setText("建立時間是:"+beanList.get(position).getCreatedAt());
    //載入圖片
    Glide.with(context).load(beanList.get(position).getUrl()).into(subViewHolder.image_icon);
}

@Override
public int getItemCount() {
    //條目數
    return beanList.size();
}

class SubViewHolder extends RecyclerView.ViewHolder {

    private final TextView tv_title;
    private final ImageView image_icon;

    public SubViewHolder(@NonNull View itemView) {
        super(itemView);

        tv_title = itemView.findViewById(R.id.tv_title);
        image_icon = itemView.findViewById(R.id.image_icon);
    }
}}