当前位置:   article > 正文

使用友盟实现第三方登录_fcl_login_kakao_auth_syn

fcl_login_kakao_auth_syn

 

 

 

配置清单

App

  1. public class App extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. UMConfigure.init(this, "5a12384aa40fa3551f0001d1"
  6. , "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
  7. UMConfigure.setLogEnabled(true);
  8. PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
  9. //豆瓣RENREN平台目前只能在服务器端配置
  10. PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad", "http://sns.whalecloud.com");
  11. PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
  12. PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
  13. PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
  14. PlatformConfig.setAlipay("2015111700822536");
  15. PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
  16. PlatformConfig.setPinterest("1439206");
  17. PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
  18. PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
  19. PlatformConfig.setVKontakte("5764965", "5My6SNliAaLxEm3Lyd9J");
  20. PlatformConfig.setDropbox("oz8v5apet3arcdy", "h7p2pjbzkkxt02a");
  21. }
  22. }

 

 

XML

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ImageView
  9. android:id="@+id/img_pic"
  10. android:layout_width="100dp"
  11. android:layout_height="100dp"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="66dp"
  15. android:src="@drawable/a" />
  16. <EditText
  17. android:id="@+id/edit_mobile"
  18. android:layout_width="200dp"
  19. android:layout_height="48dp"
  20. android:layout_alignParentTop="true"
  21. android:layout_centerHorizontal="true"
  22. android:layout_marginTop="220dp"
  23. android:hint="请输入登录手机号"
  24. android:textSize="18sp" />
  25. <EditText
  26. android:id="@+id/edit_password"
  27. android:layout_width="200dp"
  28. android:layout_height="48dp"
  29. android:layout_centerInParent="true"
  30. android:hint="请输入密码"
  31. android:password="true"
  32. android:textSize="18sp" />
  33. <Button
  34. android:id="@+id/btn_login"
  35. android:layout_width="138dp"
  36. android:layout_height="50dp"
  37. android:layout_alignParentBottom="true"
  38. android:layout_centerHorizontal="true"
  39. android:layout_marginBottom="195dp"
  40. android:background="#f40c04"
  41. android:text="登录"
  42. android:textSize="25sp" />
  43. <TextView
  44. android:layout_width="wrap_content"
  45. android:layout_height="37dp"
  46. android:layout_alignParentBottom="true"
  47. android:layout_centerHorizontal="true"
  48. android:layout_marginBottom="128dp"
  49. android:text="--第三方登录--"
  50. android:textColor="#f1e505"
  51. android:id="@+id/txt"
  52. android:textSize="27sp" />
  53. <LinearLayout
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:layout_alignParentBottom="true"
  57. android:orientation="horizontal">
  58. <ImageView
  59. android:id="@+id/btn_qq"
  60. android:layout_width="50dp"
  61. android:layout_height="50dp"
  62. android:layout_alignParentBottom="true"
  63. android:layout_marginBottom="47dp"
  64. android:src="@drawable/umeng_socialize_qq"
  65. android:layout_weight="1"/>
  66. <ImageView
  67. android:id="@+id/login_wx"
  68. android:layout_width="50dp"
  69. android:layout_height="50dp"
  70. android:layout_weight="1"
  71. android:layout_alignParentBottom="true"
  72. android:layout_marginBottom="47dp"
  73. android:src="@drawable/umeng_socialize_wechat" />
  74. </LinearLayout>
  75. </RelativeLayout>
  76. Activity
  77. import android.Manifest;
  78. import android.content.Intent;
  79. import android.os.Build;
  80. import android.os.Bundle;
  81. import android.support.v4.app.ActivityCompat;
  82. import android.support.v7.app.AppCompatActivity;
  83. import android.view.View;
  84. import android.widget.Button;
  85. import android.widget.EditText;
  86. import android.widget.ImageView;
  87. import android.widget.Toast;
  88. import com.umeng.socialize.UMAuthListener;
  89. import com.umeng.socialize.UMShareAPI;
  90. import com.umeng.socialize.bean.SHARE_MEDIA;
  91. import com.umeng.soexample.MainActivity;
  92. import com.umeng.soexample.R;
  93. import com.umeng.soexample.mvp.presenter.LoginPresenter;
  94. import com.umeng.soexample.mvp.view.LoginView;
  95. import java.util.Map;
  96. public class LoginActivity extends AppCompatActivity implements LoginView {
  97. private EditText mEditMobile;
  98. private EditText mEditPassword;
  99. private Button mBtnLogin;
  100. private ImageView mBtnQq;
  101. private ImageView mLoginWx;
  102. private LoginPresenter loginPresenter;
  103. private UMAuthListener authListener;
  104. @Override
  105. protected void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. setContentView(R.layout.activity_login);
  108. initView();
  109. initViewListener();
  110. initAuthority();
  111. loginPresenter = new LoginPresenter(this);
  112. initListener();
  113. }
  114. private void initListener() {
  115. authListener = new UMAuthListener() {
  116. /**
  117. * @desc 授权开始的回调
  118. * @param platform 平台名称
  119. */
  120. @Override
  121. public void onStart(SHARE_MEDIA platform) {
  122. }
  123. /**
  124. * @desc 授权成功的回调
  125. * @param platform 平台名称
  126. * @param action 行为序号,开发者用不上
  127. * @param data 用户资料返回
  128. */
  129. @Override
  130. public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
  131. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  132. intent.putExtra("name", data.get("name"));
  133. intent.putExtra("pic", data.get("iconurl"));
  134. startActivity(intent);
  135. finish();
  136. }
  137. /**
  138. * @desc 授权失败的回调
  139. * @param platform 平台名称
  140. * @param action 行为序号,开发者用不上
  141. * @param t 错误原因
  142. */
  143. @Override
  144. public void onError(SHARE_MEDIA platform, int action, Throwable t) {
  145. Toast.makeText(LoginActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();
  146. }
  147. /**
  148. * @desc 授权取消的回调
  149. * @param platform 平台名称
  150. * @param action 行为序号,开发者用不上
  151. */
  152. @Override
  153. public void onCancel(SHARE_MEDIA platform, int action) {
  154. Toast.makeText(LoginActivity.this, "取消了", Toast.LENGTH_LONG).show();
  155. }
  156. };
  157. }
  158. @Override
  159. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  160. super.onActivityResult(requestCode, resultCode, data);
  161. UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
  162. }
  163. //动态权限
  164. private void initAuthority() {
  165. if (Build.VERSION.SDK_INT >= 23) {
  166. String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
  167. Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS,
  168. Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP,
  169. Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
  170. ActivityCompat.requestPermissions(this, mPermissionList, 123);
  171. }
  172. }
  173. private void initViewListener() {
  174. mBtnLogin.setOnClickListener(new View.OnClickListener() {
  175. @Override
  176. public void onClick(View v) {
  177. String mobile = mEditMobile.getText().toString().trim();
  178. String password = mEditPassword.getText().toString().trim();
  179. loginPresenter.login(mobile, password);
  180. }
  181. });
  182. mBtnQq.setOnClickListener(new View.OnClickListener() {
  183. @Override
  184. public void onClick(View v) {
  185. UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, authListener);
  186. }
  187. });
  188. mLoginWx.setOnClickListener(new View.OnClickListener() {
  189. @Override
  190. public void onClick(View v) {
  191. UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, authListener);
  192. }
  193. });
  194. }
  195. private void initView() {
  196. mEditMobile = (EditText) findViewById(R.id.edit_mobile);
  197. mEditPassword = (EditText) findViewById(R.id.edit_password);
  198. mBtnLogin = (Button) findViewById(R.id.btn_login);
  199. mBtnQq = (ImageView) findViewById(R.id.btn_qq);
  200. mLoginWx = (ImageView) findViewById(R.id.login_wx);
  201. }
  202. @Override
  203. public void onSuccess(String result) {
  204. Intent intent = new Intent(this, MainActivity.class);
  205. startActivity(intent);
  206. finish();
  207. }
  208. @Override
  209. public void onFailer(String msg) {
  210. Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
  211. }
  212. @Override
  213. public void onRequestPermissionsResult(int requestCode,
  214. String permissions[], int[] grantResults) {
  215. }
  216. }

