bottomtabbar实现底部导航+fragment切换+RecyclerView+二级联动+自定义LinearLayout

MainActivity
package com.bawei.zhoukao520;

import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.bawei.zhoukao520.fragments.Fragment1;
import com.bawei.zhoukao520.fragments.Fragment2;
import com.bawei.zhoukao520.fragments.Fragment3;
import com.hjm.bottomtabbar.BottomTabBar;

public class MainActivity extends FragmentActivity {
    private BottomTabBar mb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化组件
        mb=(BottomTabBar)findViewById(R.id.bottom_tab_bar);
        mb.init(getSupportFragmentManager())
                .setImgSize(50,50)
                .setFontSize(20)
                .setTabPadding(4,6,10)
                .setChangeColor(Color.RED,Color.DKGRAY)
                .addTabItem("首页",R.drawable.ic_launcher_background, Fragment1.class)
                .addTabItem("分类",R.drawable.ic_launcher_background, Fragment2.class)
                .addTabItem("购物车",R.drawable.ic_launcher_background, Fragment3.class)
                .isShowDivider(false)
                .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
                    @Override
                    public void onTabChange(int position, String name) {

                    }
                });
    }
}

adpter
package com.bawei.zhoukao520.adpter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bawei.zhoukao520.R;
import com.bawei.zhoukao520.bean.UserBean;


import java.util.ArrayList;
import java.util.List;

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyVh> {

    private Context context;
    private List<UserBean.ResultBean> list;

    public LeftAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<UserBean.ResultBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.left_item,parent,false);
        return new MyVh(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyVh holder, final int position) {
        holder.title.setText(list.get(position).name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclicklisener!=null){
                    onclicklisener.click(list.get(position).id);
                }
            }
        });
    }

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

    public class MyVh extends RecyclerView.ViewHolder {

        private final TextView title;

        public MyVh(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }
    private onclicklisener onclicklisener;
    public void setonItemClickLisener(onclicklisener onclicklisener){
        this.onclicklisener = onclicklisener;
    }

    public interface onclicklisener{
        void click(String cid);
    }

}

package com.bawei.zhoukao520.adpter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bawei.zhoukao520.R;
import com.bawei.zhoukao520.bean.RightBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class RightAdapter extends RecyclerView.Adapter<RightAdapter.MyVh> {

    private Context context;
    private List<RightBean.ResultBean> list;

    public RightAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<RightBean.ResultBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.right_item,parent,false);
        return new MyVh(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyVh holder, int position) {
        holder.title.setText(list.get(position).name);
        Glide.with(context).load("http://365jia.cn/uploads/appletrecommend/201804/5ad6ba6c6bb8b.jpg").into(holder.imageView);
    }
//http://365jia.cn/uploads/appletrecommend/201804/5ad6ba6c6bb8b.jpg
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyVh extends RecyclerView.ViewHolder {


        private final ImageView imageView;
        private final TextView title;

        public MyVh(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
        }
    }
}

Api
package com.bawei.zhoukao520.api;

public class Api {

    public static final String  url = "http://172.17.8.100/small/commodity/v1/findFirstCategory";

    public static final  String  url1 ="http://172.17.8.100/small/commodity/v1/findSecondCategory?firstCategoryId=";


}

bean
package com.bawei.zhoukao520.bean;

import java.util.List;

public class RightBean {


    public List<ResultBean> result;
    public static class ResultBean {
        public String id;
        public String name;


    }
}

package com.bawei.zhoukao520.bean;

import java.util.List;

public class UserBean {

    public List<ResultBean> result;

    public static class ResultBean {

        public String id;
        public String name;


    }
}

fragments
package com.bawei.zhoukao520.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


import com.bawei.zhoukao520.R;
import com.bawei.zhoukao520.adpter.LeftAdapter;
import com.bawei.zhoukao520.adpter.RightAdapter;
import com.bawei.zhoukao520.bean.RightBean;
import com.bawei.zhoukao520.bean.UserBean;
import com.bawei.zhoukao520.presenter.ClsPresenter;
import com.bawei.zhoukao520.view.ClsContart;
import com.google.gson.Gson;


import java.util.HashMap;

