MVP列表展示

MVP列表展示_第1张图片



//依赖:

//加载图片
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.loopj.android:android-async-http:1.4.9'


//权限:



//接口:

http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0



//main.xml文件:




    
    




//item.xml文件:




    

    





//MainActivity : 

public class MainActivity extends AppCompatActivity implements ShowView {

    private RecyclerView rlv;

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

        rlv = (RecyclerView)findViewById(R.id.clv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rlv.setLayoutManager(linearLayoutManager);
        //P关联V
        ShowPresentermpl presentermpl = new ShowPresentermpl(this);
        //P关联M 做网络请求
        presentermpl.relevance();


    }

    @Override
    public void showData(NewsBean newsBean) {
        MyAdapter adapter = new MyAdapter(this,newsBean);
        rlv.setAdapter(adapter);
    }
}

//适配器:

public class MyAdapter extends RecyclerView.Adapter {

    public MyAdapter(Context context, NewsBean newsBean) {
        this.context = context;
        this.newsBean = newsBean;
    }

    Context context;
    NewsBean newsBean;


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        List data = newsBean.getData();
        NewsBean.DataBean dataBean = data.get(position);
        String title = dataBean.getTITLE();
        String imageurl = dataBean.getIMAGEURL();
        myViewHolder.tv.setText(title);
        Picasso.with(context).load(imageurl).placeholder(R.mipmap.ic_launcher_round).into(myViewHolder.iv);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv;
        private final ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
            iv = itemView.findViewById(R.id.iv);

        }
    }
}



//OnFinishListener :

public interface OnFinishListener {
    void onSuccess(NewsBean newsBean);
}


//modle 包:

  // ShowModle

public interface ShowModle {
    void getDate(OnFinishListener listener);
}

      

   //ShowModleImpl 

public class ShowModleImpl implements ShowModle {

    @Override
    public void getDate(final OnFinishListener listener) {
        String url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0";
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Gson gson = new Gson();
                NewsBean newsBean = gson.fromJson(responseString, NewsBean.class);
                //接口回调
                if (listener != null){
                    listener.onSuccess(newsBean);
                }

            }
        });


    }
}


//view 包:

 

    //ShowView 

public interface ShowView {
    void showData(NewsBean newsBean);
}


//presenter 包:

     //ShowPresenter 

public interface ShowPresenter {
    void relevance();
}

    //ShowPresentermpl 

public class ShowPresentermpl implements ShowPresenter,OnFinishListener {

    ShowView showView;
    private final ShowModle showModle;

    public ShowPresentermpl(ShowView showView) {
        this.showView = showView;
        showModle = new ShowModleImpl();
    }

    @Override
    public void relevance() {
        //P V 关联
        showModle.getDate(this);
    }


    @Override
    public void onSuccess(NewsBean newsBean) {
        //关联View
        showView.showData(newsBean);
    }
}



你可能感兴趣的:(MVP列表展示)