1.MainActivity
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private RadioGroup rg;
private ArrayList<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = findViewById(R.id.vp);
rg = findViewById(R.id.rg);
RadioButton rb1 = findViewById(R.id.rb);
RadioButton rb2 = findViewById(R.id.rb2);
RadioButton rb3 = findViewById(R.id.rb3);
//创建一个Fragment集合
list = new ArrayList<>();
Frag01 frag01 = new Frag01();
Frag02 frag02 = new Frag02();
Frag03 frag03 = new Frag03();
list.add(frag01);
list.add(frag02);
list.add(frag03);
rg.check(list.get(0).getId());
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
rg.check(rg.getChildAt(i).getId());
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb:
vp.setCurrentItem(0);
break;
case R.id.rb2:
vp.setCurrentItem(1);
break;
case R.id.rb3:
vp.setCurrentItem(2);
break;
}
}
});
}
}
3.OKHttpUtils
public class OkHttpUtils {
private static OkHttpUtils okHttpUtils = null;
private HttpLoggingInterceptor loggingInterceptor;
private OkHttpUtils(){
}
public static OkHttpUtils getInstance(){
//同步锁
if(okHttpUtils==null){
synchronized (OkHttpUtils.class){
if(okHttpUtils==null){
okHttpUtils = new OkHttpUtils();
}
}
}
return okHttpUtils;
}
//创建拦截器
public Interceptor getintercepter(){
loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("mmm",message);
}
});
return loggingInterceptor;
}
public void doGet(String url, Callback callback){
//解析
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(getintercepter())
.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
2.Z_View
public class Z_View extends RelativeLayout {
public Z_View(Context context) {
super(context);
}
public Z_View(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom,this);
}
public Z_View(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
3.Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
final static int TYPE_ONE = 0;
final static int TYPE_TWO = 1;
Context context;
ArrayList<Result> result;
public MyAdapter(Context context, ArrayList<Result> result) {
this.context = context;
this.result = result;
}
// 获取
@Override
public int getItemViewType(int position) {
return position % 2;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// 使用ONE 等于i
if (TYPE_ONE == i){
// 找布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item1,null,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}else {
// 找布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item2,null,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
int itemViewType = getItemViewType(i);
Result result = this.result.get(i);
if (itemViewType==TYPE_ONE){
myViewHolder.item1_title1.setText(result.getCommodityName());
Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item1_image1);
}else {
myViewHolder.item2_title1.setText(result.getCommodityName());
Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item2_image1);
Glide.with(context).load(result.getMasterPic()).into(myViewHolder.item2_image2);
}
}
@Override
public int getItemCount() {
return result.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView item1_title1;
private final ImageView item1_image1;
private final TextView item2_title1;
private final ImageView item2_image1;
private final ImageView item2_image2;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
// 1
item1_title1 = itemView.findViewById(R.id.item1_title1);
item1_image1 = itemView.findViewById(R.id.item1_image1);
// 2
item2_title1 = itemView.findViewById(R.id.item2_title1);
item2_image1 = itemView.findViewById(R.id.item2_image1);
item2_image2 = itemView.findViewById(R.id.item2_image2);
}
}
}
4.ShowModel
public class ShowModel {
private String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=";
private String path="&count=10&page=";
private String xie = "";
private String page1 = "";
public interface onShowLisenter{
void onResult(String json);
}
public onShowLisenter showLisenter;
public void setShowLisenter(onShowLisenter onShowLisenter) {
this.showLisenter = onShowLisenter;
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String json = (String) msg.obj;
showLisenter.onResult(json);
break;
}
}
};
public void show(String trim,int page){
xie=trim;
page1=page+"";
OkHttpUtils.getInstance().doGet(url + xie + path + page1, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Log.i("xxxc",json);
Message message = new Message();
message.what=0;
message.obj=json;
handler.sendMessage(message);
}
});
}
}
5.ShowView
public interface ShowView {
void getViewData(String json);
}
6.ShowPresenter
public class ShowPresenter {
private final ShowModel showModel;
private final ShowView showView;
public ShowPresenter(ShowView view){
showModel = new ShowModel();
showView = view;
}
public void show(String trim, int page) {
showModel.show(trim,page);
showModel.setShowLisenter(new ShowModel.onShowLisenter() {
@Override
public void onResult(String json) {
showView.getViewData(json);
}
});
}
}
7.Frag01
public class Frag01 extends Fragment implements ShowView {
private RecyclerView rlv;
private SwipyRefreshLayout src;
private EditText seek;
private Button button;
private ShowPresenter presenter;
private String title = "板鞋";
private int page = 1;
private Handler handler = new Handler();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_01,null,false);
//找控件
rlv = view.findViewById(R.id.rlv);
//设置上拉下拉
src = view.findViewById(R.id.frag01_src);
//搜索控件
seek = view.findViewById(R.id.seek);
button = view.findViewById(R.id.sou);
//布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rlv.setLayoutManager(linearLayoutManager);
presenter = new ShowPresenter(this);
initData();
//设置颜色
src.setColorSchemeColors(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
//设置模式
src.setDirection(SwipyRefreshLayoutDirection.BOTH);
//设置sw 监听
src.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(int index) {
page = 1;
presenter.show(title,page);
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "刷新成功", Toast.LENGTH_SHORT).show();
src.setRefreshing(false);
}
},2000);
}
@Override
public void onLoad(int index) {
page++;
presenter.show(title,page);
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "加载成功", Toast.LENGTH_SHORT).show();
src.setRefreshing(false);
}
},2000);
}
});
return view;
}
private void initData() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title = seek.getText().toString();
if (TextUtils.isEmpty(title)){
Toast.makeText(getActivity(), "请输入要搜索的内容", Toast.LENGTH_SHORT).show();
}else {
presenter.show(title,page);
}
}
});
}
@Override
public void getViewData(String json) {
Gson gson = new Gson();
JsonBean fromJson = gson.fromJson(json, JsonBean.class);
ArrayList<Result> result = fromJson.getResult();
MyAdapter myAdapter = new MyAdapter(getContext(), result);
rlv.setAdapter(myAdapter);
}
}
8.Layout
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:layout_weight="1"
/>
<RadioButton
android:id="@+id/rb2"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二页"
android:layout_weight="1"
/>
<RadioButton
android:id="@+id/rb3"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的"
android:layout_weight="1"
/>
</RadioGroup>
</LinearLayout>
frag01
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<com.bawei.weeklianxi.Z_View
android:layout_width="match_parent"
android:layout_height="50dp" />
<com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout
android:id="@+id/frag01_src"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout>
</LinearLayout>
custom
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<EditText
android:id="@+id/seek"
android:layout_width="0dp"
android:hint="请输入要搜索的商品"
android:gravity="center"
android:layout_weight="1"
android:layout_height="50dp" />
<Button
android:id="@+id/sou"
android:text="搜索"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</LinearLayout>
item1
<?xml version="1.0" encoding="utf-8"?>`在这里插入代码片`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item1_image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"
/>
<TextView
android:id="@+id/item1_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈"
/>
</LinearLayout>
item2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item2_image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"
/>
<ImageView
android:id="@+id/item2_image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"
/>
<TextView
android:id="@+id/item2_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈"
/>
</LinearLayout>