1. 程式人生 > >MVP展示RecycleView(瀑布流)

MVP展示RecycleView(瀑布流)

繼上篇部落格 1.2.3.7

一:view層

1.BaseView介面

public interface BaseView {
}

2.MainView介面繼承BaseView

public interface MainView extends BaseView{
    void onSuccess(ShouBean shouBean);

    void onFailed(String msg);
}

二:p層

1.BasePresenter

public class BasePresenter<V extends BaseView>{
    private V iv;

    public void attachView(V iv) {
        this.iv = iv;
    }

    public void datachView(V iv) {
        this.iv = iv;
    }

    public V getView() {
        return iv;
    }
}

2.MainPresenter繼承BasePresenter

public class MainPresenter extends BasePresenter<MainView> {

    public void loadData() {
        Call<ShouBean> shou = HttpUtils.getData().api.shou();
        shou.enqueue(new Callback<ShouBean>() {
            @Override
            public void onResponse(Call<ShouBean> call, Response<ShouBean> response) {
                ShouBean shouBean = response.body();
                getView().onSuccess(shouBean);
            }

            @Override
            public void onFailure(Call<ShouBean> call, Throwable t) {

            }
        });
    }
}

三:主頁面

public class MainActivity extends AppCompatActivity implements MainView {

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    public MainPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mainPresenter = new MainPresenter();
        mainPresenter.attachView(this);
        mainPresenter.loadData();
    }

    @Override
    public void onSuccess(ShouBean shouBean) {
        ShouBean.DataBean.TuijianBean tuijian = shouBean.getData().getTuijian();
        recycleView.setAdapter(new ShouAdapter(MainActivity.this,tuijian));

        //瀑布流
        recycleView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(50);
        recycleView.addItemDecoration(decoration);

        adapter.setOnItemCickListener(new ShouAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                int pid = shouBean.get(position).getPid();
                Intent intent = new Intent(MainActivity.this,XiangActivity.class);
                intent.putExtra("pid",String.valueOf(pid));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onFailed(String msg) {

    }

    //避免記憶體洩漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainPresenter.datachView(this);
    }
}

四:介面卡

public class ShouAdapter extends RecyclerView.Adapter<ShouAdapter.ViewHolder>{
    private Context context;
    private ShouBean.DataBean.TuijianBean list;

 private OnItemClickListener onItemCickListener;
    public ShouAdapter(Context context,  ShouBean.DataBean.TuijianBean list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.simple_view.setImageURI(list.getList().get(position).getImages().split("\\|")[0]);
        holder.text_view.setText(list.getList().get(position).getTitle());

 holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemCickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.getList().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final SimpleDraweeView simple_view;
        private final TextView text_view;

        public ViewHolder(View itemView) {
            super(itemView);
            simple_view = itemView.findViewById(R.id.simple_view);
            text_view = itemView.findViewById(R.id.text_view);
        }
    }

 public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemCickListener(OnItemClickListener onItemCickListener){
        this.onItemCickListener = onItemCickListener;
    }
}

五:item_layout佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/simple_view"/>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="xxxxxx"
        android:layout_margin="10dp"/>

</LinearLayout>

六:瀑布流類

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public SpacesItemDecoration(int space) {
        this.space=space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left=space;
        outRect.right=space;
        outRect.bottom=space;
        if(parent.getChildAdapterPosition(view)==0){
            outRect.top=space;
        }
    }
}