bean类

  1. import java.io.Serializable;
  2. public class LoginBean implements Serializable {
  3. private String msg;
  4. private String code;
  5. public String getMsg() {
  6. return msg;
  7. }
  8. public void setMsg(String msg) {
  9. this.msg = msg;
  10. }
  11. public String getCode() {
  12. return code;
  13. }
  14. public void setCode(String code) {
  15. this.code = code;
  16. }
  17. }

 

V层

 

public interface LoginView {
    //成功
    void onSuccess(String result);
    //失败
    void onFailer(String msg);
}


M层

  1. import com.google.gson.Gson;
  2. import com.umeng.soexample.bean.LoginBean;
  3. import com.umeng.soexample.mvp.presenter.LoginCallback;
  4. import com.umeng.soexample.util.OkHttpHelper;
  5. import com.umeng.soexample.util.OkHttpHelperListener;
  6. public class LoginModel {
  7. public void login(String mobile, String password, final LoginCallback loginCallback){
  8. final String url = "http://www.zhaoapi.cn/user/login?mobile="+mobile+"&password="+password;
  9. new OkHttpHelper().get( url ).setListener(new OkHttpHelperListener() {
  10. @Override
  11. public void succeed(String data) {
  12. Gson gson = new Gson();
  13. LoginBean loginBean = gson.fromJson( data, LoginBean.class );
  14. if (loginBean.getCode().equals( "0" )) {
  15. loginCallback.onLoginSuccess( loginBean.getMsg() );
  16. }else {
  17. loginCallback.onLoginFailer( loginBean.getMsg() );
  18. }
  19. }
  20. @Override
  21. public void failure(String error) {
  22. }
  23. } );
  24. }
  25. }

 

