标签:初始化 activity tab class pull inter 布局管理器 api thold
///适配器
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.TextHolder>{
Context mContext;
List<HomeBean.DataBean> homeBean;
//构造函数
public HomeAdapter(Context mcontext,List<HomeBean.DataBean> homeBean) {
this.homeBean=homeBean;
this.mContext=mcontext;
}
///////////////////////////////////////////////////////////////////////////
//创建视图
@NonNull
@Override
public TextHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//获取子布局
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.item_layout, parent, false);
return new TextHolder(mContext,view);
}
//绑定数据和视图
@Override
public void onBindViewHolder(TextHolder textHolder, int position) {
String mtext = homeBean.get(position).getTitle();
String mpath = homeBean.get(position).getImages().split("\\|")[0];
textHolder.bindText(mtext,mpath);
}
//统计条目数量
@Override
public int getItemCount() {
return homeBean.size();
}
//通过内部类
public static class TextHolder extends RecyclerView.ViewHolder{
private TextView mTextView;
private ImageView mImageView;
private Context context;
public TextHolder(Context mcontext,View itemView) {
super(itemView);
this.context=mcontext;
mTextView = (TextView) itemView.findViewById(R.id.list_items);
mImageView=(ImageView) itemView.findViewById(R.id.list_items_iv);
}
public void bindText(String mText,String path){
mTextView.setText(mText);
Glide.with(context).load(path).into(mImageView);
}
}
}
//Model层
public interface ImodelHome {
public void callBackSuccess(HomeBean hbean);
public void callBackFailure(int code);
}
public class ModelHome {
public static final String TAG="ModelHome";
public void getData( final ImodelHome ModelListener){
// 拦截器addInterceptor.addInterceptor(new LogInterceptor())
OkHttpClient builder = new OkHttpClient.Builder().build();
final Request request = new Request.Builder().url("https://www.zhaoapi.cn/product/getProducts?pscid=1").build();
builder.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
ModelListener.callBackFailure(1);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
Log.i(TAG, "onResponse: "+result.toString());
Gson gson=new Gson();
HomeBean qbean = gson.fromJson(result, HomeBean.class);
ModelListener.callBackSuccess(qbean);
}
});
}
}
//Present层**********
public class HomePresenter implements IhomePresenter{
private IhomeView ihomeView;
private ModelHome modelHome;
public HomePresenter(IhomeView ihomeView) {
this.ihomeView=ihomeView;
this.modelHome=new ModelHome();
}
public void getdatas() {
modelHome.getData(new ImodelHome() {
@Override
public void callBackSuccess(HomeBean hbean) {
ihomeView.callBackSuccess(hbean);
}
@Override
public void callBackFailure(int code) {
ihomeView.callBackFailure(code);
}
});
}
@Override
public void ondestroys() {
if (ihomeView!=null){
ihomeView=null;
}
}
public interface IhomePresenter {
void ondestroys();
}
//View层
public interface IhomeView {
public void callBackSuccess(HomeBean qbean);
public void callBackFailure(int code);
}
//主界面
public class MainActivity extends AppCompatActivity implements IhomeView{
PullToRefreshScrollView mpullto;
RecyclerView mrecy;
HomeAdapter homeAdapter;
HomePresenter homePresenter;
private List<HomeBean.DataBean> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
//初始化view
public void init(){
//初始化presenter层
homePresenter=new HomePresenter(this);
//调起presenter方法
homePresenter.getdatas();
mpullto=findViewById(R.id.pull_to_refresh);
mrecy= findViewById(R.id.recycleview);
//加载recycleview 布局管理器 3种
mrecy.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mpullto.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
//刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
homePresenter.getdatas();
mpullto.onRefreshComplete();
}
//加载更多
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
homePresenter.getdatas();
mpullto.onRefreshComplete();
}
});
}
@Override
public void callBackSuccess(final HomeBean qbean) {
data = qbean.getData();
if(data!=null){
//子线程切换中线程
runOnUiThread(new Runnable() {
@Override
public void run() {
//初始化适配器
homeAdapter=new HomeAdapter(MainActivity.this, data);
mrecy.setAdapter(homeAdapter);
mpullto.onRefreshComplete();
}
});
}
}
@Override
public void callBackFailure(int code) {
}
}
******************************************************************************
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
******************************************************
内容
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_items"
android:text="@string/app_name"
android:textSize="16sp"/>
<ImageView
android:id="@+id/list_items_iv"
android:layout_width="100dp"
android:layout_height="100dp" />
标签:初始化 activity tab class pull inter 布局管理器 api thold
原文地址:https://www.cnblogs.com/xiaxinxin/p/9315888.html