Fragment(碎片化布局)
使用场景描述:
在Activity中,需要有一个无论怎么切换依旧屹立不动的底部导航,这时候就可以用到Fragment了,这个东西就相当于在你的Activity中嵌入了一个可以随意切换的页面。
1.xml部分
<?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:orientation="vertical"
android:background="#f1f1f1">
//
<FrameLayout
android:id="@+id/frameOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"/>
//底部导航
<LinearLayout
android:id="@+id/tabbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/tabOne"
android:layout_width="32dp"
android:layout_height="32dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="btn1"
android:layout_below="@id/tabOne"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/tabTwo"
android:layout_width="32dp"
android:layout_height="32dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="btn1"
android:layout_below="@+id/tabTwo"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/tabThree"
android:layout_width="32dp"
android:layout_height="32dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="btn1"
android:layout_below="@+id/tabThree"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
2.java部分
package com.example.ylb;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import com.example.ylb.MainFragment.IndexFragment;
import com.example.ylb.MainFragment.MyPageFragment;
import com.example.ylb.MainFragment.OldcarFragment;
public class MainActivity extends AppCompatActivity {
private IndexFragment indexFragment = new IndexFragment();//实例化indexFragment
private OldcarFragment oldcarFragment = new OldcarFragment();//实例化oldcarFragment
private MyPageFragment myPageFragment = new MyPageFragment();//实例化myPageFragment
private RelativeLayout mTab1;
private RelativeLayout mTab2;
private RelativeLayout mTab3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//把indexFragment添加到Acticity中
getSupportFragmentManager().beginTransaction().add(R.id.frameOne, indexFragment).commitNowAllowingStateLoss();
//跳转index
mTab1 = (RelativeLayout) findViewById(R.id.tab1);
mTab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameOne,indexFragment).commitNowAllowingStateLoss();
}
});
//跳转oldcar
mTab2 = (RelativeLayout) findViewById(R.id.tab2);
mTab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameOne,oldcarFragment).commitNowAllowingStateLoss();
}
});
//跳转mypage
mTab3 = (RelativeLayout) findViewById(R.id.tab3);
mTab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameOne,myPageFragment).commitNowAllowingStateLoss();
}
});
}
}
3.Fragment部分
package com.example.ylb.MainFragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class OldcarFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//R.layout.oldcar_fragment是你写好的布局文件
view = inflater.inflate(R.layout.oldcar_fragment,container,false);
//对recycleview进行配置
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
加载网络图片框架Glide
1.引用
implementation 'com.github.bumptech.glide:glide:4.5.0'
2.使用(只放一个基础使用的代码,如果需要更深层次的优化请百度)
//创建变量
private ImageView banner;
private String bannerSrc = "图片链接放这里";
//使用
banner = view.findViewById(R.id.banner);
Glide.with(this).load(bannerSrc).into(banner);
3.设置图片显示带圆角或正圆
//设置图片圆角角度
RoundedCorners roundedCorners = new RoundedCorners(50);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
RequestOptions mRequestOptions = RequestOptions.circleCropTransform()
.diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
.skipMemoryCache(true);//不做内存缓存
portrait = findViewById(R.id.portrait);
//apply里写options就是圆角
//apply里写mRequestOptions就是正圆
Glide.with(this).load(portraitSrc).apply(mRequestOptions).into(portrait);
网络请求框架OkHttp3
1.设计思路
首先我写了一个工具类,里面是一些在发起请求时需要执行的代码,其次是一个抽象类,这个抽象类的作用就是写,在请求成功之后需要执行的代码。
2.工具类(因为我是前端自学原生所以我习惯叫它Ajax)
package com.example.ylb.CustomEntityClass;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.example.ylb.CustomEntityClass.AjaxResponse.AjaxResponse;
import com.google.gson.Gson;
import org.json.JSONException;
import java.io.IOException;
import java.util.HashMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static android.support.constraint.Constraints.TAG;
public class Ajax{
protected static final int SUCCESS_MESSAGE = 0;
protected static final int FAILURE_MESSAGE = 1;
public void ajaxRequest(final String url, final Object data,final AjaxResponse ajaxResponse){
//请求体类型
MediaType mediaType = MediaType.parse("application/json");
//请求体结构(如果后端限制了请求体结构,就按照结构写,没有限制可以不要这一步)
HashMap<String, Object> jsonObject = new HashMap<>();
jsonObject.put("info", data);
jsonObject.put("auth", "");
//转换成JSON字符串
Gson gson = new Gson();
String userJson = gson.toJson(jsonObject);
//创建请求
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
//发起请求
final Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(mediaType, userJson))
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败回调
Log.d(TAG, "onFailure: ");
}
@Override
public void onResponse(final Call call,final Response response) throws IOException {
//请求成功回调
//response.body().bytes() 可以获取到后台传回的参数,再通过String responsa = new String(tmp)转换成字符串
byte[] tmp = response.body().bytes();
final String responsa = new String(tmp);
Log.d(TAG, "call: " + call);
Log.d(TAG, "onResponse: " + responsa);
//把需要执行的逻辑代码扔回主线程
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
try {
//抽象类,需要执行的逻辑代码
//看到这里没看懂的回到上面看ajaxRequest方法的形参
ajaxResponse.onResponse(call,responsa);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
}
3.抽象类
package com.example.ylb.CustomEntityClass.AjaxResponse;
import org.json.JSONException;
import okhttp3.Call;
public abstract class AjaxResponse {
public abstract void onResponse(Call call, String responsa) throws JSONException;
}
4.使用
//发起请求
private void initData() {
//这个是需要传回的数据,我这里用的是pojo类,你也可以自己写一个对象
sell_car_info sellcarinfo = new sell_car_info();
sellcarinfo.old_and_new = 0;
sellcarinfo.pageSize = 20;
sellcarinfo.pageNum = 1;
//实例化工具类
Ajax $ = new Ajax();
//使用工具类里的方法
$.ajaxRequest("请求地址", sellcarinfo, new AjaxResponse() {
@Override
public void onResponse(Call call, String responsa) throws JSONException {
//逻辑代码
JSONObject object = new JSONObject(responsa);
JSONArray data = object.getJSONArray("data");
Log.d(TAG,"data:"+object);
for (int i=0;i<data.length();i++){
JSONObject oj = data.getJSONObject(i);
Log.d(TAG,"type_name:"+oj.getString("type_name"));
}
}
});
}
持续更新中...