P层

  1. import com.umeng.soexample.mvp.model.LoginModel;
  2. import com.umeng.soexample.mvp.view.LoginView;
  3. public class LoginPresenter {
  4. public LoginModel loginModel;
  5. public LoginView loginView;
  6. public LoginPresenter(LoginView loginView){
  7. this.loginView = loginView;
  8. loginModel = new LoginModel();
  9. }
  10. public void login(String mobile,String password){
  11. loginModel.login( mobile, password, new LoginCallback() {
  12. @Override
  13. public void onLoginSuccess(String result) {
  14. loginView.onSuccess( result );
  15. }
  16. @Override
  17. public void onLoginFailer(String msg) {
  18. loginView.onFailer( msg );
  19. }
  20. } );
  21. }
  22. }

P层 CallBack

  1. public interface LoginCallback {
  2. //登录成功
  3. void onLoginSuccess(String result);
  4. //登录失败
  5. void onLoginFailer(String msg);
  6. }

 

OkHttpUtil

  1. //接口
  2. public interface OkHttpHelperListener {
  3. void succeed(String data);
  4. void failure(String error);
  5. }
  1. //工具类
  2. import android.annotation.SuppressLint;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.util.Log;
  6. import java.io.IOException;
  7. import okhttp3.Call;
  8. import okhttp3.Callback;
  9. import okhttp3.HttpUrl;
  10. import okhttp3.Interceptor;
  11. import okhttp3.OkHttpClient;
  12. import okhttp3.Request;
  13. import okhttp3.Response;
  14. public class OkHttpHelper {
  15. OkHttpHelperListener listener;
  16. public void setListener(OkHttpHelperListener listener) {
  17. this.listener = listener;
  18. }
  19. public OkHttpHelper get(String url) {
  20. Interceptor interceptor = new Interceptor() {
  21. @Override
  22. public Response intercept(Chain chain) throws IOException {
  23. Request request = chain.request();
  24. HttpUrl url1 = request.url();
  25. String method = request.method();
  26. Log.i("aaaa", "OkHttpHelper intercept: " + "url:" + url1 + " method:" + method);
  27. return chain.proceed(request);
  28. }
  29. };
  30. OkHttpClient build = new OkHttpClient.Builder()
  31. .addInterceptor(interceptor).build();
  32. Request request = new Request.Builder()
  33. .url(url)
  34. .build();
  35. build.newCall(request).enqueue(new Callback() {
  36. @Override
  37. public void onFailure(Call call, IOException e) {
  38. Message message = new Message();
  39. message.obj=e.getMessage();
  40. message.what=1;
  41. handler.sendMessage(message);
  42. }
  43. @Override
  44. public void onResponse(Call call, Response response) throws IOException {
  45. Message message = new Message();
  46. message.obj=response.body().string();
  47. message.what=0;
  48. handler.sendMessage(message);
  49. }
  50. });
  51. return this;
  52. }
  53. @SuppressLint("HandlerLeak")
  54. private Handler handler = new Handler() {
  55. @Override
  56. public void handleMessage(Message msg) {
  57. super.handleMessage(msg);
  58. switch (msg.what) {
  59. case 0:
  60. String data = (String) msg.obj;
  61. listener.succeed(data);
  62. break;
  63. case 1:
  64. String error= (String) msg.obj;
  65. listener.failure(error);
  66. break;
  67. }
  68. }
  69. };
  70. }

