ShouYe

shouye_fragment.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="match_parent"
    android:orientation="vertical"
    >
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap"
        ></SearchView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

shouye_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="match_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_width="175dp"
        android:layout_height="200dp" />
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ShouYe(Model<ShouYeModel>,Presenter<ShoeYepresenter>,View<ShouYeView>)

Model<ShouYeModel>

public class ShouYeModel {
    public void getShouYe(String url, IcallBack icallBack, Type type){
        HttpUtils.getInstance().get(url, icallBack,type);
    }
}

ShouYePresenter

public class ShouYePresenter {
    private ShouYeView iv;
    private ShouYeModel mShouYeModel;

    public void attach(ShouYeView iv){
        this.iv = iv;
        mShouYeModel = new ShouYeModel();
    }
    public void get(){
        Type type = new TypeToken<ShouYe>(){}.getType();
        mShouYeModel.getShouYe("http://www.xieast.com/api/news/news.php", new IcallBack() {
            @Override
            public void onSunness(Object obj) {
                ShouYe shouYe = (ShouYe) obj;
                if (shouYe != null){
                    iv.onSuccess(shouYe.getData());
                }
            }

            @Override
            public void onfailed(Exception e) {
                iv.onFailed(e);
            }
        },type);
    }
}

ShouYeView

public interface ShouYeView {
    void onSuccess(List<ShouYe.DataBean> list);
    void onFailed(Exception e);

}

ShouYeAdapter

public class ShouYeAdapter extends RecyclerView.Adapter<ShouYeAdapter.ViewHolder> {
    private Context context;
    private List<ShouYe.DataBean> mList;

    public ShouYeAdapter(Context context, List<ShouYe.DataBean> list) {
        this.context = context;
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = View.inflate(context, R.layout.shouye_item,null);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(mList.get(position).getThumbnail_pic_s()).into(holder.img);
        holder.title.setText(mList.get(position).getTitle());
    }

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

     class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private TextView title;
        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            title = itemView.findViewById(R.id.title);
        }
    }
}

ShouYeFragment

public class ShouYeFragment extends Fragment implements ShouYeView{
    private RecyclerView recyview;
    private ShouYeAdapter adapter;
    private List<ShouYe.DataBean> listBeans;
    private ShouYePresenter presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.shouye_fragment,null,false);
        recyview = v.findViewById(R.id.recyview);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecyclerView.LayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyview.setLayoutManager(manager);
        listBeans = new ArrayList<>();
        adapter = new ShouYeAdapter(getActivity(),listBeans);
        recyview.setAdapter(adapter);
        presenter = new ShouYePresenter();
        presenter.attach(this);
        presenter.get();
    }

    @Override
    public void onSuccess(List<ShouYe.DataBean> list) {
        if (list != null){
            listBeans.clear();
            listBeans.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailed(Exception e) {
        Toast.makeText(getActivity(),"错误",Toast.LENGTH_LONG).show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值