public class Fragment1 extends Fragment implements ClsContart.IClsView,LeftAdapter.onclicklisener {
    private RecyclerView mRightRv;
    private RecyclerView mLeftRv;
    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;
    private ClsPresenter clsPresenter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
        //找控件
        mRightRv = view.findViewById(R.id.right_rv);
        mLeftRv = view.findViewById(R.id.left_rv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLeftRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRightRv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        leftAdapter = new LeftAdapter(getActivity());
        rightAdapter = new RightAdapter(getActivity());
        clsPresenter = new ClsPresenter(this);
        clsPresenter.ClsLeftList(new HashMap<String, String>());

    }

    private void getLeft(String cid) {
        HashMap<String, String> params = new HashMap<>();
        params.put("firstCategoryId", cid);
        clsPresenter.ClsRightList(params);
    }


    @Override
    public void LeftSuccess(String result) {
        UserBean userBean = new Gson().fromJson(result, UserBean.class);
        leftAdapter.setList(userBean.result);
        leftAdapter.setonItemClickLisener(this);
        mLeftRv.setAdapter(leftAdapter);
    }

    @Override
    public void LeftFailure(String msg) {

    }

    @Override
    public void RightSuccess(String result) {
        RightBean rightBean = new Gson().fromJson(result, RightBean.class);
        rightAdapter.setList(rightBean.result);
        mRightRv.setAdapter(rightAdapter);
    }

    @Override
    public void RightFailure(String msg) {

    }

    @Override
    public void click(String cid) {
        getLeft(cid);
    }

    class Mlayout extends LinearLayout implements View.OnClickListener {
        View view;
        EditText editText;
        Button button;

        public Mlayout(Context context) {
            super(context);
            view = LayoutInflater.from(context).inflate(R.layout.activity_main, this);
            editText = view.findViewById(R.id.edit_id);
            editText.setOnClickListener(this);
            button = view.findViewById(R.id.but_id);
            button.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.edit_id:
                    break;
                case R.id.but_id:
                    break;
            }
        }
    }

}
package com.bawei.zhoukao520.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.zhoukao520.R;


public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);
        return view;
    }
}

package com.bawei.zhoukao520.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.zhoukao520.R;


public class Fragment3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment3, container, false);
        return view;
    }
}

model
package com.bawei.zhoukao520.model;

import com.bawei.zhoukao520.api.Api;
import com.bawei.zhoukao520.utils.OkHttpCallBack;
import com.bawei.zhoukao520.utils.OkHttpUtils;
import com.bawei.zhoukao520.view.ClsContart;

import java.util.HashMap;
import java.util.Map;

public class ClsModel implements ClsContart.IClsModel {


    private String key;
    private String value;

    @Override
    public void ClsLeftList(HashMap<String, String> params, final IClsModelCallBack iClsModelCallBack) {
        OkHttpUtils.getInstance().doGet(Api.url, new OkHttpCallBack() {
            @Override
            public void Success(String result) {
                iClsModelCallBack.Success(result);
            }

            @Override
            public void failure(String msg) {
                iClsModelCallBack.Failure(msg);
            }
        });
    }

    @Override
    public void ClsRightList(HashMap<String, String> params, final IClsModelCallBack iClsModelCallBack) {
        for (Map.Entry<String, String> map: params.entrySet()) {
            key = map.getKey();
            value = map.getValue();
        }
        String s = Api.url1 + value;
        OkHttpUtils.getInstance().doGet(s, new OkHttpCallBack() {
            @Override
            public void Success(String result) {
                iClsModelCallBack.Success(result);
            }

            @Override
            public void failure(String msg) {
                iClsModelCallBack.Failure(msg);
            }
        });
    }

    public interface IClsModelCallBack{
        void Success(String result);
        void Failure(String msg);
    }


}

presenter
package com.bawei.zhoukao520.presenter;


import com.bawei.zhoukao520.model.ClsModel;
import com.bawei.zhoukao520.view.ClsContart;

import java.util.HashMap;

public class ClsPresenter extends ClsContart.IClsPresenter {

    private ClsContart.IClsView iClsView;
    private ClsModel clsModel;

    public ClsPresenter(ClsContart.IClsView iClsView) {
        this.iClsView = iClsView;
        clsModel = new ClsModel();
    }

    @Override
    public void ClsLeftList(HashMap<String, String> params) {
        clsModel.ClsLeftList(params, new ClsModel.IClsModelCallBack() {
            @Override
            public void Success(String result) {
                iClsView.LeftSuccess(result);
            }

            @Override
            public void Failure(String msg) {
                iClsView.LeftFailure(msg);
            }
        });
    }

