前言:umeng的配置
app配置
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
UMConfigure.setLogEnabled(true);
UMConfigure.init(this,"5a12384aa40fa3551f0001d1"
,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad", "http://sns.whalecloud.com");
}
}
<2>WXEntryActivity
public class WXEntryActivity extends WXCallbackActivity {
}
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:gravity="center"
android:textSize="30dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:hint="请输入密码"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="登录"
/>
<Button
android:text="分享"
android:onClick="qqshare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="100dp"
android:gravity="center"
android:layout_weight="1">
<ImageView
android:onClick="qqLogin"
android:src="@drawable/qq"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_weight="1"/>
<ImageView
android:onClick="weiXinLogin"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/weixin"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
2.mainactivity
public class MainActivity extends AppCompatActivity implements LoginView {
private String TAG = this.getClass().getSimpleName();
private Button login;
private LoginPersenter loginPersenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = findViewById(R.id.login);
loginPersenter = new LoginPersenter(this);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击登录
loginPersenter.login("xn", "0228");
}
});
}
//QQ分享
public void qqshare(View view) {
//6.0以上主动请求权限
if (Build.VERSION.SDK_INT >= 23) {
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this, mPermissionList, 123);
}
UMImage umImage = new UMImage(MainActivity.this, R.mipmap.ic_launcher);
UMWeb web = new UMWeb("https://www.baidu.com/");
web.setTitle("This is music title");//标题
web.setThumb(umImage); //缩略图
web.setDescription("my description");//描述
//开始分享
new ShareAction(MainActivity.this).withText("hello").withMedia(web)
.withMedia(web)
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
//.share();无面板分享
.open();
}
//QQ登录
public void qqLogin(View view) {
UMShareConfig config = new UMShareConfig();
config.isNeedAuthOnGetUserInfo(true);
UMShareAPI.get(MainActivity.this).setShareConfig(config);
authorization(SHARE_MEDIA.QQ);
}
//微信登录
public void weiXinLogin(View view) {
UMShareConfig config = new UMShareConfig();
config.isNeedAuthOnGetUserInfo(true);
UMShareAPI.get(MainActivity.this).setShareConfig(config);
authorization(SHARE_MEDIA.WEIXIN);
}
//授权方法
private void authorization(final SHARE_MEDIA share_media) {
UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, share_media, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
Log.d(TAG, "onStart " + "授权开始");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
Log.w(TAG, "onComplete " + map + "授权完成");
//sdk是6.4.4的,但是获取值的时候用的是6.2以前的(access_token)才能获取到值,未知原因
/* String uid = map.get("uid");
String openid = map.get("openid");//微博没有
String unionid = map.get("unionid");//微博没有
String access_token = map.get("access_token");
String refresh_token = map.get("refresh_token");//微信,qq,微博都没有获取到
String expires_in = map.get("expires_in");
String name = map.get("name");
String gender = map.get("gender");
String iconurl = map.get("iconurl");*/
//Toast.makeText(MainActivity.this, "name=" + name + ",gender=" + iconurl, Toast.LENGTH_SHORT).show();
//遍历map集合
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
intent.putExtra("name", map.get("name"));
intent.putExtra("iconurl", map.get("iconurl"));
startActivity(intent);
finish();
//拿到信息去请求登录接口。。。*/
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
Log.d(TAG, "onError授权失败");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
Log.d(TAG, "onCancel授权取消");
}
});
}
//返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
//防止内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
UMShareAPI.get(this).release();
}
@Override
public void LoginSuccess(String cheng) {
Toast.makeText(MainActivity.this, "" + cheng, Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this, TwoActivity.class));
finish();
}
@Override
public void LoginError(String shi) {
Toast.makeText(MainActivity.this, shi, Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(List s) {
}
}
2.mvp死数据登录功能
//model层
public class LoginModel {
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
//new thread是子线程 不能更新UI
// handler是主线程 不能做耗时操作
public void Login(String name, String pwd, final CallBack callBack){
new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);
handler.post(new Runnable() {
@Override
public void run() {
// 主线程 更新UI
callBack.OnloginSuccess("登录成功");
}
});
} catch (Exception e) {
e.printStackTrace();
callBack.onLoginError("登录失败");
}
}
}.start();
}
}
view层
public interface LoginView {
void LoginSuccess(String cheng);
void LoginError(String shi);
void onSuccess(List<User.DataBean> list);
}
//controller层
//model层
public class LoginModel {
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
//new thread是子线程 不能更新UI
// handler是主线程 不能做耗时操作
public void Login(String name, String pwd, final CallBack callBack){
new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);
handler.post(new Runnable() {
@Override
public void run() {
// 主线程 更新UI
callBack.OnloginSuccess("登录成功");
}
});
} catch (Exception e) {
e.printStackTrace();
callBack.onLoginError("登录失败");
}
}
}.start();
}
}
封装的callback层
public interface CallBack {
void OnloginSuccess(String deng);
void onLoginError(String error);
}
3.recyclerview的使用
twoactivity
public class TwoActivity extends AppCompatActivity implements LoginView {
private ImageView imageView;
private TextView mingzi;
private RecyclerView recyclerView;
private List<User.DataBean> list = new ArrayList<>();
private String path = "http://www.xieast.com/api/news/news.php";
private MyRecyclerAdapter adapter;
private Button add;
private Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
imageView = findViewById(R.id.touxiang);
mingzi = findViewById(R.id.mingzi);
recyclerView = findViewById(R.id.recyclerview);
add = findViewById(R.id.add);
delete = findViewById(R.id.jian);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String iconurl = intent.getStringExtra("iconurl");
imageView.setVisibility(View.VISIBLE);
mingzi.setText(name);
Picasso.with(TwoActivity.this).load(iconurl).into(imageView);
// OkHttpUtils utils = new OkHttpUtils();
NetProcenter netProcenter = new NetProcenter(this);
netProcenter.NetHttps(path);
}
@Override
public void LoginSuccess(String cheng) {
}
@Override
public void LoginError(String shi) {
}
@Override
public void onSuccess(final List<User.DataBean> list) {
list.addAll(list);
// adapter.setList(list);
adapter = new MyRecyclerAdapter(TwoActivity.this, list);
recyclerView.setAdapter(adapter);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter.setDj(new MyRecyclerAdapter.Dj() {
@Override
public void dj(View v, final int pisition) {
Toast.makeText(TwoActivity.this,"点击了",Toast.LENGTH_LONG).show();
Intent intent = new Intent(TwoActivity.this, ThreeActivity.class);
intent.putExtra("image",list.get(pisition).getThumbnail_pic_s());
startActivity(intent);
}
});
//添加和删除的功能
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addData(1);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.deleteData(2);
}
});
}
}
mvc层
public class NetMolder {
public void NetHttps(String url, final Data dat){
OkHttpUtils okHttpUtils = new OkHttpUtils();
okHttpUtils.get(url, new OkHttpUtils.HttpData() {
@Override
public void getData(String s) {
Gson gson=new Gson();
User user = gson.fromJson(s, User.class);
List<User.DataBean> data = user.getData();
dat.F(data);
}
});
}
public interface Data{
void F(List s);
}
}
netprocenter层
public class NetProcenter {
private LoginView loginView;
private NetMolder netMolder;
public NetProcenter(LoginView loginView) {
this.loginView = loginView;
netMolder=new NetMolder();
}
public void NetHttps(String url){
netMolder.NetHttps(url, new NetMolder.Data() {
@Override
public void F(List s) {
loginView.onSuccess(s);
}
});
}
}
adapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
private Context context;
private List<User.DataBean> list;
public MyRecyclerAdapter(Context context, List<User.DataBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
User.DataBean bean = list.get(i);
//填充布局
Picasso.with(context).load(""+bean.getThumbnail_pic_s()).into(myHolder.imageView);
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
//添加
public void addData(int position){
list.add(position,list.get(position));
notifyItemInserted(position);
notifyDataSetChanged();
}
//删除
public void deleteData(int position){
list.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageview);
//瀑布流
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width=300;
layoutParams.height=(int)(Math.random()*400+200);
imageView.setLayoutParams(layoutParams);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dj.dj(v,getAdapterPosition());
}
});
}
}
public void setDj(Dj dj) {
this.dj = dj;
}
private Dj dj;
public MyRecyclerAdapter(Dj dj) {
this.dj = dj;
}
public interface Dj{
void dj(View v,int pisition);
}
}
<3>配置user类
<4>网路请求工具类
public class OkHttpUtils {
private HttpData data;
public OkHttpUtils() {
}
public OkHttpUtils get(String path, final HttpData httpData){
final android.os.Handler handler = new android.os.Handler();
//1.new okhttpclient
OkHttpClient client = new OkHttpClient();
//2.okhttpclient.builder
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.writeTimeout(3000,TimeUnit.MILLISECONDS);
builder.readTimeout(3000,TimeUnit.MILLISECONDS);
//3.转化字符
StringBuilder stringBuilder = new StringBuilder();
String params = stringBuilder.toString();
//4.requestbody
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json charset=utf-8"),params);
//5.
Request request=new Request.Builder()
.post(requestBody)
.url(path)
.build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
int code = response.code();
if(code==200){
ResponseBody body = response.body();
final String string = body.string();
handler.post(new Runnable() {
@Override
public void run() {
httpData.getData(string);
}
});
}
}
});
return this;
}
//创建接口
public interface HttpData{
void getData(String s);
}
}
<3>第三个界面
public class ThreeActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
imageView = findViewById(R.id.imageviewt);
Intent intent = getIntent();
String image1 = intent.getStringExtra("image");
Picasso.with(getApplicationContext()).load(image1).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator1 = ObjectAnimator.ofFloat(v,"translationY",200);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(v,"alpha",0,1);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(v,"rotation",360);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(v,"scaleX",2,1);
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1,animator2,animator3,animator4);
set.setDuration(3000);
set.start();
}
});
}
}