一、依赖包
implementation 'com.hjm:BottomTabBar:1.1.1'
二、BottomTabBar
1、xml:
<com.hjm.bottomtabbar.BottomTabBar
android:id="@+id/bottomTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2、Activity
public class ShowActivity extends AppCompatActivity {
private BottomTabBar botab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
botab = findViewById(R.id.bottomTab);
botab.init(getSupportFragmentManager()).setImgSize(50,50)
.setFontSize(10)
.setTabPadding(4,6,10)
// .setBackgroundColor(Color.BLACK)
.setChangeColor(Color.RED,Color.BLACK)
// .setBackground(Color.BLACK)
.addTabItem("首页",R.drawable.fist,FirstFragment.class)
.addTabItem("街头",R.drawable.twono,TwoFragment.class)
.isShowDivider(false)
.setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
@Override
public void onTabChange(int position, String name) {
}
});
}
}
三、TopBar
1、xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SearchView
android:id="@+id/ser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:paddingLeft="30dp"
/>
<Button
android:id="@+id/left"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_weight="1"
/>
<Button
android:id="@+id/right"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignTop="@+id/left"
android:layout_alignParentEnd="true"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
2、Activity
public class TopBar extends RelativeLayout {
private Button left;
private final Button right;
private final TextView text;
setLeftAndRightButton listenr;
private SearchView serch;
public void setLeftAndRightButton(setLeftAndRightButton listenr){
this.listenr=listenr;
}
public interface setLeftAndRightButton{
void onleft();
void onright();
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.topbar,this);
left = findViewById(R.id.left);
serch = findViewById(R.id.ser);
right = findViewById(R.id.right);
text = findViewById(R.id.textView);
serch.setIconified(false);
serch.setIconifiedByDefault(false);
serch.onActionViewExpanded();
left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
listenr.onleft();
}
});
right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
listenr.onright();
}
});
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.fuTopBar);
int leftback = typedArray.getResourceId(R.styleable.fuTopBar_leftBackgroud, 0);
int rightback = typedArray.getResourceId(R.styleable.fuTopBar_rightBackgroud, 0);
String texttitle = typedArray.getString(R.styleable.fuTopBar_titleText);
float textsize = typedArray.getDimension(R.styleable.fuTopBar_titleTextSize, 0);
int textcolor = typedArray.getColor(R.styleable.fuTopBar_titleTextColor, 0);
int serche = typedArray.getResourceId(R.styleable.fuTopBar_search, 0);
typedArray.recycle();
left.setBackgroundResource(leftback);
right.setBackgroundResource(rightback);
// serch.setBackgroundDrawable(serche);
text.setText(texttitle);
text.setTextSize(textsize);
text.setTextColor(textcolor);
}
}
3、 values
<declare-styleable name="fuTopBar"
>
<attr name="leftBackgroud" format="reference"/>
<attr name="rightBackgroud" format="reference"/>
<attr name="titleText" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="search" format="reference"/>
</declare-styleable>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorWhite">#f5f5f5</color>
四、RecyclerView展示数据
注意:不要忘记到依赖哦!
( implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0')
1、引入TopBar和布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".frag.FirstFragment">
<com.example.bw.week2.utils.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:leftBackgroud="@drawable/artbai"
app:rightBackgroud="@drawable/fuckbai"
app:titleText="首页"
app:titleTextSize="15sp"
app:titleTextColor="@color/colorWhite"
android:background="@color/colorAccent"
/>
<android.support.v7.widget.RecyclerView
android:paddingTop="50dp"
android:id="@+id/recyview"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.RecyclerView
android:paddingTop="50dp"
android:id="@+id/recy"
android:orientation="vertical"
android:layout_width="500dp"
android:layout_marginLeft="100dp"
android:layout_height="match_parent"
/>
</FrameLayout>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
2、Activity
注意:不要忘记Gson的解析:implementation 'com.google.code.gson:gson:2.8.5'
public class FirstFragment extends Fragment implements ViewInter {
private MyAdapter myAdapter;
private MyAdapter1 myAdapter1;
private TopBar top;
private RecyclerView recy;
private RecyclerView recy1;
private bean bean;
private PresenterBase presenterBase;
@Override
public void onDestroy() {
super.onDestroy();
if (presenterBase!=null){
presenterBase.destory();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_first, container, false);
presenterBase = new PresenterBase(this);
presenterBase.getModel();
top = inflate.findViewById(R.id.topbar);
top.setLeftAndRightButton(new TopBar.setLeftAndRightButton() {
@Override
public void onleft() {
Toast.makeText(getActivity(),"我",Toast.LENGTH_SHORT).show();
}
@Override
public void onright() {
Toast.makeText(getActivity(),"他",Toast.LENGTH_SHORT).show();
}
});
recy = inflate.findViewById(R.id.recyview);
recy1 = inflate.findViewById(R.id.recy);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
recy1.setLayoutManager(gridLayoutManager);
recy.setLayoutManager(linearLayoutManager1);
return inflate;
}
@Override
public void getMviewdata(final String mviewData) {
Handler handler=new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
Databean databean = gson.fromJson(mviewData, Databean.class);
List<Databean.DataBean> list = databean.getData();
myAdapter = new MyAdapter(getActivity(), list);
recy.setAdapter(myAdapter);
}
});
}
@Override
public void getListview(final String viewda) {
Gson gson = new Gson();
bean = gson.fromJson(viewda, bean.class);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("v",""+viewda);
myAdapter1 = new MyAdapter1(getActivity(), bean);
recy1.setAdapter(myAdapter1);
}
});
}
}
3、Adapter
注意:使用Glide加载图片的时候加入两个解析
( implementation 'jp.wasabeef:glide-transformations:3.0.1'
implementation 'com.github.bumptech.glide:glide:4.8.0')
public class MyAdapter1 extends RecyclerView.Adapter<MyAdapter1.ViewHolder>{
private LayoutInflater mInflater;
Context context;
bean bean;
public MyAdapter1(Context context, bean bean) {
this.context =context;
this.bean=bean;
mInflater=LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = mInflater.inflate(R.layout.listview2, viewGroup, false);
ViewHolder holder = new ViewHolder(inflate);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
// viewHolder.imag.setImageResource(list.get(i).getIcon());
// viewHolder.imag.setImageResource(data.get(i).getPcid());
// viewHolder.imag.setImageResource(bean.getData().get(i).getPcid());
viewHolder.tv1.setText(bean.getData().get(i).getName());
// Glide.with(context)
// .load(bean.getData().get(i).getPcid())
//
// .into(viewHolder.imag);
}
@Override
public int getItemCount() {
return bean.getData().size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView tv1;
ImageView imag;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv1= itemView.findViewById(R.id.textView2);
imag= itemView.findViewById(R.id.imageView);
}
}
}
五、MVP
1、model
interface:
public interface ModelInter {
void getData(String url);
void getList(String url);
}
class:
public class ModelBase implements ModelInter{
private final ModelInterface modelInterface;
public ModelBase(ModelInterface modelInterface) {
this.modelInterface =modelInterface;
}
@Override
public void getData(String url) {
OkHttp3.OkHttpGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
modelInterface.failed();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
modelInterface.successful(response.body().string());
}
});
}
@Override
public void getList(String url) {
OkHttp3.OkHttpGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
modelInterface.successful(response.body().string());
}
});
}
public interface ModelInterface{
void successful(String viewdata);
void failed();
}
}
2、presenter
interface:
public interface PresenterInter {
void getModel();
void getList();
}
class:
public class PresenterBase implements PresenterInter,ModelBase.ModelInterface {
private FirstFragment firstFragment;
private ModelBase modelBase;
public PresenterBase(FirstFragment firstFragment) {
this.firstFragment =firstFragment;
modelBase = new ModelBase(this);
}
@Override
public void getModel() {
modelBase.getData(Api.shopList);
}
@Override
public void getList() {
modelBase.getList(Api.List);
}
@Override
public void successful(String viewdata) {
Log.i("dfd",""+viewdata);
firstFragment.getMviewdata(viewdata);
firstFragment.getListview(viewdata);
}
@Override
public void failed() {
}
public void destory(){
if (firstFragment!=null){
firstFragment=null;
}
}
}
3、view
interface:
public interface ViewInter {
void getMviewdata(String mviewData);
void getListview(String viewda);
}
六、工具类
1、OKHttp的get请求以及拦截器
注意:不要忘记到依赖哦!
( implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0')
public class OkHttp3 {
private static Request get;
private static OkHttpClient okHttpClient;
private static RequestBody build;
//拦截器
// private Interceptor getAppInterceptor(){
// Interceptor interceptor = new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request();
// Log.i("++++++","拦截前");
// Response proceed = chain.proceed(request);
// Log.i("+++++","拦截后");
// return proceed;
// }
// };
// return interceptor;
// }
// private OkHttp3(){
// File file = new File(Environment.getExternalStorageDirectory(), "cachell");
// client = new OkHttpClient().newBuilder()
// .readTimeout(3000, TimeUnit.SECONDS)
// .connectTimeout(3000, TimeUnit.SECONDS)
// //拦截器
// .addInterceptor(getAppInterceptor())
// .cache(new Cache(file, 10 * 1024))
// .build();
// }
//okhttp get的异步请求方法
public static void OKHttpget(String url, final setData data){
//创建okHttpClient
okHttpClient = new OkHttpClient();
//创建reuest
get = new Request.Builder()
.url(url)
.method("GET", null)
.build();
//
okHttpClient.newCall(get).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.i("数据:", "onResponse: ===="+string);
data.getData(string);
}
});
}
public interface setData{
void getData(String data);
}
}
2、Api
public class Api {
// public final static String ShopList="http://www.zhaoapi.cn/product/getCatagory";
public final static String shopList="http://www.zhaoapi.cn/product/getCatagory";
public final static String List="http://www.zhaoapi.cn/product/getProductCatagory";
// http://www.zhaoapi.cn/product/getProductCatagory
}