传值和瀑布流

  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.support.v7.widget.StaggeredGridLayoutManager;
  6. import android.view.View;
  7. import android.view.animation.AlphaAnimation;
  8. import android.view.animation.Animation;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import com.squareup.picasso.Picasso;
  12. import com.umeng.soexample.activity.AnimaActivity;
  13. import com.umeng.soexample.adapter.MyRecyclerViewAdapter;
  14. import java.util.ArrayList;
  15. import de.hdodenhof.circleimageview.CircleImageView;
  16. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  17. private CircleImageView mImgIcon;
  18. /**
  19. * XXXXX
  20. */
  21. private TextView mTxtName;
  22. private RecyclerView mRecyclerView;
  23. /**
  24. * 添加
  25. */
  26. private Button mBtnAdd;
  27. /**
  28. * 删除
  29. */
  30. private Button mBtnDelete;
  31. /**
  32. * 显示的数据
  33. */
  34. private ArrayList<String> mDatas;
  35. private StaggeredGridLayoutManager mStaggeredGridLayoutManager;
  36. private MyRecyclerViewAdapter mMyRecyclerViewAdapter;
  37. private Button mBtnAnima;
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. initView();
  43. //传值
  44. Intent intent = getIntent();
  45. String name = intent.getStringExtra("name");
  46. String pic = intent.getStringExtra("pic");
  47. mTxtName.setText(name);
  48. Picasso.with(this).load(pic).into(mImgIcon);
  49. initData();
  50. mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
  51. mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
  52. mMyRecyclerViewAdapter = new MyRecyclerViewAdapter(this, mDatas);
  53. mRecyclerView.setAdapter(mMyRecyclerViewAdapter);
  54. initListener();
  55. }
  56. private void initListener() {
  57. //添加点击事件
  58. mMyRecyclerViewAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnRecyclerItemClickListener() {
  59. @Override
  60. public void onItemClick(View view, int position) {
  61. mMyRecyclerViewAdapter.addItem(position, "添加的内容");
  62. }
  63. });
  64. //长按删除
  65. mMyRecyclerViewAdapter.setOnItemLongClickListener(new MyRecyclerViewAdapter.onRecyclerItemLongClickListener() {
  66. @Override
  67. public void onItemLongClick(View view, int position) {
  68. mMyRecyclerViewAdapter.removeItem(position);
  69. }
  70. });
  71. }
  72. //初始化数据
  73. protected void initData() {
  74. mDatas = new ArrayList<String>();
  75. for (int i = 'A'; i < 'z'; i++) {
  76. mDatas.add("" + (char) i);
  77. }
  78. }
  79. private void initView() {
  80. mImgIcon = (CircleImageView) findViewById(R.id.img_icon);
  81. mTxtName = (TextView) findViewById(R.id.txt_name);
  82. mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  83. mBtnAnima = (Button) findViewById(R.id.btn_anima);
  84. mBtnAnima.setOnClickListener(this);
  85. /* mBtnDelete = (Button) findViewById(R.id.btn_delete);*/
  86. }
  87. @Override
  88. public void onClick(View v) {
  89. switch (v.getId()) {
  90. default:
  91. break;
  92. case R.id.btn_anima:
  93. startActivity(new Intent(MainActivity.this,AnimaActivity.class));
  94. break;
  95. }
  96. }
  97. }

