本来说卓成一个gif动态图片的,但是一直上传失败,只能多截图了,如果需要看的话就费劲点
上图
此处有一个记住账号,自动登录,还有一个第三方分享,注册功能没写,跟登录差不太多
此处是有xlistview写的,可以刷新以及加载
这就是第三方登录的效果展示
三个点使用menu写的,可以进行一个分享,我做的分享就是得到每个目录里面的第二张图分享出去
分享完毕后有接口回调,这里呢,我们用的都是第三方,所以只要赋值粘贴就好了
好了,直接上代码吧
首先两个fragment的适配器,
package soexample.umeng.com.zhoukaomoni.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import java.util.ArrayList;
public class FragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> datas;
public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> datas) {
super(fm);
this.datas = datas;
}
@Override
public Fragment getItem(int i) {
return datas.get(i);
}
@Override
public int getCount() {
return datas.size();
}
}
xlistview的适配器
package soexample.umeng.com.zhoukaomoni.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import soexample.umeng.com.zhoukaomoni.R;
import soexample.umeng.com.zhoukaomoni.bean.MyData;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<MyData.DataBean> datas;
public MyAdapter(Context context, ArrayList<MyData.DataBean> datas) {
this.context = context;
this.datas = datas;
}
public ArrayList<MyData.DataBean> getDatas() {
return datas;
}
public void setDatas(ArrayList<MyData.DataBean> datas) {
this.datas = datas;
//刷新适配器
notifyDataSetChanged();
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//声明一个holder类
ViewHolderOne one = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.myadapter, null);
one = new ViewHolderOne();
one.iv_img = convertView.findViewById(R.id.Img_One);
one.tv_title = convertView.findViewById(R.id.Title_One);
convertView.setTag(one);
} else {
one = (ViewHolderOne) convertView.getTag();
}
Glide.with(context).load(datas.get(position).getThumbnail_pic_s()).into(one.iv_img);
one.tv_title.setText(datas.get(position).getTitle());
return convertView;
}
class ViewHolderOne {
ImageView iv_img;
TextView tv_title;
}
}
两个bean包
登录注册的
package soexample.umeng.com.zhoukaomoni.bean;
public class User {
/**
* msg : 用户名不能为空
* code : 101
* data : {}
*/
private String msg;
private int code;
private DataBean data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class DataBean {
}
}
数据展示的
package soexample.umeng.com.zhoukaomoni.bean;
import java.util.List;
public class MyData {
private String msg;
private int code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
MVP用的接口回调
package soexample.umeng.com.zhoukaomoni.callback;
public interface MyCallBack<T> {
void setData(T user);
void setError(T error);
}
MVPm层的接口
package soexample.umeng.com.zhoukaomoni.moudle;
import soexample.umeng.com.zhoukaomoni.callback.MyCallBack;
public interface Moudle {
void userMoudle(String url, String username, String password, MyCallBack callBack);
}
M层的实现类
扫描二维码关注公众号,回复:
4576248 查看本文章
package soexample.umeng.com.zhoukaomoni.moudle;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.json.JSONException;
import org.json.JSONObject;
import soexample.umeng.com.zhoukaomoni.bean.MyData;
import soexample.umeng.com.zhoukaomoni.bean.User;
import soexample.umeng.com.zhoukaomoni.callback.MyCallBack;
import soexample.umeng.com.zhoukaomoni.util.HttpUtil;
public class MoudleImpl implements Moudle {
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsons = (String) msg.obj;
Gson gson = new Gson();
User user = gson.fromJson(jsons, User.class);
callBack.setData(user);
}
};
private MyCallBack callBack;
@Override
public void userMoudle(final String url, final String username, final String password, final MyCallBack callBack) {
this.callBack = callBack;
new Thread(new Runnable() {
@Override
public void run() {
try {
String jsons = HttpUtil.getStr(url, username, password);
handler.sendMessage(handler.obtainMessage(0, jsons));
} catch (Exception e) {
Looper.prepare();
callBack.setError("出现了异常");
Looper.loop();
}
}
}).start();
}
}
V层的接口
package soexample.umeng.com.zhoukaomoni.iview;
public interface IView<T> {
void success(T data);
void error(T error);
}
P层的接口
package soexample.umeng.com.zhoukaomoni.ipresenter;
public interface IPresenter {
void startRequest(String url, String username, String password);
}
P层的实现类
package soexample.umeng.com.zhoukaomoni.ipresenter;
import soexample.umeng.com.zhoukaomoni.MainActivity;
import soexample.umeng.com.zhoukaomoni.callback.MyCallBack;
import soexample.umeng.com.zhoukaomoni.iview.IView;
import soexample.umeng.com.zhoukaomoni.moudle.MoudleImpl;
public class IPresenterImpl implements IPresenter {
private IView iView;
private MoudleImpl moudle;
public IPresenterImpl(MainActivity iView) {
this.iView = iView;
moudle = new MoudleImpl();
}
@Override
public void startRequest(final String url, final String username, final String password) {
moudle.userMoudle(url, username, password, new MyCallBack() {
@Override
public void setData(Object user) {
iView.success(user);
}
@Override
public void setError(Object error) {
iView.error(error);
}
});
}
//防止内存泄漏
public void onDetacth() {
if (moudle!=null){
moudle = null;
}
if (iView!=null){
}
}
}
用户登录解析工具类
package soexample.umeng.com.zhoukaomoni.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtil {
public static String getStr(String mUrl, String username, String password) {
try {
URL url = new URL(mUrl + "?username=" + username + "&password" + password);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
String jsons = getStream(stream);
return jsons;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String getStream(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String con;
while ((con = reader.readLine()) != null) {
sb.append(con);
}
reader.close();
return sb.toString();
}
}
XlistView工具解析类
package soexample.umeng.com.zhoukaomoni.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class ListHttpUtil {
public static String getStr(String mUrl) {
try {
URL url = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
String jsons = getStream(stream);
return jsons;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String getStream(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String con;
while ((con = reader.readLine()) != null) {
sb.append(con);
}
reader.close();
return sb.toString();
}
}
登录页面
package soexample.umeng.com.zhoukaomoni;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.util.Map;
import soexample.umeng.com.zhoukaomoni.bean.User;
import soexample.umeng.com.zhoukaomoni.ipresenter.IPresenterImpl;
import soexample.umeng.com.zhoukaomoni.iview.IView;
public class MainActivity<T> extends AppCompatActivity implements View.OnClickListener, IView<T> {
private EditText UserName, Password;
private Button Login, ZhuCe, Three;
private SharedPreferences mShare;
private boolean isLogin;
private SharedPreferences.Editor mEditor;
private IPresenterImpl presenter;
private String mUrl = "http://www.xieast.com/api/user/login.php";
private CheckBox MyZiDong;
private CheckBox MyJiZhu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShare = getSharedPreferences("lf", MODE_PRIVATE);
initView();
if (mShare.getBoolean("jizhu", false)) {
String zhanghao = mShare.getString("zhanghao", null);
String mima = mShare.getString("mima", null);
UserName.setText(zhanghao);
Password.setText(mima);
MyJiZhu.setChecked(true);
}
processLogic();
}
private void processLogic() {
//本地保存账号密码
mShare = getSharedPreferences("lf", MODE_PRIVATE);
mEditor = mShare.edit();
boolean zhuangtai = mShare.getBoolean("zhuangtai", false);
//
if (zhuangtai) {
startActivity(new Intent(this, HomeActivity.class));
finish();
}
presenter = new IPresenterImpl(this);
}
private void initView() {
UserName = (EditText) findViewById(R.id.UserName);
Password = (EditText) findViewById(R.id.Password);
Login = (Button) findViewById(R.id.Login);
ZhuCe = (Button) findViewById(R.id.ZhuCe);
Three = (Button) findViewById(R.id.Three);
Login.setOnClickListener(this);
ZhuCe.setOnClickListener(this);
Three.setOnClickListener(this);
MyZiDong = (CheckBox) findViewById(R.id.MyZiDong);
MyZiDong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mEditor.putBoolean("zhuangtai", true);
mEditor.putBoolean("jizhu", true);
mEditor.commit();
MyJiZhu.setChecked(true);
} else {
mEditor.putBoolean("zhuangtai", false);
mEditor.commit();
}
}
});
MyJiZhu = (CheckBox) findViewById(R.id.MyJiZhu);
MyJiZhu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = UserName.getText().toString().trim();
String pwd = Password.getText().toString().trim();
if (MyJiZhu.isChecked()) {
mEditor.putBoolean("jizhu", true);
mEditor.putString("zhanghao", name);
mEditor.putString("mima", pwd);
mEditor.commit();
} else {
mEditor.putBoolean("jizhu", false);
mEditor.putString("zhanghao", null);
mEditor.putString("mima", null);
mEditor.commit();
}
}
});
MyJiZhu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Login:
//进行取值
String name = UserName.getText().toString().trim();
String pwd = Password.getText().toString().trim();
//进行判断
if (name.isEmpty() || pwd.isEmpty()) {
Toast.makeText(MainActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
return;
} else {
if (!name.equals("13800138000") || !pwd.equals("123456")) {
Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
return;
} else {
presenter.startRequest(mUrl, name, pwd);
}
}
break;
case R.id.ZhuCe:
startActivity(new Intent(this, RegisterActivity.class));
break;
case R.id.Three:
umengDeleteOauth(SHARE_MEDIA.QQ);
//开始授权
shareLoginUmeng(this, SHARE_MEDIA.QQ);
break;
}
}
public static void shareLoginUmeng(final Activity activity, SHARE_MEDIA share_media_type) {
UMShareAPI.get(activity).getPlatformInfo(activity, share_media_type, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
// Log.e(TAG, "onStart授权开始: ");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//sdk是6.4.5的,但是获取值的时候用的是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");//头像地址
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
Toast.makeText(activity, "授权失败", Toast.LENGTH_LONG).show();
// Log.e(TAG, "onError: " + "授权失败");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
Toast.makeText(activity, "授权取消", Toast.LENGTH_LONG).show();
// Log.e(TAG, "onError: " + "授权取消");
}
});
}
/**
* 友盟取消授权(登入)
*/
private void umengDeleteOauth(SHARE_MEDIA share_media_type) {
UMShareAPI.get(this).deleteOauth(this, share_media_type, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
//开始授权
// Log.e(TAG, "onStart: ");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//取消授权成功 i=1
//Log.e(TAG, "onComplete: ");
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
//授权出错
//Log.e(TAG, "onError: ");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
//取消授权
//Log.e(TAG, "onCancel: ");
}
});
}
@Override
public void success(T data) {
User user = (User) data;
if (user.getCode() == 100) {
Toast.makeText(this, user.getCode() + "", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, user.getCode() + "", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, HomeActivity.class));
//登录完毕销毁此页面
finish();
}
}
@Override
public void error(T error) {
String e = (String) error;
Toast.makeText(MainActivity.this, e, Toast.LENGTH_SHORT).show();
}
}
登录页面的xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6cd6bf"
android:gravity="center"
android:padding="8dp"
android:text="登录"
android:textSize="32sp" />
<EditText
android:id="@+id/UserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="请输入账号" />
<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="请输入密码"
android:inputType="numberPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/MyZiDong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
<CheckBox
android:id="@+id/MyJiZhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/Login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="登录" />
<Button
android:id="@+id/ZhuCe"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="注册" />
</LinearLayout>
<Button
android:id="@+id/Three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="授权第三方登录" />
</LinearLayout>
主页的代码
package soexample.umeng.com.zhoukaomoni;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;
import java.util.ArrayList;
import soexample.umeng.com.zhoukaomoni.adapter.FragmentAdapter;
import soexample.umeng.com.zhoukaomoni.fragment.MyFragment;
import soexample.umeng.com.zhoukaomoni.fragment.XlistFragment;
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
private ArrayList<Fragment> datas = new ArrayList<>();
private FragmentAdapter adapter;
private ViewPager My_Vp;
private RadioGroup RG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initView();
initData();
adapter = new FragmentAdapter(getSupportFragmentManager(), datas);
My_Vp.setAdapter(adapter);
RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.RB1) {
My_Vp.setCurrentItem(0);
} else {
My_Vp.setCurrentItem(1);
}
}
});
My_Vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (i == 0) {
RG.check(R.id.RB1);
} else {
RG.check(R.id.RB2);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void initData() {
XlistFragment xlistFragment = new XlistFragment();
MyFragment myFragment = new MyFragment();
datas.add(xlistFragment);
datas.add(myFragment);
}
@Override
public void onClick(View v) {
}
private void initView() {
My_Vp = findViewById(R.id.My_Vp);
RG = findViewById(R.id.RG);
}
}
主页的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".HomeActivity">
<RadioGroup
android:id="@+id/RG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/RB1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="首页"
android:textSize="26sp" />
<RadioButton
android:id="@+id/RB2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="我的"
android:textSize="26sp" />
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/My_Vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/RG"></android.support.v4.view.ViewPager>
</RelativeLayout>
XListView的fragmet页面
package soexample.umeng.com.zhoukaomoni.fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import com.example.xlistviewflush.view.XListView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Date;
import soexample.umeng.com.zhoukaomoni.R;
import soexample.umeng.com.zhoukaomoni.WebViewActivity;
import soexample.umeng.com.zhoukaomoni.adapter.MyAdapter;
import soexample.umeng.com.zhoukaomoni.bean.MyData;
import soexample.umeng.com.zhoukaomoni.util.ListHttpUtil;
public class XlistFragment extends Fragment implements XListView.IXListViewListener, View.OnClickListener {
private XListView My_XList;
private View v;
private String mUrl = "http://www.xieast.com/api/news/news.php?page=";
private MyAdapter adapter;
private ArrayList<MyData.DataBean> datas = new ArrayList<>();
private int index = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_xlist, null);
initView(v);
My_XList.setPullLoadEnable(true);
//设置监听事;
My_XList.setXListViewListener(this);
//创建适配器
adapter = new MyAdapter(getActivity(), datas);
//设置适配器
My_XList.setAdapter(adapter);
new MyTask().execute(mUrl + index);
My_XList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), WebViewActivity.class);
String url = datas.get(position - 1).getUrl();
String img = datas.get(position - 1).getThumbnail_pic_s02();
intent.putExtra("uu", url);
intent.putExtra("img", img);
startActivity(intent);
}
});
return v;
}
private void initView(View v) {
My_XList = v.findViewById(R.id.My_XList);
}
@Override
public void onClick(View v) {
}
//进行异步加载
class MyTask extends AsyncTask<String, Void, ArrayList<MyData.DataBean>> {
@Override
protected ArrayList<MyData.DataBean> doInBackground(String... strings) {
//进行解析
String jsonstr = ListHttpUtil.getStr(strings[0]);
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonstr, MyData.class);
return (ArrayList<MyData.DataBean>) myData.getData();
}
@Override
protected void onPostExecute(ArrayList<MyData.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
datas.addAll(dataBeans);
//刷新适配器
adapter.notifyDataSetChanged();
//停止刷新动画
stopRefresh();
}
}
private void stopRefresh() {
//停止刷新
My_XList.stopRefresh();
My_XList.stopLoadMore();
//显示刷新时间
Date date = new Date();
String datast = (String) android.text.format.DateFormat.format("EEEE, MMMM dd日, yyyy kk:mm:ss", date);
My_XList.setRefreshTime(datast);
}
@Override
public void onRefresh() {
//上拉刷新
datas.clear();
index = 1;
new MyTask().execute(mUrl + index);
stopRefresh();
}
@Override
public void onLoadMore() {
//下拉加载
new MyTask().execute(mUrl + (index++));
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".fragment.XlistFragment">
<com.example.xlistviewflush.view.XListView
android:id="@+id/My_XList"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.xlistviewflush.view.XListView>
</LinearLayout>
我的页面二维码和退出登录
package soexample.umeng.com.zhoukaomoni.fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.google.zxing.WriterException;
import com.yzq.zxinglibrary.encode.CodeCreator;
import soexample.umeng.com.zhoukaomoni.MainActivity;
import soexample.umeng.com.zhoukaomoni.R;
/**
* A simple {@link Fragment} subclass.
*/
public class MyFragment extends Fragment implements View.OnClickListener {
private ImageView Image;
private Button Exit;
private View v;
private String contentEtString = "";
private SharedPreferences mShared;
private SharedPreferences.Editor mEdit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_my, null);
mShared = getActivity().getSharedPreferences("lf", Context.MODE_PRIVATE);
String zhanghao = mShared.getString("zhanghao", null);
contentEtString = zhanghao;
initView(v);
initData();
return v;
}
private void initData() {
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
try {
//生成二维码需要用到类是CodeCreator
Bitmap bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, logo);
Image.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
private void initView(View v) {
Image = (ImageView) v.findViewById(R.id.Image);
Exit = (Button) v.findViewById(R.id.Exit);
Exit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Exit:
mEdit = mShared.edit();
mEdit.clear();
mEdit.commit();
startActivity(new Intent(getActivity(), MainActivity.class));
getActivity().finish();
break;
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".fragment.MyFragment">
<ImageView
android:id="@+id/Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />
<Button
android:id="@+id/Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="退出登录" />
</LinearLayout>
WebView和友盟分享
package soexample.umeng.com.zhoukaomoni;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
public class WebViewActivity extends AppCompatActivity {
private WebView My_Web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
initView();
Intent intent = getIntent();
String uu = intent.getStringExtra("uu");
WebSettings settings = My_Web.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
My_Web.setWebViewClient(new WebViewClient());
My_Web.loadUrl(uu);
}
private void initView() {
My_Web = (WebView) findViewById(R.id.My_Web);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//引入布局
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
//菜单点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.My_menu:
Intent intent = getIntent();
String img = intent.getStringExtra("img");
UMImage umImage = new UMImage(this, img);
new ShareAction(WebViewActivity.this)
.withText("hello").withMedia(umImage)
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE)
//分享的监听
.setCallback(shareListener).open();
break;
}
return true;
}
//设置回调监听
private UMShareListener shareListener = new UMShareListener() {
/**
* @descrption 分享开始的回调
* @param platform 平台类型
*/
@Override
public void onStart(SHARE_MEDIA platform) {
}
/**
* @descrption 分享成功的回调
* @param platform 平台类型
*/
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(WebViewActivity.this, "成功了", Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享失败的回调
* @param platform 平台类型
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(WebViewActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享取消的回调
* @param platform 平台类型
*/
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(WebViewActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};
}
webview的第三方分享xml
<?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"
android:orientation="vertical"
tools:context=".WebViewActivity">
<WebView
android:id="@+id/My_Web"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
友盟分享大的微信的接口
package soexample.umeng.com.zhoukaomoni.wxapi;
import com.umeng.socialize.weixin.view.WXCallbackActivity;
public class WXEntryActivity extends WXCallbackActivity {
}
Application的代码 需要注册
package soexample.umeng.com.zhoukaomoni;
import android.app.Application;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this, "5a12384aa40fa3551f0001d1"
, "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
//开启log打印 报错会给你提示
UMConfigure.setLogEnabled(true);
PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
//豆瓣RENREN平台目前只能在服务器端配置
PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad", "http://sns.whalecloud.com");
PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
PlatformConfig.setAlipay("2015111700822536");
PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
PlatformConfig.setPinterest("1439206");
PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
PlatformConfig.setVKontakte("5764965", "5My6SNliAaLxEm3Lyd9J");
PlatformConfig.setDropbox("oz8v5apet3arcdy", "h7p2pjbzkkxt02a");
}
}
需要用到的java包 友盟第三方的东西
第三方粘贴的
<activity
android:name="com.tencent.tauth.AuthActivity"
android:launchMode="singleTask"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tencent100424468" />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="UMENG_APPKEY"
android:value="5c091588b465f573080003f6" />
做menu的代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/kongbai"
android:title="空白"
app:showAsAction="never"
/>
<item
android:id="@+id/My_menu"
android:title="分享" />
</menu>
友盟需要粘贴的xml
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation project(':pulltolibrary')//这个是自己写的laibrary
implementation project(':xlistviewflush')
implementation 'com.github.yuzhiqiang1993:zxing:2.1.4'
implementation 'com.android.support:design:28.0.0'
implementation files('libs/umeng-share-wechat-simplify-6.9.4.jar')
implementation 'com.umeng.sdk:common:latest.integration' //这两个都是友盟的
implementation 'com.umeng.sdk:analytics:latest.integration'
需要再项目的目录中写入
allprojects {
repositories {
mavenCentral()
}
}
sdk改成16
根目录中在allprojects中加入这句话,否则会报错
maven { url 'https://jitpack.io' }
源代码已上传
以上,如有错误,请指正.