<EditText
android:id="@+id/editText0"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="150dp"
android:hint="请输入手机号"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:hint="请输入密码"
android:password="true"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="记住密码"
/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
android:layout_marginRight="60dp"
android:text="快速注册"
android:textSize="14dp" />
</RelativeLayout>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="100dp"
android:text="登录"
android:textSize="18dp"
/>
‘
以上是登录页面的布局
<?xml version="1.0" encoding="utf-8"?><EditText
android:id="@+id/editText0"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="150dp"
android:hint="请输入手机号"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:hint="请输入密码"
android:password="true"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
android:layout_marginRight="60dp"
android:text="已有账户? 立即登录"
android:textSize="14dp" />
</RelativeLayout>
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="100dp"
android:text="注册"
android:textSize="18dp"
/>
这是注册页面的布局
这是展示数据用的RecyclerView的布局
item布局自定义位置
public class HttpUtils {
//创建单例
public OkHttpClient okHttpClient;
private HttpUtils(){
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.build();
}
public static HttpUtils getInstance(){
return HttpViewHolder.instance;
}
private static class HttpViewHolder{
private static HttpUtils instance = new HttpUtils();
}
private CallBackData mcallBack;
public void getData(String path, final Class<T> tClass, CallBackData callBackData){
this.mcallBack = callBackData;
Request request = new Request.Builder()
.url(path)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message message = handler.obtainMessage();
message.what=0;
message.obj = e.getMessage();
handler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
T t = gson.fromJson(string, tClass);
Message message = handler.obtainMessage();
message.what=1;
message.obj = t;
handler.sendMessage(message);
}
});
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1){
T t = (T) msg.obj;
mcallBack.onSuccess(t);
}else {
String err = (String) msg.obj;
mcallBack.OnFail(err);
}
}
};
//定义一个接口
public interface CallBackData<D>{
void onSuccess(D d);
void OnFail(String msg);
}
}
这是展示数据的封装Http类
这是Base类Presenter层
这是继承BasePresenter的MainPresenter层实现了IMainView接口类(下面有)
上面两个就是IMainView 继承IBaseView类
public class MainActivity extends BaseActivity implements IMainView {
private RecyclerView recyclerView;
private ShopAdapter shopAdapter;
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
@Override
protected int setSelfView() {
return R.layout.activity_main;
}
@Override
protected void initView() {
recyclerView = findViewById(R.id.recyclerView);
}
@Override
protected void initData() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
recyclerView.addItemDecoration(new DividerItemDecoration(getBaseContext(),LinearLayoutManager.VERTICAL));
recyclerView.setLayoutManager(linearLayoutManager);
shopAdapter = new ShopAdapter(getBaseContext());
recyclerView.setAdapter(shopAdapter);
mainPresenter = new MainPresenter();
mainPresenter.setView(this);
mainPresenter.loadFormDataNet();
}
@Override
protected void onDestroy() {
super.onDestroy();
mainPresenter.detachView();
}
@Override
public void CallBackSuccess(JavaBean javaBean) {
List<JavaBean.ResultBean.PzshBean.CommodityListBeanX> data = javaBean.getResult().getPzsh().getCommodityList();
shopAdapter.setData(data);
}
@Override
public void CallBackFail(String errMsg) {
}
}
这个就是展示数据的Activity继承了BaseActivity
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
private Context context;
private List<JavaBean.ResultBean.PzshBean.CommodityListBeanX> mList = new ArrayList<>();
public ShopAdapter(Context context) {
this.context = context;
}
public void setData(List<JavaBean.ResultBean.PzshBean.CommodityListBeanX> list){
mList.addAll(list);
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item,null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.textView.setText(mList.get(i).getCommodityName());
Glide.with(context).load(mList.get(i).getMasterPic()).into(myViewHolder.imageView);
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
这个是adapter写的展示数据