Base
//接口
App
context/public class MyApi {
public static String url(String keyword){
return “http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=”+keyword+"&page=1&count=5";
}
}
//Volley
model
p层
MainActivity的
:
public class MainActivity extends BaseActivity implements IContract.IView ,MyView.oncall{
private RecyclerView re_view;
private MyView myview;
private FlowLayout flow;
@Override
protected void initData() {
String str = URLEncoder.encode("板鞋");
MyPresenter myPresenter = (MyPresenter) p;
myPresenter.Success(MyApi.url(str));
}
@Override
protected void initView() {
re_view = findViewById(R.id.re_view);
re_view.setLayoutManager(new LinearLayoutManager(this));
myview = findViewById(R.id.myview);
myview.setCall(this);
flow = findViewById(R.id.flow);
}
@Override
protected BasePresenter initPresenter() {
return new MyPresenter();
}
@Override
protected int initLayout() {
return R.layout.activity_main;
}
@Override
public void Success(MyBean myBean) {
List<MyBean.ResultBean> mlist = myBean.getResult();
MyAdapter myAdapter = new MyAdapter(mlist, MainActivity.this);
re_view.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
public void onclick() {
String ed = myview.ed_one.getText().toString();
flow.addTag(ed);
MyPresenter myPresenter = (MyPresenter) p;
myPresenter.Success(MyApi.url(ed));
}
}
adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<MyBean.ResultBean> mlist;
private int oneHolder = 1;
private int twoHodel = 2;
Context context;
private oneHolder holder;
MainActivity mainActivity;
public MyAdapter(List<MyBean.ResultBean> mlist, Context context) {
this.mlist = mlist;
this.context = context;
mainActivity = (MainActivity) context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder holder = null;
switch (i){
case 1:
View view = LayoutInflater.from(context).inflate(R.layout.one_layout,null);
holder = new oneHolder(view);
break;
case 2:
View view1 = LayoutInflater.from(context).inflate(R.layout.two_layout,null);
holder = new twoHodel(view1);
break;
default:
break;
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof oneHolder){
Glide.with(context)
.load(mlist.get(i).getMasterPic())
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH)
.placeholder(R.mipmap.ic_launcher_round)
.into(((oneHolder) viewHolder).imageView);
((oneHolder) viewHolder).textView1.setText(mlist.get(i).getCommodityName()+"");
((oneHolder) viewHolder).textView2.setText(mlist.get(i).getCommodityId()+"");
((oneHolder) viewHolder).lin1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Main2Activity.class);
mainActivity.startActivity(intent);
}
});
}else if (viewHolder instanceof twoHodel){
Glide.with(context)
.load(mlist.get(i).getMasterPic())
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH)
.placeholder(R.mipmap.ic_launcher_round)
.into(((twoHodel) viewHolder).imageView2);
((twoHodel) viewHolder).textView_one.setText(mlist.get(i).getCommodityName()+"");
((twoHodel) viewHolder).textView_two.setText(mlist.get(i).getCommodityId()+"");
((twoHodel) viewHolder).lin2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,Main2Activity.class);
mainActivity.startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
return mlist.size();
}
public class oneHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView1;
TextView textView2;
LinearLayout lin1;
public oneHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img);
textView1 = itemView.findViewById(R.id.one);
textView2 = itemView.findViewById(R.id.two);
lin1 = itemView.findViewById(R.id.lin1);
}
}
public class twoHodel extends RecyclerView.ViewHolder{
ImageView imageView2;
TextView textView_one;
TextView textView_two;
RelativeLayout lin2;
public twoHodel(@NonNull View itemView) {
super(itemView);
imageView2 = itemView.findViewById(R.id.img2);
textView_one = itemView.findViewById(R.id.re1);
textView_two = itemView.findViewById(R.id.re2);
lin2 = itemView.findViewById(R.id.lin2);
}
}
@Override
public int getItemViewType(int position) {
int commodityId = mlist.get(position).getCommodityId();
if (commodityId%2==0){
return oneHolder;
}else {
return twoHodel;
}
}
}
//自定义view
流失布局
public class FlowLayout extends ViewGroup {
private Context context;
private int pixels;//屏幕宽度
// private dlete ondelete;
public FlowLayout(Context context) {
super(context);
this.context = context;
init(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init(context);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init(context);
}
public void init(Context context) {
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
pixels = displayMetrics.widthPixels;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int left = 0;
int top = 0;
int right = 0;
int button = 0;
int sprce = 0;
for (int j = 0; j < childCount; j++) {
//获取某个子view
View childAt = getChildAt(j);
//主动测量view的宽、高,否则获取的宽度和高度为0
childAt.measure(0, 0);
//获取测量的宽度
int measuredWidth = childAt.getMeasuredWidth();
//获取测量的高度
int measuredHeight = childAt.getMeasuredHeight();
//计算坐标
left = right + sprce;
right = left + measuredWidth;
if (right > pixels) {
left = sprce;
right = left + measuredWidth;
top = button + sprce;
}
button = measuredHeight + top;
//摆放子view
childAt.layout(left, top, right, button);
}
}
public void addTag(String tag) {
final TextView textView = new TextView(context);
textView.setText(tag);
textView.setTextColor(Color.RED);
textView.setTextSize(20);
textView.setPadding(10, 10, 10, 10);
textView.setBackgroundColor(Color.parseColor("#fff5ee"));
addView(textView);
}
}
WEbview交互:
public class Main2Activity extends AppCompatActivity {
private WebView web_view;
private Button bu_one;
private Button bu_two;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
web_view = findViewById(R.id.web_view);
bu_one = findViewById(R.id.bu_one);
bu_two = findViewById(R.id.bu_two);
//允许加载网页
web_view.getSettings().setJavaScriptEnabled(true);
//加载网页
web_view.loadUrl("https://abnerming8.github.io/abnerming.html");
//在本应用加载h5的页面
bu_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
web_view.loadUrl("javascript:changeColor('#f00')");
}
});
//web_view.setWebViewClient(new WebViewClient());
web_view.setWebChromeClient(new WebChromeClient());
bu_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
web_view.loadUrl("javascript:toast()");
}
});
web_view.addJavascriptInterface(new adds(),"android");
}
class adds{
@JavascriptInterface
void show(){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, "", Toast.LENGTH_SHORT).show();
}
});
}
}
}