Adapter

  1. import android.content.Context;
  2. import android.support.v7.widget.RecyclerView;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import com.umeng.soexample.R;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. /**
  12. * 实现瀑布流效果的适配器
  13. */
  14. public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
  15. private Context context;
  16. private List<String> list;//数据
  17. private List<Integer> heightList;//装产出的随机数
  18. private OnRecyclerItemClickListener mOnItemClickListener;//单击事件
  19. private onRecyclerItemLongClickListener mOnItemLongClickListener;//长按事件
  20. public MyRecyclerViewAdapter(Context context, List<String> list) {
  21. this.context = context;
  22. this.list = list;
  23. //记录为每个控件产生的随机高度,避免滑回到顶部出现空白
  24. heightList = new ArrayList<>();
  25. for (int i = 0; i < list.size(); i++) {
  26. int height = new Random().nextInt(200) + 100;//[100,300)的随机数
  27. heightList.add(height);
  28. }
  29. }
  30. @Override
  31. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  32. //找到item的布局
  33. View view= LayoutInflater.from(context).inflate(R.layout.pinterest_view,parent,false);
  34. return new MyViewHolder(view);//将布局设置给holder
  35. }
  36. @Override
  37. public int getItemCount() {
  38. return list.size();
  39. }
  40. /**
  41. * 绑定视图到holder,就如同ListView的getView(),但是这里已经把复用实现了,我们只需要填充数据就行.
  42. * 由于在复用的时候都是调用该方法填充数据,但是上滑的时候,又会随机产生高度设置到控件上,这样当滑
  43. * 到顶部可能就会看到一片空白,因为后面随机产生的高度和之前的高度不一样,就不能填充屏幕了,所以
  44. * 需要记录每个控件产生的随机高度,然后在复用的时候再设置上去
  45. */
  46. @Override
  47. public void onBindViewHolder(final MyViewHolder holder, final int position) {
  48. //填充数据
  49. holder.textView.setText(list.get(position)+"");
  50. //由于需要实现瀑布流的效果,所以就需要动态的改变控件的高度了
  51. ViewGroup.LayoutParams params = holder.textView.getLayoutParams();
  52. params.height=heightList.get(position);
  53. holder.textView.setLayoutParams(params);
  54. //设置单击事件
  55. if(mOnItemClickListener !=null){
  56. holder.textView.setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. //这里是为textView设置了单击事件,回调出去
  60. //mOnItemClickListener.onItemClick(v,position);这里需要获取布局中的position,不然乱序
  61. mOnItemClickListener.onItemClick(v,holder.getLayoutPosition());
  62. }
  63. });
  64. }
  65. //长按事件
  66. if(mOnItemLongClickListener != null){
  67. holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
  68. @Override
  69. public boolean onLongClick(View v) {
  70. //回调出去
  71. mOnItemLongClickListener.onItemLongClick(v,holder.getLayoutPosition());
  72. return true;//不返回true,松手还会去执行单击事件
  73. }
  74. });
  75. }
  76. }
  77. class MyViewHolder extends RecyclerView.ViewHolder{
  78. TextView textView;
  79. public MyViewHolder(View itemView) {
  80. super(itemView);
  81. textView = (TextView) itemView.findViewById(R.id.textView);
  82. }
  83. }
  84. /**
  85. * 处理item的点击事件,因为recycler没有提供单击事件,所以只能自己写了
  86. */
  87. public interface OnRecyclerItemClickListener {
  88. public void onItemClick(View view, int position);
  89. }
  90. /**
  91. * 长按事件
  92. */
  93. public interface onRecyclerItemLongClickListener{
  94. public void onItemLongClick(View view, int position);
  95. }
  96. /**
  97. * 暴露给外面的设置单击事件
  98. */
  99. public void setOnItemClickListener(OnRecyclerItemClickListener onItemClickListener){
  100. mOnItemClickListener = onItemClickListener;
  101. }
  102. /**
  103. * 暴露给外面的长按事件
  104. */
  105. public void setOnItemLongClickListener(onRecyclerItemLongClickListener onItemLongClickListener){
  106. mOnItemLongClickListener = onItemLongClickListener;
  107. }
  108. /**
  109. * 向指定位置添加元素
  110. */
  111. public void addItem(int position, String value) {
  112. if(position > list.size()) {
  113. position = list.size();
  114. }
  115. if(position < 0) {
  116. position = 0;
  117. }
  118. /**
  119. * 使用notifyItemInserted/notifyItemRemoved会有动画效果
  120. * 而使用notifyDataSetChanged()则没有
  121. */
  122. //在集合中添加这条数据
  123. list.add(position, value);
  124. //添加一个随机高度,会在onBindViewHolder方法中得到设置
  125. heightList.add(position,new Random().nextInt(200) + 100);
  126. //通知插入了数据
  127. notifyItemInserted(position);
  128. }
  129. /**
  130. * 移除指定位置元素
  131. */
  132. public String removeItem(int position) {
  133. if(position > list.size()-1) {
  134. return null;
  135. }
  136. //删除添加的高度
  137. heightList.remove(position);
  138. //所以还需要手动在集合中删除一次
  139. String value = list.remove(position);
  140. //通知删除了数据,但是没有删除list集合中的数据
  141. notifyItemRemoved(position);
  142. return value;
  143. }
  144. }

属性动画

 

  1. public class AnimaActivity extends AppCompatActivity {
  2. private static final int ANIMATION_TIME = 5000;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_anima);
  7. //图片渐变
  8. AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
  9. //渐变时间
  10. alphaAnimation.setDuration(ANIMATION_TIME);
  11. //展示图片渐变动画
  12. this.findViewById(R.id.anima_imgicon).setAnimation(alphaAnimation);
  13. //渐变监听效果
  14. alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
  15. /**
  16. * 动画开始
  17. * @param animation
  18. */
  19. @Override
  20. public void onAnimationStart(Animation animation) {
  21. System.out.println("动画开始");
  22. }
  23. /**
  24. * 动画重复
  25. * @param animation
  26. */
  27. @Override
  28. public void onAnimationEnd(Animation animation) {
  29. System.out.println("动画重复");
  30. }
  31. /**
  32. * 动画结束
  33. * @param animation
  34. */
  35. @Override
  36. public void onAnimationRepeat(Animation animation) {
  37. System.out.println("动画结束");
  38. }
  39. });
  40. }
  41. }

 

瀑布流

 

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);
                    }
                });
    }
}

 

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);
    }
}


属性动画

 

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();
            }
        });

    }

}

 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号