    @Override
    public void ClsRightList(HashMap<String, String> params) {
        clsModel.ClsRightList(params, new ClsModel.IClsModelCallBack() {
            @Override
            public void Success(String result) {
                iClsView.RightSuccess(result);
            }

            @Override
            public void Failure(String msg) {
                iClsView.RightFailure(msg);
            }
        });
    }
}

utils
package com.bawei.zhoukao520.utils;

public interface OkHttpCallBack {

    void Success(String result);
    void failure(String msg);

}

package com.bawei.zhoukao520.utils;

import android.os.Handler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtils {

    private static OkHttpUtils mInstance;
    private final OkHttpClient okHttpClient;
    private Handler handler = new Handler();
    public static OkHttpUtils getInstance() {
        if (mInstance==null){
            synchronized (OkHttpUtils.class){
                if (mInstance==null){
                    mInstance = new OkHttpUtils();
                }
            }
        }
        return mInstance;
    }

    private OkHttpUtils(){
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .writeTimeout(5,TimeUnit.SECONDS)
                .connectTimeout(5,TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .build();
    }
    public void doGet(String url, final OkHttpCallBack okHttpCallback){

        Request build = new Request.Builder().url(url).get().build();

        okHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        okHttpCallback.failure("网络异常");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code()==200){
                    final String string = response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            okHttpCallback.Success(string);
                        }
                    });
                }
            }
        });
    }
    public void dopost(String url, HashMap<String,String> params, final OkHttpCallBack callBack){
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String,String> map:params.entrySet()) {
            builder.add(map.getKey(),map.getValue());
        }
        RequestBody requestBody = builder.build();
        Request request = new Request.Builder().url(url).post(requestBody).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callBack!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.failure("网络请求失败");
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callBack!=null){
                    if (response.code()==200){
                        final String result = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                            callBack.Success(result);
                            }
                        });
                    }
                }
            }
        });

    }

}

view
package com.bawei.zhoukao520.view;




import com.bawei.zhoukao520.model.ClsModel;

import java.util.HashMap;

public interface ClsContart {

    public abstract class IClsPresenter{
        public abstract void ClsLeftList(HashMap<String,String> params);
        public abstract void ClsRightList(HashMap<String,String> params);
    }


    interface IClsModel{
        void ClsLeftList(HashMap<String, String> params, ClsModel.IClsModelCallBack iClsModelCallBack);
        void ClsRightList(HashMap<String, String> params, ClsModel.IClsModelCallBack iClsModelCallBack);
    }

    interface IClsView{
        void LeftSuccess(String result);
        void LeftFailure(String msg);
        void RightSuccess(String result);
        void RightFailure(String msg);

    }

}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">


    <com.hjm.bottomtabbar.BottomTabBar
        android:id="@+id/bottom_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </com.hjm.bottomtabbar.BottomTabBar>


</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:background="#0ff"
    android:orientation="vertical"
>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"

        android:orientation="horizontal">
        <EditText
            android:id="@+id/edit_id"
            android:layout_width="200dp"
            android:layout_height="50dp" />
        <Button
            android:id="@+id/but_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:id="@+id/left_rv"/>

<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="7"
android:id="@+id/right_rv"/>

    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0ff">

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0f0">

</LinearLayout>

left.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:text="jjjj"
        android:lines="1"
        android:maxLength="4"
        android:textSize="24sp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        />



</android.support.constraint.ConstraintLayout>
right.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="30dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"

        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:text="dsdsad"
        android:lines="1"
        android:maxLength="3"
        app:layout_constraintTop_toBottomOf="@id/image"
        app:layout_constraintLeft_toLeftOf="@id/image"
        app:layout_constraintRight_toRightOf="@id/image"
        android:layout_marginTop="10dp"
        android:textSize="14sp"
        />

</android.support.constraint.ConstraintLayout>
权限
    //okhttp网络请求框架
    implementation 'com.squareup.okhttp3:okhttp:3.12.1'
//    gson
    implementation 'com.google.code.gson:gson:2.8.5'
    //recyclerview的依赖
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    //黄油刀
    implementation  'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
//    glidet图片加载框架
    implementation 'com.github.bumptech.glide:glide:3.7.0'

//    ok拦截器
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
//底部导航栏
    implementation'com.hjm:BottomTabBar:1.1.1'

    //轮播
    implementation 'com.xhb:xbanner:1.5.1'
//   recycleView


    implementation 'com.android.support:recyclerview-v7:28.0.0'
}
//版本冲突
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'    //此处填写你项目使用的sdk版本
            }
        }
    }
权限
 <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值