├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── app-release.apk ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── laravelchen │ │ └── toutiao │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── laravelchen │ │ │ └── toutiao │ │ │ ├── ArticleContentShow.java │ │ │ ├── Auth │ │ │ ├── LoginFragment.java │ │ │ └── RegisterFragment.java │ │ │ ├── Constant.java │ │ │ ├── Interface │ │ │ └── OnItemClickListener.java │ │ │ ├── JokeContent.java │ │ │ ├── MainActivity.java │ │ │ ├── PathRandom.java │ │ │ ├── PhotoActivity.java │ │ │ ├── UserLoginAndRegister.java │ │ │ ├── UserSetting.java │ │ │ ├── VideoActivity.java │ │ │ ├── alladapter │ │ │ ├── JokeAdapter.java │ │ │ ├── MyFragmentPagerAdapter.java │ │ │ ├── PhotoAdapter.java │ │ │ ├── RecommendAdapter.java │ │ │ └── VideoAdapter.java │ │ │ ├── allbean │ │ │ ├── JokeBean.java │ │ │ ├── PhotoBean.java │ │ │ ├── RecommendBean.java │ │ │ └── VideoBean.java │ │ │ ├── allfragment │ │ │ ├── HomeFragment.java │ │ │ ├── JokeFragment.java │ │ │ ├── PageFragment.java │ │ │ ├── PhotoFragment.java │ │ │ ├── UserFragment.java │ │ │ └── VideoFragment.java │ │ │ └── extra │ │ │ ├── GetJson.java │ │ │ ├── ImgLoader.java │ │ │ └── ImgUtils.java │ └── res │ │ ├── drawable │ │ └── login.xml │ │ ├── layout │ │ ├── activity_article_content_show.xml │ │ ├── activity_joke_content.xml │ │ ├── activity_main.xml │ │ ├── activity_photo.xml │ │ ├── activity_user_login_and_register.xml │ │ ├── activity_user_setting.xml │ │ ├── activity_video.xml │ │ ├── fragment_home.xml │ │ ├── fragment_login.xml │ │ ├── fragment_page.xml │ │ ├── fragment_photo.xml │ │ ├── fragment_register.xml │ │ ├── fragment_user.xml │ │ ├── fragment_video.xml │ │ ├── fragment_wei_tou_tiao.xml │ │ ├── joke_main.xml │ │ ├── navigation_header.xml │ │ ├── navigation_header_before.xml │ │ ├── photo_main.xml │ │ ├── recommend_home.xml │ │ ├── recommend_home_has_image.xml │ │ ├── recommend_home_no_image.xml │ │ ├── theme_choose.xml │ │ ├── user_share.xml │ │ └── video_main.xml │ │ ├── menu │ │ ├── drawer.xml │ │ └── main.xml │ │ ├── mipmap-hdpi │ │ ├── app.png │ │ ├── app1.png │ │ ├── avatar.jpeg │ │ ├── home_view.jpeg │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── comment.png │ │ ├── email.png │ │ ├── fanhui.png │ │ ├── fenxiang.png │ │ ├── github.png │ │ ├── githubblue.png │ │ ├── home.png │ │ ├── home2.png │ │ ├── ic_launcher.png │ │ ├── ic_launcher_round.png │ │ ├── login.jpeg │ │ ├── loginbck.jpg │ │ ├── password.png │ │ ├── photo.png │ │ ├── play.png │ │ ├── qq.png │ │ ├── right.png │ │ ├── search2.png │ │ ├── share_moment.png │ │ ├── share_qq.png │ │ ├── share_qzone.png │ │ ├── share_wechat.png │ │ ├── share_weibo.png │ │ ├── shezhi.png │ │ ├── shoucang.png │ │ ├── theme_black.png │ │ ├── theme_blue.png │ │ ├── theme_purple.png │ │ ├── theme_yellow.png │ │ ├── themem_pink.png │ │ ├── tuichu.png │ │ ├── user.png │ │ ├── username.png │ │ ├── video.png │ │ ├── weibo.png │ │ └── zhuti.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimen.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── laravelchen │ └── toutiao │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── home.gif ├── info.gif ├── login.gif ├── logout.gif ├── photo.gif └── video.gif ├── readme.md └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 35 | 36 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LaravelChen/TouTiao/c1f1bfa9c9d37013723a4cfaf9f02a357cad68d1/app/app-release.apk -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "com.example.laravelchen.toutiao" 8 | minSdkVersion 21 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | manifestPlaceholders = [ 14 | // 替换成你的qq_id 15 | qq_id: "101410114" 16 | ] 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | 32 | compile('com.dou361.dialogui:jjdxm-dialogui:1.0.3') { 33 | // exclude group: 'com.android.support', module: 'support-v7' 34 | // exclude group: 'com.android.support', module: 'design' 35 | } 36 | 37 | compile('com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.5') { 38 | exclude group: 'com.android.support', module: 'appcompat-v7' 39 | } 40 | 41 | //最小版本21 42 | //最小版本21 43 | compile 'com.android.support:appcompat-v7:25.3.1' 44 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 45 | compile 'com.android.support:recyclerview-v7:25.3.1' 46 | compile 'com.android.support:cardview-v7:25.3.1' 47 | compile 'com.android.support:design:25.3.1' 48 | compile 'com.android.support:support-v4:25.3.1' 49 | compile 'com.ashokvarma.android:bottom-navigation-bar:1.4.3' 50 | compile 'com.github.bumptech.glide:glide:3.7.0' 51 | compile 'com.squareup.okhttp3:okhttp:3.7.0' 52 | compile 'org.jsoup:jsoup:1.10.1' 53 | compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' 54 | compile 'de.hdodenhof:circleimageview:2.1.0' 55 | compile 'com.tamic.novate:novate:1.5.0' 56 | compile 'me.shaohui.shareutil:shareutil:1.3.8' 57 | compile 'com.rengwuxian.materialedittext:library:2.1.4' 58 | compile 'com.contrarywind:Android-PickerView:3.2.5' 59 | compile 'tv.danmaku.ijk.media:ijkplayer-java:0.6.0' 60 | compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.6.0' 61 | compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.6.0' 62 | compile 'tv.danmaku.ijk.media:ijkplayer-arm64:0.6.0' 63 | compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.6.0' 64 | compile 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.6.0' 65 | testCompile 'junit:junit:4.12' 66 | debugCompile 'com.squareup.okhttp3:logging-interceptor:3.7.0' 67 | } 68 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/apple/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/laravelchen/toutiao/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.laravelchen.toutiao", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 31 | 34 | 37 | 40 | 43 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/ArticleContentShow.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import android.app.Activity; 4 | import android.app.ProgressDialog; 5 | import android.content.Intent; 6 | import android.content.SharedPreferences; 7 | import android.graphics.Color; 8 | import android.os.Build; 9 | import android.support.annotation.RequiresApi; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.os.Bundle; 12 | import android.support.v7.widget.Toolbar; 13 | import android.view.KeyEvent; 14 | import android.view.MenuItem; 15 | import android.view.View; 16 | import android.webkit.WebChromeClient; 17 | import android.webkit.WebSettings; 18 | import android.webkit.WebView; 19 | import android.webkit.WebViewClient; 20 | import android.widget.TextView; 21 | 22 | public class ArticleContentShow extends AppCompatActivity { 23 | private WebView webView = null; 24 | 25 | private ProgressDialog dialog = null; 26 | 27 | private Intent data; 28 | private String share_url; 29 | private Toolbar toolbar; 30 | private SharedPreferences sp; 31 | 32 | 33 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_article_content_show); 38 | toolbar = (Toolbar) findViewById(R.id.contentToolbar); 39 | toolbar.setTitle("今日头条 - 文章内容"); 40 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | finish(); 44 | } 45 | }); 46 | setSupportActionBar(toolbar); 47 | sp=getSharedPreferences("user_auth", Activity.MODE_PRIVATE); 48 | toolbar.setBackgroundColor(Color.parseColor(sp.getString("theme", "#3F51B5"))); 49 | 50 | //初始化页面 51 | initViews(); 52 | 53 | // 启用支持JavaScript 54 | WebSettings webSettings = webView.getSettings(); 55 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 56 | webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); 57 | } 58 | webSettings.setJavaScriptEnabled(true); 59 | webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); 60 | webSettings.setDomStorageEnabled(true); 61 | webSettings.setAppCacheEnabled(true);// 应用可以有缓存 62 | webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 优先使用缓存 63 | webSettings.setAppCacheEnabled(true);// 缓存最多可以有10M 64 | 65 | // 进度条显示网页的加载过程 66 | download(); 67 | // 优先使用缓存优化效率 68 | webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 69 | } 70 | 71 | 72 | private void download() { 73 | webView.setWebChromeClient(new WebChromeClient() { 74 | @Override 75 | public void onProgressChanged(WebView view, int newProgress) { 76 | 77 | if (newProgress == 100) { 78 | // 加载完毕 79 | closeDialog(newProgress); 80 | } else { 81 | openDialog(newProgress); 82 | } 83 | 84 | 85 | super.onProgressChanged(view, newProgress); 86 | } 87 | 88 | private void openDialog(int newProgress) { 89 | if (dialog == null) { 90 | dialog = new ProgressDialog(ArticleContentShow.this); 91 | dialog.setTitle("正在加载"); 92 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 93 | dialog.setProgress(newProgress); 94 | dialog.show(); 95 | } else { 96 | dialog.setProgress(newProgress); 97 | } 98 | } 99 | 100 | private void closeDialog(int newProgress) { 101 | if (dialog != null && dialog.isShowing()) { 102 | dialog.dismiss(); 103 | dialog = null; 104 | } 105 | } 106 | }); 107 | } 108 | 109 | private void initViews() { 110 | getSupportActionBar().setDisplayShowHomeEnabled(true); 111 | getSupportActionBar().setDisplayUseLogoEnabled(true); 112 | webView = (WebView) findViewById(R.id.webView); 113 | data = getIntent(); 114 | share_url = data.getStringExtra("share_url"); 115 | webView.loadUrl(share_url); 116 | webView.setWebViewClient(new WebViewClient() { 117 | @Override 118 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 119 | view.loadUrl(url); 120 | return true; 121 | } 122 | }); 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/Auth/LoginFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.Auth; 2 | 3 | 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.SharedPreferences; 7 | import android.os.Bundle; 8 | import android.support.v4.app.Fragment; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.EditText; 13 | 14 | import com.example.laravelchen.toutiao.Constant; 15 | import com.example.laravelchen.toutiao.MainActivity; 16 | import com.example.laravelchen.toutiao.R; 17 | import com.example.laravelchen.toutiao.UserLoginAndRegister; 18 | import com.tamic.novate.Novate; 19 | import com.tamic.novate.Throwable; 20 | import com.tamic.novate.callback.ResponseCallback; 21 | 22 | import org.json.JSONObject; 23 | 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | import java.util.regex.Matcher; 27 | import java.util.regex.Pattern; 28 | 29 | import de.hdodenhof.circleimageview.CircleImageView; 30 | import okhttp3.Call; 31 | import okhttp3.ResponseBody; 32 | 33 | /** 34 | * A simple {@link Fragment} subclass. 35 | */ 36 | public class LoginFragment extends Fragment implements View.OnClickListener { 37 | 38 | 39 | private EditText email; 40 | private EditText password; 41 | private Novate novate; 42 | 43 | public LoginFragment() { 44 | // Required empty public constructor 45 | } 46 | 47 | 48 | @Override 49 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 50 | Bundle savedInstanceState) { 51 | // Inflate the layout for this fragment 52 | View view = inflater.inflate(R.layout.fragment_login, container, false); 53 | 54 | email = (EditText) view.findViewById(R.id.email); 55 | password = (EditText) view.findViewById(R.id.password); 56 | 57 | //初始化novate 58 | novate = new Novate.Builder(getContext()) 59 | .baseUrl("http://cxboss.cn/laravelandroid/public/api/") 60 | .build(); 61 | 62 | view.findViewById(R.id.register).setOnClickListener(this); 63 | view.findViewById(R.id.fanhui).setOnClickListener(this); 64 | view.findViewById(R.id.login).setOnClickListener(this); 65 | return view; 66 | } 67 | 68 | @Override 69 | public void onClick(View v) { 70 | switch (v.getId()) { 71 | case R.id.register: 72 | getFragmentManager().beginTransaction().replace(R.id.usermainfragment, new RegisterFragment()).commit(); 73 | break; 74 | case R.id.fanhui: 75 | getActivity().finish(); 76 | break; 77 | case R.id.login: 78 | login(email.getText().toString(), password.getText().toString()); 79 | break; 80 | } 81 | } 82 | 83 | private void login(String emailstr, String passwordstr) { 84 | Boolean is_validate = validate(emailstr, passwordstr); 85 | Map map = new HashMap(); 86 | map.put("email", emailstr); 87 | map.put("password", passwordstr); 88 | if (is_validate) { 89 | novate.rxPost("user_login", map, new ResponseCallback() { 90 | @Override 91 | public Object onHandleResponse(ResponseBody response) throws Exception { 92 | String string = new String(response.bytes()); 93 | JSONObject user = new JSONObject(string); 94 | JSONObject userinfo = new JSONObject(user.getString("user")); 95 | if (user.optBoolean("message")) { 96 | SharedPreferences sp = getActivity().getSharedPreferences("user_auth", getActivity().MODE_PRIVATE); 97 | SharedPreferences.Editor editor = sp.edit(); 98 | editor.putInt("id", userinfo.optInt("id")); 99 | editor.putString("name", userinfo.optString("name")); 100 | editor.putInt("followers", userinfo.optInt("followers")); 101 | editor.putInt("following", userinfo.optInt("following")); 102 | editor.putString("api_token", userinfo.optString("api_token")); 103 | editor.putString("created_at", userinfo.optString("created_at")); 104 | editor.putString("introduction", userinfo.optString("introduction")); 105 | editor.putString("avatar", userinfo.optString("avatar")); 106 | editor.putString("sex", userinfo.optString("sex")); 107 | editor.putString("birthday", userinfo.optString("birthday")); 108 | editor.putBoolean("message", user.optBoolean("message")); 109 | editor.commit(); 110 | startActivity(new Intent(getContext(), MainActivity.class)); 111 | } 112 | return null; 113 | } 114 | 115 | @Override 116 | public void onError(Object tag, Throwable e) { 117 | 118 | } 119 | 120 | @Override 121 | public void onCancel(Object tag, Throwable e) { 122 | 123 | } 124 | 125 | @Override 126 | public void onNext(Object tag, Call call, Object response) { 127 | 128 | } 129 | }); 130 | } 131 | } 132 | 133 | private Boolean validate(String emailstr, String passwordstr) { 134 | String rex = "[\\w-]+[\\.\\w]*@[\\w]+(\\.[\\w]{2,3})"; 135 | Pattern p = Pattern.compile(rex); 136 | Matcher m = p.matcher(emailstr); 137 | if (m.find() == false) { 138 | email.setError("邮箱地址不正确"); 139 | return false; 140 | } 141 | if (passwordstr.length() < 6 || passwordstr.length() > 15) { 142 | password.setError("密码长度为6~15"); 143 | return false; 144 | } 145 | return true; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/Auth/RegisterFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.Auth; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.EditText; 10 | import android.widget.Toast; 11 | 12 | import com.example.laravelchen.toutiao.R; 13 | import com.example.laravelchen.toutiao.UserLoginAndRegister; 14 | import com.tamic.novate.Novate; 15 | import com.tamic.novate.Throwable; 16 | import com.tamic.novate.callback.ResponseCallback; 17 | 18 | import org.json.JSONObject; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | import java.util.regex.Matcher; 23 | import java.util.regex.Pattern; 24 | 25 | import okhttp3.ResponseBody; 26 | import retrofit2.Call; 27 | 28 | /** 29 | * A simple {@link Fragment} subclass. 30 | */ 31 | public class RegisterFragment extends Fragment implements View.OnClickListener { 32 | 33 | 34 | private EditText name; 35 | private EditText email; 36 | private EditText password; 37 | private Novate novate; 38 | 39 | public RegisterFragment() { 40 | // Required empty public constructor 41 | } 42 | 43 | 44 | @Override 45 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 46 | Bundle savedInstanceState) { 47 | // Inflate the layout for this fragment 48 | View view = inflater.inflate(R.layout.fragment_register, container, false); 49 | 50 | name = (EditText) view.findViewById(R.id.name); 51 | email = (EditText) view.findViewById(R.id.email); 52 | password = (EditText) view.findViewById(R.id.password); 53 | 54 | //初始化novate 55 | novate = new Novate.Builder(getContext()) 56 | .baseUrl("http://cxboss.cn/laravelandroid/public/api/") 57 | .build(); 58 | 59 | 60 | view.findViewById(R.id.signin).setOnClickListener(this); 61 | view.findViewById(R.id.register).setOnClickListener(this); 62 | view.findViewById(R.id.fanhui).setOnClickListener(this); 63 | return view; 64 | } 65 | 66 | @Override 67 | public void onClick(View v) { 68 | switch (v.getId()) { 69 | case R.id.signin: 70 | getFragmentManager().beginTransaction().replace(R.id.usermainfragment, new LoginFragment()).commit(); 71 | break; 72 | case R.id.register: 73 | register(name.getText().toString(), email.getText().toString(), password.getText().toString()); 74 | break; 75 | case R.id.fanhui: 76 | getActivity().finish(); 77 | break; 78 | } 79 | } 80 | 81 | private void register(String namestr, String emailstr, String paswordstr) { 82 | Boolean is_validate = validate(namestr, emailstr, paswordstr); 83 | Map map = new HashMap(); 84 | map.put("name", namestr); 85 | map.put("email", emailstr); 86 | map.put("password", paswordstr); 87 | //进行post请求 88 | if (is_validate) { 89 | novate.rxPost("user_register", map, new ResponseCallback() { 90 | @Override 91 | public Object onHandleResponse(ResponseBody response) throws Exception { 92 | String string = new String(response.bytes()); 93 | JSONObject user = new JSONObject(string); 94 | if (user.optBoolean("message")) { 95 | getFragmentManager().beginTransaction().replace(R.id.usermainfragment, new LoginFragment()).commit(); 96 | Toast.makeText(getContext(), "注册成功!", Toast.LENGTH_SHORT).show(); 97 | } else { 98 | System.out.println(user.optString("type")); 99 | if (user.optString("type").equals("name")) { 100 | System.out.println("hello"); 101 | name.setError(user.optString("info")); 102 | } 103 | if (user.optString("type").equals("email")) { 104 | email.setError(user.optString("info")); 105 | } 106 | } 107 | return null; 108 | } 109 | 110 | @Override 111 | public void onError(Object tag, Throwable e) { 112 | 113 | } 114 | 115 | @Override 116 | public void onCancel(Object tag, Throwable e) { 117 | 118 | } 119 | 120 | @Override 121 | public void onNext(Object tag, okhttp3.Call call, Object response) { 122 | 123 | } 124 | }); 125 | } 126 | } 127 | 128 | //验证字符格式 129 | public boolean validate(String namestr, String emailstr, String paswordstr) { 130 | String rex = "[\\w-]+[\\.\\w]*@[\\w]+(\\.[\\w]{2,3})"; 131 | Pattern p = Pattern.compile(rex); 132 | Matcher m = p.matcher(emailstr); 133 | if (namestr.length() < 3 || namestr.length() > 20) { 134 | name.setError("用户名长度为3~20"); 135 | return false; 136 | } 137 | if (m.find() == false) { 138 | email.setError("邮箱地址不正确"); 139 | return false; 140 | } 141 | if (paswordstr.length() < 6 || paswordstr.length() > 15) { 142 | password.setError("密码长度为6~15"); 143 | return false; 144 | } 145 | return true; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/Constant.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | /** 4 | * Created by Meiji on 2017/4/13. 5 | */ 6 | 7 | public class Constant { 8 | 9 | public static final String USER_AGENT_MOBILE = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"; 10 | 11 | public static final String USER_AGENT_PC = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/Interface/OnItemClickListener.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.Interface; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * Created by LaravelChen on 2017/6/12. 7 | */ 8 | 9 | public interface OnItemClickListener { 10 | void onClick(View view, int position); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/JokeContent.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.content.SharedPreferences; 6 | import android.graphics.Color; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.support.v7.widget.Toolbar; 10 | import android.view.KeyEvent; 11 | import android.view.View; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | 15 | import com.example.laravelchen.toutiao.extra.ImgLoader; 16 | 17 | 18 | public class JokeContent extends AppCompatActivity { 19 | private Intent data; 20 | private Toolbar toolbar; 21 | private String image_url; 22 | private String name; 23 | private String text; 24 | private String source; 25 | private String comment_count; 26 | private ImageView imageView; 27 | private TextView tvTitle; 28 | private TextView tvContent; 29 | private TextView tvExtra; 30 | private SharedPreferences sp; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_joke_content); 36 | toolbar = (Toolbar) findViewById(R.id.contentToolbar); 37 | toolbar.setTitle("今日头条 - 段子内容"); 38 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { 39 | @Override 40 | public void onClick(View v) { 41 | finish(); 42 | } 43 | }); 44 | setSupportActionBar(toolbar); 45 | sp=getSharedPreferences("user_auth", Activity.MODE_PRIVATE); 46 | toolbar.setBackgroundColor(Color.parseColor(sp.getString("theme", "#3F51B5"))); 47 | initView(); 48 | } 49 | 50 | 51 | private void initView() { 52 | data = getIntent(); 53 | image_url = data.getStringExtra("image_url"); 54 | name = data.getStringExtra("name"); 55 | text = data.getStringExtra("text"); 56 | source = data.getStringExtra("source"); 57 | comment_count = data.getStringExtra("comment_count"); 58 | imageView = (ImageView) findViewById(R.id.joke_image); 59 | tvTitle = (TextView) findViewById(R.id.joketitle); 60 | tvContent = (TextView) findViewById(R.id.joke_content); 61 | tvExtra = (TextView) findViewById(R.id.joke_extra); 62 | new ImgLoader(this).disPlayimg(image_url,imageView); 63 | tvTitle.setText(name); 64 | tvContent.setText(text); 65 | tvExtra.setText("来源:" + source + " ~ " + "共" + comment_count + "条评论"); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/PathRandom.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * Created by LaravelChen on 2017/6/22. 7 | */ 8 | 9 | public class PathRandom { 10 | private Random random; 11 | private static final String[] homepath = { 12 | "https://www.toutiao.com/api/article/feed/?category=news_hot&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A11539B37F25C95&cp=593FA5BC29856E1", 13 | "https://www.toutiao.com/api/article/feed/?category=news_society&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A11539B37F25C95&cp=593FA5BC29856E1", 14 | "https://www.toutiao.com/api/article/feed/?category=news_entertainment&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A11539B37F25C95&cp=593FA5BC29856E1", 15 | "https://www.toutiao.com/api/article/feed/?category=news_tech&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A11539B37F25C95&cp=594BDA7327C3FE1", 16 | "https://www.toutiao.com/api/article/feed/?category=news_sports&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1C52904FB1A3B1&cp=594B0A339B915E1", 17 | "https://www.toutiao.com/api/article/feed/?category=news_finance&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A17599547B4A3F7&cp=594B2A233FC7EE1", 18 | "https://www.toutiao.com/api/article/feed/?category=news_world&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1750974FBEA42C&cp=594BDAA4E2EC5E1", 19 | }; 20 | private static final String[] photopath = { 21 | "https://www.toutiao.com/api/article/feed/?category=gallery_photograthy&utm_source=toutiao&max_behot_time=0&as=A1F549836F636E8&cp=593F23967E985E1", 22 | "http://www.toutiao.com/api/article/feed/?category=%E7%BB%84%E5%9B%BE&utm_source=toutiao&max_behot_time=0&as=A195B9640B4A5D9&cp=594BDAF54D498E1", 23 | "http://www.toutiao.com/api/article/feed/?category=gallery_old_picture&utm_source=toutiao&max_behot_time=0&as=A1C579E48B1A73D&cp=594BBA57E30DEE1", 24 | }; 25 | private static final String[] videopath={ 26 | "https://www.toutiao.com/api/article/feed/?category=video&utm_source=toutiao&max_behot_time=0&as=A1F549836F636E8&cp=593F23967E985E1", 27 | "https://www.toutiao.com/api/article/feed/?category=subv_voice&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1F549836F636E8&cp=593F23967E985E1", 28 | "https://www.toutiao.com/api/article/feed/?category=subv_funny&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1855934BBAAB1E&cp=594B3A9BD1DE5E1", 29 | "https://www.toutiao.com/api/article/feed/?category=subv_society&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A15529E4AB7AB4B&cp=594B5A5BA44B7E1", 30 | "https://www.toutiao.com/api/article/feed/?category=%E4%B8%AD%E5%9B%BD%E6%96%B0%E5%94%B1%E5%B0%86&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1C559B4BBCABD6&cp=594BBA7B5DE6BE1", 31 | "https://www.toutiao.com/api/article/feed/?category=subv_entertainment&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1A5D934FB4AC92&cp=594B1AFC29929E1", 32 | }; 33 | 34 | public String getHomePath() { 35 | random = new Random(); 36 | int i = random.nextInt(homepath.length); 37 | return homepath[i]; 38 | } 39 | 40 | public String getPhotoPath() { 41 | random = new Random(); 42 | int i = random.nextInt(photopath.length); 43 | return photopath[i]; 44 | } 45 | 46 | public String getVideoPath(){ 47 | random = new Random(); 48 | int i = random.nextInt(videopath.length); 49 | return videopath[i]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/PhotoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.content.SharedPreferences; 6 | import android.graphics.Bitmap; 7 | import android.graphics.BitmapFactory; 8 | import android.graphics.Color; 9 | import android.os.Handler; 10 | import android.os.Message; 11 | import android.support.v4.view.PagerAdapter; 12 | import android.support.v4.view.ViewPager; 13 | import android.support.v4.widget.SwipeRefreshLayout; 14 | import android.support.v7.app.AppCompatActivity; 15 | import android.os.Bundle; 16 | import android.support.v7.widget.LinearLayoutManager; 17 | import android.support.v7.widget.RecyclerView; 18 | import android.support.v7.widget.Toolbar; 19 | import android.util.JsonReader; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.view.ViewGroup; 23 | import android.widget.ImageView; 24 | import android.widget.TextView; 25 | import android.widget.Toast; 26 | 27 | import com.dou361.dialogui.DialogUIUtils; 28 | import com.example.laravelchen.toutiao.extra.ImgLoader; 29 | import com.example.laravelchen.toutiao.extra.ImgUtils; 30 | import com.nostra13.universalimageloader.core.ImageLoader; 31 | 32 | import org.json.JSONArray; 33 | import org.json.JSONException; 34 | import org.json.JSONObject; 35 | import org.jsoup.Jsoup; 36 | import org.jsoup.nodes.Document; 37 | import org.jsoup.nodes.Element; 38 | import org.jsoup.select.Elements; 39 | 40 | import java.io.IOException; 41 | import java.io.InputStream; 42 | import java.io.StringReader; 43 | import java.net.HttpURLConnection; 44 | import java.net.URL; 45 | import java.util.ArrayList; 46 | import java.util.List; 47 | 48 | import okhttp3.MediaType; 49 | import okhttp3.OkHttpClient; 50 | import okhttp3.Request; 51 | import okhttp3.RequestBody; 52 | import okhttp3.Response; 53 | import okhttp3.internal.http2.Header; 54 | 55 | public class PhotoActivity extends AppCompatActivity { 56 | public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 57 | private static String PATH; 58 | private Intent data; 59 | private String json; 60 | private int now; 61 | private String image_list; 62 | private Toolbar toolbar; 63 | private ViewPager viewPager; 64 | private ImageView[] imageViews; 65 | private List imgIdArray; 66 | private TextView tv_count; 67 | private TextView tv_save; 68 | private SharedPreferences sp; 69 | 70 | @Override 71 | protected void onCreate(Bundle savedInstanceState) { 72 | super.onCreate(savedInstanceState); 73 | setContentView(R.layout.activity_photo); 74 | 75 | toolbar = (Toolbar) findViewById(R.id.contentToolbar); 76 | toolbar.setTitle("今日头条 - 图片内容"); 77 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { 78 | @Override 79 | public void onClick(View v) { 80 | finish(); 81 | } 82 | }); 83 | setSupportActionBar(toolbar); 84 | tv_count = (TextView) findViewById(R.id.image_count); 85 | tv_save = (TextView) findViewById(R.id.image_save); 86 | sp=getSharedPreferences("user_auth", Activity.MODE_PRIVATE); 87 | toolbar.setBackgroundColor(Color.parseColor(sp.getString("theme", "#3F51B5"))); 88 | 89 | //初始化整个图片当前的位置(很重要的一个flag) 90 | now = 1; 91 | //设置viewpager 92 | viewPager = (ViewPager) findViewById(R.id.viewPager); 93 | imgIdArray = new ArrayList(); 94 | data = getIntent(); 95 | PATH = data.getStringExtra("share_url"); 96 | //启动运行 97 | createThread(); 98 | 99 | //点击保存 100 | tv_save.setOnClickListener(new View.OnClickListener() { 101 | @Override 102 | public void onClick(View v) { 103 | String url = imgIdArray.get(now - 1); 104 | ImageLoader imageLoader = ImageLoader.getInstance(); 105 | Bitmap bmp = imageLoader.loadImageSync(url); 106 | Boolean is_save = new ImgUtils().saveImageToGallery(PhotoActivity.this, bmp); 107 | if (is_save) { 108 | Toast.makeText(PhotoActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 109 | }else{ 110 | Toast.makeText(PhotoActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); 111 | } 112 | } 113 | }); 114 | } 115 | 116 | public void createThread() { 117 | new Thread() { 118 | @Override 119 | public void run() { 120 | postJson(); 121 | } 122 | }.start(); 123 | } 124 | 125 | private void postJson() { 126 | //申明给服务端传递一个json串 127 | //创建一个OkHttpClient对象 128 | OkHttpClient okHttpClient = new OkHttpClient(); 129 | //创建一个RequestBody(参数1:数据类型 参数2传递的json串) 130 | RequestBody requestBody = RequestBody.create(JSON, "photo"); 131 | //创建一个请求对象 132 | Request request = new Request.Builder() 133 | .url(PATH) 134 | .header("User-Agent", Constant.USER_AGENT_MOBILE) 135 | .post(requestBody) 136 | .build(); 137 | //发送请求获取响应 138 | try { 139 | Response response = okHttpClient.newCall(request).execute(); 140 | //判断请求是否成功 141 | if (response.isSuccessful()) { 142 | String content = response.body().string(); 143 | Message msg = new Message(); 144 | msg.obj = content; 145 | handler.sendMessage(msg); 146 | } 147 | } catch (IOException e) { 148 | e.printStackTrace(); 149 | } 150 | 151 | } 152 | 153 | Handler handler = new Handler() { 154 | public void handleMessage(Message msg) { 155 | String str = msg.obj + ""; 156 | JSONObject root = null; 157 | image_list = parseHTML(str); 158 | try { 159 | root = new JSONObject(image_list); 160 | JSONArray ary = root.getJSONArray("sub_images"); 161 | for (int i = 0; i < ary.length() - 1; i++) { 162 | JSONObject root1 = ary.getJSONObject(i); 163 | imgIdArray.add(i, root1.optString("url")); 164 | } 165 | imageViews = new ImageView[imgIdArray.size()]; 166 | tv_count.setText(1 + "/" + imageViews.length); 167 | //设置图片 168 | for (int i = 0; i < imageViews.length; i++) { 169 | ImageView imageView = new ImageView(PhotoActivity.this); 170 | imageViews[i] = imageView; 171 | new ImgLoader(PhotoActivity.this).disPlayimg(imgIdArray.get(i), imageView); 172 | } 173 | viewPager.setAdapter(new MyAdapter()); 174 | viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 175 | @Override 176 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 177 | } 178 | 179 | @Override 180 | public void onPageSelected(int position) { 181 | System.out.println(position); 182 | now = position + 1; 183 | tv_count.setText(now + "/" + imageViews.length); 184 | } 185 | 186 | @Override 187 | public void onPageScrollStateChanged(int state) { 188 | 189 | } 190 | }); 191 | } catch (JSONException e) { 192 | e.printStackTrace(); 193 | } 194 | } 195 | }; 196 | 197 | private String parseHTML(String HTML) { 198 | boolean flag = false; 199 | Document doc = Jsoup.parse(HTML); 200 | // 取得所有的script tag 201 | Elements scripts = doc.getElementsByTag("script"); 202 | for (Element e : scripts) { 203 | // 过滤字符串 204 | String script = e.toString(); 205 | if (script.contains("var gallery = {")) { 206 | // 只取得script的內容 207 | script = e.childNode(0).toString(); 208 | // 取得JS变量数组 209 | String[] vars = script.split("var "); 210 | // 取得单个JS变量 211 | for (String var : vars) { 212 | // 取到满足条件的JS变量 213 | if (var.contains("gallery = ")) { 214 | int start = var.indexOf("="); 215 | int end = var.lastIndexOf(";"); 216 | json = var.substring(start + 1, end + 1); 217 | } 218 | } 219 | } 220 | } 221 | return json; 222 | } 223 | 224 | public class MyAdapter extends PagerAdapter { 225 | @Override 226 | public int getCount() { 227 | return imageViews.length; 228 | } 229 | 230 | @Override 231 | public boolean isViewFromObject(View view, Object object) { 232 | return view == object; 233 | } 234 | 235 | @Override 236 | public void destroyItem(ViewGroup container, int position, Object object) { 237 | ((ViewPager) container).removeView(imageViews[position]); 238 | } 239 | 240 | @Override 241 | public Object instantiateItem(ViewGroup container, int position) { 242 | try { 243 | ((ViewPager) container).addView(imageViews[position], 0); 244 | } catch (Exception e) { 245 | //handler something 246 | } 247 | return imageViews[position]; 248 | } 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/UserLoginAndRegister.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.example.laravelchen.toutiao.Auth.LoginFragment; 7 | 8 | public class UserLoginAndRegister extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_user_login_and_register); 14 | setDefaultFragment(); 15 | } 16 | 17 | private void setDefaultFragment() { 18 | getSupportFragmentManager().beginTransaction().replace(R.id.usermainfragment,new LoginFragment()).commit(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/alladapter/JokeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.alladapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | 11 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 12 | import com.example.laravelchen.toutiao.R; 13 | import com.example.laravelchen.toutiao.allbean.JokeBean; 14 | import com.example.laravelchen.toutiao.extra.ImgLoader; 15 | 16 | import java.util.Collections; 17 | import java.util.List; 18 | 19 | /** 20 | * Created by LaravelChen on 2017/6/11. 21 | */ 22 | 23 | public class JokeAdapter extends RecyclerView.Adapter { 24 | 25 | private List lists; 26 | 27 | private Context context; 28 | public JokeAdapter(List lists,Context context) { 29 | this.lists = lists; 30 | this.context=context; 31 | } 32 | 33 | public void add(List newlist) { 34 | Collections.addAll(newlist); 35 | } 36 | private OnItemClickListener onItemClickListener; 37 | //点击接口 38 | public void setOnItemClickListener(OnItemClickListener listener) { 39 | this.onItemClickListener = listener; 40 | } 41 | class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 42 | private TextView jokeExtra; 43 | private ImageView jokeImg1; 44 | private TextView jokename; 45 | private TextView jokecontent; 46 | private OnItemClickListener onItemClickListener; 47 | public ViewHolder(View itemView, OnItemClickListener onItemClickListener) { 48 | super(itemView); 49 | jokeImg1 = (ImageView) itemView.findViewById(R.id.joke_image); 50 | jokeExtra = (TextView) itemView.findViewById(R.id.joke_extra); 51 | jokename = (TextView) itemView.findViewById(R.id.joke_name); 52 | jokecontent=(TextView) itemView.findViewById(R.id.joke_content); 53 | //设置点击事件 54 | this.onItemClickListener = onItemClickListener; 55 | itemView.setOnClickListener(this); 56 | } 57 | 58 | @Override 59 | public void onClick(View v) { 60 | if (onItemClickListener != null) { 61 | onItemClickListener.onClick(v, getLayoutPosition()); 62 | } 63 | } 64 | } 65 | 66 | @Override 67 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 68 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.joke_main, parent, false); 69 | return new ViewHolder(view,onItemClickListener); 70 | } 71 | 72 | @Override 73 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 74 | ViewHolder vh = (ViewHolder) holder; 75 | vh.jokecontent.setText(lists.get(position).getText()); 76 | vh.jokeExtra.setText(lists.get(position).getSource() + " - " + lists.get(position).getComment_count() + "条评论"); 77 | new ImgLoader(context).disPlayimg(lists.get(position).getAvatar_url(),vh.jokeImg1); 78 | vh.jokename.setText(lists.get(position).getName()); 79 | } 80 | 81 | @Override 82 | public int getItemCount() { 83 | return lists.size(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/alladapter/MyFragmentPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.alladapter; 2 | 3 | import android.content.Context; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.app.FragmentPagerAdapter; 7 | import android.view.ViewGroup; 8 | 9 | import com.example.laravelchen.toutiao.allfragment.PageFragment; 10 | 11 | /** 12 | * Created by LaravelChen on 2017/6/8. 13 | */ 14 | 15 | public class MyFragmentPagerAdapter extends FragmentPagerAdapter { 16 | private String[] titles = new String[]{"推荐","热点","娱乐","科技","体育","军事","图片"}; 17 | public int COUNT = titles.length; 18 | private Context context; 19 | 20 | public MyFragmentPagerAdapter(FragmentManager fm, Context context) { 21 | super(fm); 22 | this.context = context; 23 | } 24 | 25 | 26 | 27 | @Override 28 | public Fragment getItem(int position) { 29 | return PageFragment.newInstance(); 30 | } 31 | 32 | @Override 33 | public int getCount() { 34 | return COUNT; 35 | } 36 | 37 | @Override 38 | public CharSequence getPageTitle(int position) { 39 | return titles[position]; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/alladapter/PhotoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.alladapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | 11 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 12 | import com.example.laravelchen.toutiao.R; 13 | import com.example.laravelchen.toutiao.allbean.PhotoBean; 14 | import com.example.laravelchen.toutiao.extra.ImgLoader; 15 | 16 | 17 | import java.util.Collections; 18 | import java.util.List; 19 | 20 | /** 21 | * Created by LaravelChen on 2017/6/12. 22 | */ 23 | 24 | public class PhotoAdapter extends RecyclerView.Adapter { 25 | private List lists; 26 | private Context context; 27 | 28 | public PhotoAdapter(List lists,Context context) { 29 | this.lists = lists; 30 | this.context=context; 31 | } 32 | 33 | private OnItemClickListener onItemClickListener; 34 | 35 | public void add(List newlist) { 36 | Collections.addAll(newlist); 37 | } 38 | 39 | //点击接口 40 | public void setOnItemClickListener(OnItemClickListener listener) { 41 | this.onItemClickListener = listener; 42 | } 43 | 44 | class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 45 | private TextView photoTitle; 46 | private TextView photoExtra; 47 | private ImageView img1; 48 | private OnItemClickListener onItemClickListener; 49 | 50 | public ViewHolder(View itemView, OnItemClickListener onItemClickListener) { 51 | super(itemView); 52 | photoTitle = (TextView) itemView.findViewById(R.id.photo_title); 53 | img1 = (ImageView) itemView.findViewById(R.id.photo_image); 54 | photoExtra = (TextView) itemView.findViewById(R.id.photo_extra); 55 | 56 | //设置点击事件 57 | this.onItemClickListener = onItemClickListener; 58 | itemView.setOnClickListener(this); 59 | } 60 | 61 | 62 | @Override 63 | public void onClick(View v) { 64 | if (onItemClickListener != null) { 65 | onItemClickListener.onClick(v, getLayoutPosition()); 66 | } 67 | } 68 | } 69 | 70 | @Override 71 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 72 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo_main, parent, false); 73 | return new ViewHolder(view, onItemClickListener); 74 | } 75 | 76 | @Override 77 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 78 | ViewHolder vh = (ViewHolder) holder; 79 | vh.photoTitle.setText(lists.get(position).getTitle()); 80 | vh.photoExtra.setText(lists.get(position).getSource() + " - 共" + lists.get(position).getGallary_image_count() + "张图片"); 81 | new ImgLoader(context).disPlayimg(lists.get(position).getImg1(),vh.img1); 82 | } 83 | 84 | @Override 85 | public int getItemCount() { 86 | return lists.size(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/alladapter/RecommendAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.alladapter; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 13 | import com.example.laravelchen.toutiao.R; 14 | import com.example.laravelchen.toutiao.allbean.RecommendBean; 15 | import com.example.laravelchen.toutiao.extra.ImgLoader; 16 | 17 | import com.nostra13.universalimageloader.core.DisplayImageOptions; 18 | import com.nostra13.universalimageloader.core.ImageLoader; 19 | import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 20 | 21 | import java.util.Collections; 22 | import java.util.List; 23 | 24 | /** 25 | * Created by LaravelChen on 2017/6/10. 26 | */ 27 | 28 | public class RecommendAdapter extends RecyclerView.Adapter { 29 | private List lists; 30 | private OnItemClickListener onItemClickListener; 31 | private Context context; 32 | 33 | public RecommendAdapter(List lists, Context context) { 34 | this.lists = lists; 35 | this.context=context; 36 | } 37 | 38 | public void add(List newlist) { 39 | Collections.addAll(newlist); 40 | } 41 | 42 | //点击接口 43 | public void setOnItemClickListener(OnItemClickListener listener) { 44 | this.onItemClickListener = listener; 45 | } 46 | 47 | //有图片的模板 48 | class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 49 | private TextView hotTitle; 50 | private TextView hotExtra; 51 | private ImageView hotImg1; 52 | private ImageView hotImg2; 53 | private ImageView hotImg3; 54 | private OnItemClickListener onItemClickListener; 55 | 56 | public ViewHolder(View itemView, OnItemClickListener onItemClickListener) { 57 | super(itemView); 58 | hotTitle = (TextView) itemView.findViewById(R.id.hot_title); 59 | hotExtra = (TextView) itemView.findViewById(R.id.hot_extra); 60 | hotImg1 = (ImageView) itemView.findViewById(R.id.hot_image); 61 | hotImg2 = (ImageView) itemView.findViewById(R.id.hot_image2); 62 | hotImg3 = (ImageView) itemView.findViewById(R.id.hot_image3); 63 | 64 | //设置点击事件 65 | this.onItemClickListener = onItemClickListener; 66 | itemView.setOnClickListener(this); 67 | } 68 | 69 | @Override 70 | public void onClick(View v) { 71 | if (onItemClickListener != null) { 72 | onItemClickListener.onClick(v, getLayoutPosition()); 73 | } 74 | } 75 | } 76 | 77 | //没有图片的模板 78 | class NoImageView extends RecyclerView.ViewHolder implements View.OnClickListener { 79 | private TextView hotTitle; 80 | private TextView hotExtra; 81 | private OnItemClickListener onItemClickListener = null; 82 | 83 | public NoImageView(View itemView, OnItemClickListener onItemClickListener) { 84 | super(itemView); 85 | hotTitle = (TextView) itemView.findViewById(R.id.hot_title); 86 | hotExtra = (TextView) itemView.findViewById(R.id.hot_extra); 87 | 88 | //点击事件 89 | this.onItemClickListener = onItemClickListener; 90 | itemView.setOnClickListener(this); 91 | } 92 | 93 | @Override 94 | public void onClick(View v) { 95 | if (onItemClickListener != null) { 96 | onItemClickListener.onClick(v, getLayoutPosition()); 97 | } 98 | } 99 | } 100 | 101 | @Override 102 | public int getItemViewType(int position) { 103 | //判断有没有图片 104 | if (lists.get(position).isHasimg()) { 105 | return 1; 106 | } 107 | return 0; 108 | } 109 | 110 | @Override 111 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 112 | if (viewType == 1) { 113 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_home_has_image, parent, false); 114 | return new ViewHolder(view, onItemClickListener); 115 | } 116 | if (viewType == 0) { 117 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_home_no_image, parent, false); 118 | return new NoImageView(view, onItemClickListener); 119 | } 120 | return null; 121 | } 122 | 123 | @Override 124 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 125 | if (holder instanceof ViewHolder) { 126 | ViewHolder vh = (ViewHolder) holder; 127 | vh.hotTitle.setText(lists.get(position).getTitle()); 128 | vh.hotExtra.setText(lists.get(position).getSource() + " - " + lists.get(position).getComment_couont() + "条评论"); 129 | if (lists.get(position).getImg() != null) { 130 | new ImgLoader(context).disPlayimg(lists.get(position).getImg(),vh.hotImg1); 131 | new ImgLoader(context).disPlayimg(lists.get(position).getImg2(),vh.hotImg2); 132 | new ImgLoader(context).disPlayimg(lists.get(position).getImg3(),vh.hotImg3); 133 | } 134 | } 135 | if (holder instanceof NoImageView) { 136 | NoImageView vh = (NoImageView) holder; 137 | vh.hotTitle.setText(lists.get(position).getTitle()); 138 | vh.hotExtra.setText(lists.get(position).getSource() + " - " + lists.get(position).getComment_couont() + "条评论"); 139 | } 140 | } 141 | 142 | @Override 143 | public int getItemCount() { 144 | return lists.size(); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/alladapter/VideoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.alladapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | 11 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 12 | import com.example.laravelchen.toutiao.R; 13 | import com.example.laravelchen.toutiao.allbean.VideoBean; 14 | import com.example.laravelchen.toutiao.extra.ImgLoader; 15 | 16 | 17 | import java.util.Collections; 18 | import java.util.List; 19 | 20 | /** 21 | * Created by LaravelChen on 2017/6/11. 22 | */ 23 | 24 | public class VideoAdapter extends RecyclerView.Adapter { 25 | private List lists; 26 | private Context context; 27 | public VideoAdapter(List lists,Context context) { 28 | this.lists = lists; 29 | this.context=context; 30 | } 31 | private OnItemClickListener onItemClickListener; 32 | public void add(List newlist) { 33 | Collections.addAll(newlist); 34 | } 35 | 36 | //点击接口 37 | public void setOnItemClickListener(OnItemClickListener listener) { 38 | this.onItemClickListener = listener; 39 | } 40 | class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 41 | private TextView videoTitle; 42 | private TextView videoExtra; 43 | private ImageView videoImg1; 44 | private OnItemClickListener onItemClickListener; 45 | 46 | public ViewHolder(View itemView, OnItemClickListener onItemClickListener) { 47 | super(itemView); 48 | videoTitle = (TextView) itemView.findViewById(R.id.video_title); 49 | videoImg1 = (ImageView) itemView.findViewById(R.id.video_image); 50 | videoExtra = (TextView) itemView.findViewById(R.id.video_extra); 51 | //设置点击事件 52 | this.onItemClickListener = onItemClickListener; 53 | itemView.setOnClickListener(this); 54 | } 55 | 56 | @Override 57 | public void onClick(View v) { 58 | if (onItemClickListener != null) { 59 | onItemClickListener.onClick(v, getLayoutPosition()); 60 | } 61 | } 62 | } 63 | 64 | @Override 65 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 66 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_main, parent, false); 67 | return new ViewHolder(view,onItemClickListener); 68 | } 69 | 70 | @Override 71 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 72 | ViewHolder vh = (ViewHolder) holder; 73 | vh.videoTitle.setText(lists.get(position).getTitle()); 74 | vh.videoExtra.setText(lists.get(position).getSource() + " - " + lists.get(position).getComment_count() + "条评论"); 75 | new ImgLoader(context).disPlayimg(lists.get(position).getImage_url(),vh.videoImg1); 76 | } 77 | 78 | @Override 79 | public int getItemCount() { 80 | return lists.size(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allbean/JokeBean.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allbean; 2 | 3 | /** 4 | * Created by LaravelChen on 2017/6/11. 5 | */ 6 | 7 | public class JokeBean { 8 | 9 | private String text; 10 | private String avatar_url; 11 | private String comment_count; 12 | private String name; 13 | private String source; 14 | private String share_url; 15 | 16 | public String getShare_url() { 17 | return share_url; 18 | } 19 | 20 | public void setShare_url(String share_url) { 21 | this.share_url = share_url; 22 | } 23 | 24 | public String getText() { 25 | return text; 26 | } 27 | 28 | public void setText(String text) { 29 | this.text = text; 30 | } 31 | 32 | public String getAvatar_url() { 33 | return avatar_url; 34 | } 35 | 36 | public void setAvatar_url(String avatar_url) { 37 | this.avatar_url = avatar_url; 38 | } 39 | 40 | 41 | public String getComment_count() { 42 | return comment_count; 43 | } 44 | 45 | public void setComment_count(String comment_count) { 46 | this.comment_count = comment_count; 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public void setName(String name) { 54 | this.name = name; 55 | } 56 | 57 | public String getSource() { 58 | return source; 59 | } 60 | 61 | public void setSource(String source) { 62 | this.source = source; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allbean/PhotoBean.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allbean; 2 | 3 | /** 4 | * Created by LaravelChen on 2017/6/12. 5 | */ 6 | 7 | public class PhotoBean { 8 | private String title; 9 | private String img1; 10 | private String source; 11 | private String tag; 12 | private String gallary_image_count; 13 | private String group_id; 14 | 15 | public String getGroup_id() { 16 | return group_id; 17 | } 18 | 19 | public void setGroup_id(String group_id) { 20 | this.group_id = group_id; 21 | } 22 | 23 | public String getGallary_image_count() { 24 | return gallary_image_count; 25 | } 26 | 27 | public void setGallary_image_count(String gallary_image_count) { 28 | this.gallary_image_count = gallary_image_count; 29 | } 30 | 31 | public String getTitle() { 32 | return title; 33 | } 34 | 35 | public void setTitle(String title) { 36 | this.title = title; 37 | } 38 | 39 | public String getImg1() { 40 | return img1; 41 | } 42 | 43 | public void setImg1(String img1) { 44 | this.img1 = img1; 45 | } 46 | 47 | public String getSource() { 48 | return source; 49 | } 50 | 51 | public void setSource(String source) { 52 | this.source = source; 53 | } 54 | 55 | public String getTag() { 56 | return tag; 57 | } 58 | 59 | public void setTag(String tag) { 60 | this.tag = tag; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allbean/RecommendBean.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allbean; 2 | 3 | import org.json.JSONArray; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by LaravelChen on 2017/6/10. 9 | */ 10 | 11 | public class RecommendBean { 12 | private String title; 13 | private String img; 14 | private String img2; 15 | private String img3; 16 | private String source; 17 | private boolean hasimg; 18 | private String comment_couont; 19 | private String share_url; 20 | private String group_id; 21 | 22 | public String getGroup_id() { 23 | return group_id; 24 | } 25 | 26 | public void setGroup_id(String group_id) { 27 | this.group_id = group_id; 28 | } 29 | 30 | public String getShare_url() { 31 | return share_url; 32 | } 33 | 34 | public void setShare_url(String share_url) { 35 | this.share_url = share_url; 36 | } 37 | 38 | public String getImg2() { 39 | return img2; 40 | } 41 | 42 | public void setImg2(String img2) { 43 | this.img2 = img2; 44 | } 45 | 46 | public String getImg3() { 47 | return img3; 48 | } 49 | 50 | public void setImg3(String img3) { 51 | this.img3 = img3; 52 | } 53 | 54 | public boolean isHasimg() { 55 | return hasimg; 56 | } 57 | 58 | public void setHasimg(boolean hasimg) { 59 | this.hasimg = hasimg; 60 | } 61 | 62 | public String getImg() { 63 | return img; 64 | } 65 | 66 | public void setImg(String img) { 67 | this.img = img; 68 | } 69 | 70 | public String getSource() { 71 | return source; 72 | } 73 | 74 | public void setSource(String source) { 75 | this.source = source; 76 | } 77 | 78 | public String getComment_couont() { 79 | return comment_couont; 80 | } 81 | 82 | public void setComment_couont(String comment_couont) { 83 | this.comment_couont = comment_couont; 84 | } 85 | 86 | public String getTitle() { 87 | return title; 88 | } 89 | 90 | public void setTitle(String title) { 91 | this.title = title; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allbean/VideoBean.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allbean; 2 | 3 | /** 4 | * Created by LaravelChen on 2017/6/11. 5 | */ 6 | 7 | public class VideoBean { 8 | private String title; 9 | private String source; 10 | private String comment_count; 11 | private String image_url; 12 | private String source_url; 13 | 14 | public String getSource_url() { 15 | return source_url; 16 | } 17 | 18 | public void setSource_url(String source_url) { 19 | this.source_url = source_url; 20 | } 21 | 22 | public String getTitle() { 23 | return title; 24 | } 25 | 26 | public void setTitle(String title) { 27 | this.title = title; 28 | } 29 | 30 | public String getSource() { 31 | return source; 32 | } 33 | 34 | public void setSource(String source) { 35 | this.source = source; 36 | } 37 | 38 | public String getComment_count() { 39 | return comment_count; 40 | } 41 | 42 | public void setComment_count(String comment_count) { 43 | this.comment_count = comment_count; 44 | } 45 | 46 | public String getImage_url() { 47 | return image_url; 48 | } 49 | 50 | public void setImage_url(String image_url) { 51 | this.image_url = image_url; 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/HomeFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.os.Handler; 6 | import android.os.Message; 7 | import android.support.design.widget.TabLayout; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.view.ViewPager; 10 | import android.support.v4.widget.SwipeRefreshLayout; 11 | import android.support.v7.widget.LinearLayoutManager; 12 | import android.support.v7.widget.RecyclerView; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.widget.Toast; 17 | 18 | import com.example.laravelchen.toutiao.Constant; 19 | import com.example.laravelchen.toutiao.alladapter.MyFragmentPagerAdapter; 20 | import com.example.laravelchen.toutiao.R; 21 | import com.example.laravelchen.toutiao.alladapter.RecommendAdapter; 22 | import com.example.laravelchen.toutiao.allbean.RecommendBean; 23 | 24 | import org.json.JSONArray; 25 | import org.json.JSONException; 26 | import org.json.JSONObject; 27 | 28 | import java.io.IOException; 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | import okhttp3.MediaType; 33 | import okhttp3.OkHttpClient; 34 | import okhttp3.Request; 35 | import okhttp3.RequestBody; 36 | import okhttp3.Response; 37 | 38 | 39 | /** 40 | * A simple {@link Fragment} subclass. 41 | */ 42 | public class HomeFragment extends Fragment { 43 | private View rootView; 44 | private MyFragmentPagerAdapter adapter; 45 | 46 | public HomeFragment() { 47 | 48 | } 49 | 50 | @Override 51 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 52 | Bundle savedInstanceState) { 53 | rootView = inflater.inflate(R.layout.fragment_home, container, false); 54 | initView(rootView); 55 | return rootView; 56 | } 57 | 58 | public void initView(View rootView) { 59 | ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewPager); 60 | //关键的一个知识点getChidFragmentManager 61 | adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), getContext()); 62 | viewPager.setAdapter(adapter); 63 | viewPager.setOffscreenPageLimit(7); 64 | 65 | //TabLayout 66 | TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout); 67 | tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 68 | tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 69 | //显示当前那个标签页 70 | // viewPager.setCurrentItem(1); 71 | tabLayout.setupWithViewPager(viewPager); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/JokeFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.Message; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.widget.SwipeRefreshLayout; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | 16 | import com.example.laravelchen.toutiao.ArticleContentShow; 17 | import com.example.laravelchen.toutiao.Constant; 18 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 19 | import com.example.laravelchen.toutiao.JokeContent; 20 | import com.example.laravelchen.toutiao.R; 21 | import com.example.laravelchen.toutiao.alladapter.JokeAdapter; 22 | import com.example.laravelchen.toutiao.alladapter.VideoAdapter; 23 | import com.example.laravelchen.toutiao.allbean.JokeBean; 24 | import com.example.laravelchen.toutiao.allbean.VideoBean; 25 | 26 | import org.json.JSONArray; 27 | import org.json.JSONException; 28 | import org.json.JSONObject; 29 | 30 | import java.io.IOException; 31 | import java.util.ArrayList; 32 | import java.util.List; 33 | 34 | import okhttp3.MediaType; 35 | import okhttp3.OkHttpClient; 36 | import okhttp3.Request; 37 | import okhttp3.RequestBody; 38 | import okhttp3.Response; 39 | 40 | 41 | /** 42 | * A simple {@link Fragment} subclass. 43 | */ 44 | public class JokeFragment extends Fragment { 45 | 46 | private static final String PATH = "https://www.toutiao.com/api/article/feed/?category=essay_joke&utm_source=toutiao&max_behot_time=0&as=A105177907376A5&cp=5797C7865AD54E1"; 47 | // private static String PATH="http://www.toutiao.com/api/article/feed/?category=essay_joke&utm_source=toutiao&widen=1&max_behot_time=1498129739&max_behot_time_tmp=1498129739&tadrequire=true&as=A1B5A974DB6A55D&cp=594B0A25450D8E1"; 48 | private List newList; 49 | private JokeAdapter adapter; 50 | private RecyclerView rv; 51 | private JokeBean mainAdapterBean; 52 | private SwipeRefreshLayout swipeRefreshLayout; 53 | private View view; 54 | public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 55 | 56 | public JokeFragment() { 57 | // Required empty public constructor 58 | } 59 | 60 | 61 | @Override 62 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 63 | Bundle savedInstanceState) { 64 | // Inflate the layout for this fragment 65 | view = inflater.inflate(R.layout.fragment_wei_tou_tiao, container, false); 66 | initView(); 67 | return view; 68 | } 69 | 70 | 71 | public void initView() { 72 | newList = new ArrayList(); 73 | adapter = new JokeAdapter(newList,getContext()); 74 | rv = (RecyclerView) view.findViewById(R.id.rv); 75 | swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 76 | swipeRefreshLayout.setRefreshing(true); 77 | createThread(); 78 | seeRefresh(); 79 | } 80 | 81 | //设置刷新 82 | public void seeRefresh() { 83 | // 设置下拉进度的背景颜色,默认就是白色的 84 | swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); 85 | // 设置下拉进度的主题颜色 86 | swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); 87 | //监听刷新 88 | swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 89 | @Override 90 | public void onRefresh() { 91 | createThread(); 92 | } 93 | }); 94 | } 95 | 96 | 97 | //创建进程 98 | public void createThread() { 99 | new Thread() { 100 | public void run() { 101 | postJson(); 102 | } 103 | }.start(); 104 | } 105 | 106 | //获取数据 107 | private void postJson() { 108 | //申明给服务端传递一个json串 109 | //创建一个OkHttpClient对象 110 | OkHttpClient okHttpClient = new OkHttpClient(); 111 | //创建一个RequestBody(参数1:数据类型 参数2传递的json串) 112 | RequestBody requestBody = RequestBody.create(JSON, "joke"); 113 | //创建一个请求对象 114 | Request request = new Request.Builder() 115 | .url(PATH) 116 | .header("User-Agent", Constant.USER_AGENT_MOBILE) 117 | .addHeader("Accept", "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") 118 | .post(requestBody) 119 | .build(); 120 | //发送请求获取响应 121 | try { 122 | Response response = okHttpClient.newCall(request).execute(); 123 | //判断请求是否成功 124 | if (response.isSuccessful()) { 125 | String content = response.body().string(); 126 | Message msg = new Message(); 127 | msg.obj = content; 128 | handler.sendMessage(msg); 129 | } 130 | } catch (IOException e) { 131 | e.printStackTrace(); 132 | } 133 | } 134 | 135 | //数据处理 136 | Handler handler = new Handler() { 137 | public void handleMessage(Message msg) { 138 | String str = msg.obj + ""; 139 | JSONObject root = null; 140 | try { 141 | root = new JSONObject(str); 142 | JSONArray ary = root.getJSONArray("data"); 143 | for (int i = 0; i < ary.length() - 1; i++) { 144 | JSONObject root1 = ary.getJSONObject(i); 145 | JSONObject root2 = root1.getJSONObject("group"); 146 | JSONObject root3 = root2.getJSONObject("user"); 147 | mainAdapterBean = new JokeBean(); 148 | mainAdapterBean.setText(root2.optString("text")); 149 | mainAdapterBean.setComment_count(root2.optString("comment_count")); 150 | mainAdapterBean.setSource(root2.optString("category_name")); 151 | mainAdapterBean.setAvatar_url(root3.optString("avatar_url")); 152 | mainAdapterBean.setName(root3.optString("name")); 153 | mainAdapterBean.setShare_url(root2.optString("share_url")); 154 | newList.add(0, mainAdapterBean); 155 | } 156 | adapter.add(newList); 157 | adapter.setOnItemClickListener(new OnItemClickListener() { 158 | @Override 159 | public void onClick(View view, int position) { 160 | Intent i=new Intent(getContext(), JokeContent.class); 161 | i.putExtra("text",newList.get(position).getText()); 162 | i.putExtra("name",newList.get(position).getName()); 163 | i.putExtra("source",newList.get(position).getSource()); 164 | i.putExtra("image_url",newList.get(position).getAvatar_url()); 165 | i.putExtra("comment_count",newList.get(position).getComment_count()); 166 | startActivity(i); 167 | } 168 | }); 169 | rv.setLayoutManager(new LinearLayoutManager(getContext())); 170 | rv.setAdapter(adapter); 171 | swipeRefreshLayout.setRefreshing(false); 172 | } catch (JSONException e) { 173 | e.printStackTrace(); 174 | } 175 | } 176 | }; 177 | } 178 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/PageFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.Message; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.widget.SwipeRefreshLayout; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.TextView; 16 | 17 | import com.example.laravelchen.toutiao.ArticleContentShow; 18 | import com.example.laravelchen.toutiao.Constant; 19 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 20 | import com.example.laravelchen.toutiao.PathRandom; 21 | import com.example.laravelchen.toutiao.R; 22 | import com.example.laravelchen.toutiao.alladapter.RecommendAdapter; 23 | import com.example.laravelchen.toutiao.allbean.RecommendBean; 24 | 25 | import org.json.JSONArray; 26 | import org.json.JSONException; 27 | import org.json.JSONObject; 28 | 29 | import java.io.IOException; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | import okhttp3.MediaType; 34 | import okhttp3.OkHttpClient; 35 | import okhttp3.Request; 36 | import okhttp3.RequestBody; 37 | import okhttp3.Response; 38 | 39 | 40 | /** 41 | * A simple {@link Fragment} subclass. 42 | */ 43 | public class PageFragment extends Fragment { 44 | 45 | 46 | public static final String ARGS_PAGE = "args_page"; 47 | private TextView tv; 48 | private static String PATH = new PathRandom().getHomePath(); 49 | 50 | private List newList; 51 | private RecommendAdapter adapter; 52 | private RecyclerView rv; 53 | private RecommendBean mainAdapterBean; 54 | private SwipeRefreshLayout swipeRefreshLayout; 55 | private View view; 56 | public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 57 | 58 | 59 | public static PageFragment newInstance() { 60 | PageFragment fragment = new PageFragment(); 61 | return fragment; 62 | } 63 | 64 | 65 | @Override 66 | public void onCreate(Bundle savedInstanceState) { 67 | super.onCreate(savedInstanceState); 68 | } 69 | 70 | 71 | @Override 72 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 73 | view = inflater.inflate(R.layout.fragment_page, container, false); 74 | initView(); 75 | return view; 76 | } 77 | 78 | public void initView() { 79 | newList = new ArrayList(); 80 | adapter = new RecommendAdapter(newList, getContext()); 81 | rv = (RecyclerView) view.findViewById(R.id.rv); 82 | swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 83 | swipeRefreshLayout.setRefreshing(true); 84 | createThread(); 85 | seeRefresh(); 86 | } 87 | 88 | //设置刷新 89 | public void seeRefresh() { 90 | // 设置下拉进度的背景颜色,默认就是白色的 91 | swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); 92 | // 设置下拉进度的主题颜色 93 | swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); 94 | //监听刷新 95 | swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 96 | @Override 97 | public void onRefresh() { 98 | PATH = new PathRandom().getHomePath(); 99 | createThread(); 100 | } 101 | }); 102 | } 103 | 104 | 105 | //创建进程 106 | public void createThread() { 107 | new Thread() { 108 | public void run() { 109 | postJson(); 110 | } 111 | }.start(); 112 | } 113 | 114 | //获取数据 115 | private void postJson() { 116 | //申明给服务端传递一个json串 117 | //创建一个OkHttpClient对象 118 | OkHttpClient okHttpClient = new OkHttpClient(); 119 | //创建一个RequestBody(参数1:数据类型 参数2传递的json串) 120 | RequestBody requestBody = RequestBody.create(JSON, "video"); 121 | //创建一个请求对象 122 | Request request = new Request.Builder() 123 | .url(PATH) 124 | .header("User-Agent", Constant.USER_AGENT_MOBILE) 125 | .post(requestBody) 126 | .build(); 127 | //发送请求获取响应 128 | try { 129 | Response response = okHttpClient.newCall(request).execute(); 130 | //判断请求是否成功 131 | if (response.isSuccessful()) { 132 | String content = response.body().string(); 133 | Message msg = new Message(); 134 | msg.obj = content; 135 | handler.sendMessage(msg); 136 | } 137 | } catch (IOException e) { 138 | e.printStackTrace(); 139 | } 140 | } 141 | 142 | //数据处理 143 | Handler handler = new Handler() { 144 | public void handleMessage(Message msg) { 145 | String str = msg.obj + ""; 146 | try { 147 | JSONObject root = new JSONObject(str); 148 | JSONArray ary = root.getJSONArray("data"); 149 | for (int i = 0; i < ary.length() - 1; i++) { 150 | JSONObject root1 = ary.getJSONObject(i); 151 | mainAdapterBean = new RecommendBean(); 152 | mainAdapterBean.setTitle(root1.optString("title")); 153 | mainAdapterBean.setComment_couont(root1.optString("comments_count")); 154 | mainAdapterBean.setSource(root1.optString("source")); 155 | mainAdapterBean.setShare_url(root1.optString("share_url")); 156 | mainAdapterBean.setGroup_id(root1.optString("source_url")); 157 | JSONArray img_url = root1.optJSONArray("image_list"); 158 | System.out.println(PATH); 159 | if (img_url != null && img_url.length() > 0) { 160 | mainAdapterBean.setImg(img_url.getJSONObject(0).optString("url")); 161 | mainAdapterBean.setImg2(img_url.getJSONObject(1).optString("url")); 162 | mainAdapterBean.setImg3(img_url.getJSONObject(2).optString("url")); 163 | mainAdapterBean.setHasimg(true); 164 | } else { 165 | mainAdapterBean.setHasimg(false); 166 | } 167 | newList.add(0, mainAdapterBean); 168 | } 169 | adapter.add(newList); 170 | //设置点击事件 171 | adapter.setOnItemClickListener(new OnItemClickListener() { 172 | @Override 173 | public void onClick(View view, int position) { 174 | Intent i = new Intent(getContext(), ArticleContentShow.class); 175 | i.putExtra("share_url", "http://www.toutiao.com" + newList.get(position).getGroup_id()); 176 | startActivity(i); 177 | } 178 | }); 179 | rv.setLayoutManager(new LinearLayoutManager(getContext())); 180 | rv.setAdapter(adapter); 181 | swipeRefreshLayout.setRefreshing(false); 182 | } catch (JSONException e) { 183 | e.printStackTrace(); 184 | } 185 | } 186 | }; 187 | } 188 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/PhotoFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.Message; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.widget.SwipeRefreshLayout; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | 16 | import com.example.laravelchen.toutiao.ArticleContentShow; 17 | import com.example.laravelchen.toutiao.Constant; 18 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 19 | import com.example.laravelchen.toutiao.PathRandom; 20 | import com.example.laravelchen.toutiao.PhotoActivity; 21 | import com.example.laravelchen.toutiao.R; 22 | import com.example.laravelchen.toutiao.alladapter.PhotoAdapter; 23 | import com.example.laravelchen.toutiao.allbean.PhotoBean; 24 | 25 | import org.json.JSONArray; 26 | import org.json.JSONException; 27 | import org.json.JSONObject; 28 | 29 | import java.io.IOException; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | import okhttp3.MediaType; 34 | import okhttp3.OkHttpClient; 35 | import okhttp3.Request; 36 | import okhttp3.RequestBody; 37 | import okhttp3.Response; 38 | 39 | /** 40 | * A simple {@link Fragment} subclass. 41 | */ 42 | public class PhotoFragment extends Fragment { 43 | 44 | private static String PATH = new PathRandom().getPhotoPath(); 45 | private List newList; 46 | private PhotoAdapter adapter; 47 | private RecyclerView rv; 48 | private PhotoBean mainAdapterBean; 49 | private SwipeRefreshLayout swipeRefreshLayout; 50 | private View view; 51 | public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 52 | 53 | public PhotoFragment() { 54 | // Required empty public constructor 55 | } 56 | 57 | 58 | @Override 59 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 60 | Bundle savedInstanceState) { 61 | view = inflater.inflate(R.layout.fragment_photo, container, false); 62 | initView(); 63 | return view; 64 | } 65 | 66 | private void initView() { 67 | newList = new ArrayList(); 68 | adapter = new PhotoAdapter(newList, getContext()); 69 | rv = (RecyclerView) view.findViewById(R.id.rv); 70 | swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 71 | swipeRefreshLayout.setRefreshing(true); 72 | createThread(); 73 | seeRefresh(); 74 | } 75 | 76 | 77 | //设置刷新 78 | public void seeRefresh() { 79 | // 设置下拉进度的背景颜色,默认就是白色的 80 | swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); 81 | // 设置下拉进度的主题颜色 82 | swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); 83 | //监听刷新 84 | swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 85 | @Override 86 | public void onRefresh() { 87 | PATH = new PathRandom().getPhotoPath(); 88 | createThread(); 89 | } 90 | }); 91 | } 92 | 93 | //创建进程 94 | public void createThread() { 95 | new Thread() { 96 | public void run() { 97 | postJson(); 98 | } 99 | }.start(); 100 | } 101 | 102 | //获取数据 103 | private void postJson() { 104 | //申明给服务端传递一个json串 105 | //创建一个OkHttpClient对象 106 | OkHttpClient okHttpClient = new OkHttpClient(); 107 | //创建一个RequestBody(参数1:数据类型 参数2传递的json串) 108 | RequestBody requestBody = RequestBody.create(JSON, "photo"); 109 | //创建一个请求对象 110 | Request request = new Request.Builder() 111 | .url(PATH) 112 | .header("User-Agent", Constant.USER_AGENT_MOBILE) 113 | .post(requestBody) 114 | .build(); 115 | //发送请求获取响应 116 | try { 117 | Response response = okHttpClient.newCall(request).execute(); 118 | //判断请求是否成功 119 | if (response.isSuccessful()) { 120 | String content = response.body().string(); 121 | Message msg = new Message(); 122 | msg.obj = content; 123 | handler.sendMessage(msg); 124 | } 125 | } catch (IOException e) { 126 | e.printStackTrace(); 127 | } 128 | } 129 | 130 | //数据处理 131 | Handler handler = new Handler() { 132 | public void handleMessage(Message msg) { 133 | String str = msg.obj + ""; 134 | JSONObject root = null; 135 | try { 136 | root = new JSONObject(str); 137 | JSONArray ary = root.getJSONArray("data"); 138 | for (int i = 0; i < ary.length() - 1; i++) { 139 | JSONObject root1 = ary.getJSONObject(i); 140 | mainAdapterBean = new PhotoBean(); 141 | mainAdapterBean.setTitle(root1.optString("title")); 142 | mainAdapterBean.setTag(root1.optString("chinese_tag")); 143 | mainAdapterBean.setSource(root1.optString("source")); 144 | JSONArray img_url = root1.getJSONArray("image_list"); 145 | mainAdapterBean.setImg1(img_url.getJSONObject(0).optString("url")); 146 | mainAdapterBean.setGallary_image_count(root1.optString("gallary_image_count")); 147 | mainAdapterBean.setGroup_id(root1.optString("source_url")); 148 | newList.add(0, mainAdapterBean); 149 | } 150 | adapter.add(newList); 151 | //设置点击事件 152 | adapter.setOnItemClickListener(new OnItemClickListener() { 153 | @Override 154 | public void onClick(View view, int position) { 155 | Intent i = new Intent(getContext(), PhotoActivity.class); 156 | i.putExtra("share_url", "http://www.toutiao.com" + newList.get(position).getGroup_id()); 157 | startActivity(i); 158 | } 159 | }); 160 | rv.setLayoutManager(new LinearLayoutManager(getContext())); 161 | rv.setAdapter(adapter); 162 | swipeRefreshLayout.setRefreshing(false); 163 | } catch (JSONException e) { 164 | e.printStackTrace(); 165 | } 166 | } 167 | }; 168 | } 169 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/UserFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.example.laravelchen.toutiao.R; 11 | 12 | 13 | /** 14 | * A simple {@link Fragment} subclass. 15 | */ 16 | public class UserFragment extends Fragment { 17 | 18 | 19 | public UserFragment() { 20 | // Required empty public constructor 21 | } 22 | 23 | 24 | @Override 25 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 26 | Bundle savedInstanceState) { 27 | // Inflate the layout for this fragment 28 | return inflater.inflate(R.layout.fragment_user, container, false); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/allfragment/VideoFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.allfragment; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.Message; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.widget.SwipeRefreshLayout; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | 16 | import com.example.laravelchen.toutiao.ArticleContentShow; 17 | import com.example.laravelchen.toutiao.Constant; 18 | import com.example.laravelchen.toutiao.Interface.OnItemClickListener; 19 | import com.example.laravelchen.toutiao.PathRandom; 20 | import com.example.laravelchen.toutiao.R; 21 | import com.example.laravelchen.toutiao.VideoActivity; 22 | import com.example.laravelchen.toutiao.alladapter.RecommendAdapter; 23 | import com.example.laravelchen.toutiao.alladapter.VideoAdapter; 24 | import com.example.laravelchen.toutiao.allbean.RecommendBean; 25 | import com.example.laravelchen.toutiao.allbean.VideoBean; 26 | 27 | import org.json.JSONArray; 28 | import org.json.JSONException; 29 | import org.json.JSONObject; 30 | 31 | import java.io.IOException; 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | 35 | import okhttp3.MediaType; 36 | import okhttp3.OkHttpClient; 37 | import okhttp3.Request; 38 | import okhttp3.RequestBody; 39 | import okhttp3.Response; 40 | 41 | 42 | /** 43 | * A simple {@link Fragment} subclass. 44 | */ 45 | public class VideoFragment extends Fragment { 46 | private static String PATH = new PathRandom().getVideoPath(); 47 | private List newList; 48 | private VideoAdapter adapter; 49 | private RecyclerView rv; 50 | private VideoBean mainAdapterBean; 51 | private SwipeRefreshLayout swipeRefreshLayout; 52 | private View view; 53 | public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 54 | 55 | public VideoFragment() { 56 | // Required empty public constructor 57 | } 58 | 59 | 60 | @Override 61 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 62 | Bundle savedInstanceState) { 63 | // Inflate the layout for this fragment 64 | 65 | view = inflater.inflate(R.layout.fragment_video, container, false); 66 | initView(); 67 | return view; 68 | } 69 | 70 | public void initView() { 71 | newList = new ArrayList(); 72 | adapter = new VideoAdapter(newList, getContext()); 73 | rv = (RecyclerView) view.findViewById(R.id.rv); 74 | swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh); 75 | swipeRefreshLayout.setRefreshing(true); 76 | createThread(); 77 | seeRefresh(); 78 | } 79 | 80 | //设置刷新 81 | public void seeRefresh() { 82 | // 设置下拉进度的背景颜色,默认就是白色的 83 | swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); 84 | // 设置下拉进度的主题颜色 85 | swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); 86 | //监听刷新 87 | swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 88 | @Override 89 | public void onRefresh() { 90 | createThread(); 91 | PATH = new PathRandom().getVideoPath(); 92 | } 93 | }); 94 | } 95 | 96 | 97 | //创建进程 98 | public void createThread() { 99 | new Thread() { 100 | public void run() { 101 | postJson(); 102 | } 103 | }.start(); 104 | } 105 | 106 | //获取数据 107 | private void postJson() { 108 | //申明给服务端传递一个json串 109 | //创建一个OkHttpClient对象 110 | OkHttpClient okHttpClient = new OkHttpClient(); 111 | //创建一个RequestBody(参数1:数据类型 参数2传递的json串) 112 | RequestBody requestBody = RequestBody.create(JSON, "video"); 113 | //创建一个请求对象 114 | Request request = new Request.Builder() 115 | .url(PATH) 116 | .header("User-Agent", Constant.USER_AGENT_MOBILE) 117 | .post(requestBody) 118 | .build(); 119 | //发送请求获取响应 120 | try { 121 | Response response = okHttpClient.newCall(request).execute(); 122 | //判断请求是否成功 123 | if (response.isSuccessful()) { 124 | String content = response.body().string(); 125 | Message msg = new Message(); 126 | msg.obj = content; 127 | handler.sendMessage(msg); 128 | } 129 | } catch (IOException e) { 130 | e.printStackTrace(); 131 | } 132 | } 133 | 134 | //数据处理 135 | Handler handler = new Handler() { 136 | public void handleMessage(Message msg) { 137 | String str = msg.obj + ""; 138 | JSONObject root = null; 139 | try { 140 | root = new JSONObject(str); 141 | JSONArray ary = root.getJSONArray("data"); 142 | for (int i = 0; i < ary.length() - 1; i++) { 143 | JSONObject root1 = ary.getJSONObject(i); 144 | mainAdapterBean = new VideoBean(); 145 | mainAdapterBean.setTitle(root1.optString("title")); 146 | mainAdapterBean.setComment_count(root1.optString("comments_count")); 147 | mainAdapterBean.setSource(root1.optString("source")); 148 | mainAdapterBean.setImage_url(root1.optString("image_url")); 149 | mainAdapterBean.setSource_url(root1.optString("source_url")); 150 | newList.add(0, mainAdapterBean); 151 | } 152 | adapter.add(newList); 153 | //设置点击事件 154 | adapter.setOnItemClickListener(new OnItemClickListener() { 155 | @Override 156 | public void onClick(View view, int position) { 157 | Intent i = new Intent(getContext(), VideoActivity.class); 158 | i.putExtra("share_url", "http://www.365yg.com" + newList.get(position).getSource_url()); 159 | i.putExtra("video_title",newList.get(position).getTitle()); 160 | startActivity(i); 161 | } 162 | }); 163 | rv.setLayoutManager(new LinearLayoutManager(getContext())); 164 | rv.setAdapter(adapter); 165 | swipeRefreshLayout.setRefreshing(false); 166 | } catch (JSONException e) { 167 | e.printStackTrace(); 168 | } 169 | } 170 | }; 171 | } 172 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/extra/GetJson.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.extra; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * Created by LaravelChen on 2017/6/10. 8 | */ 9 | 10 | public class GetJson { 11 | public static String readStream(InputStream is) throws Exception{ 12 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 13 | byte[] buffer = new byte[1024]; 14 | int len = 0; 15 | while ((len = is.read(buffer))!=-1){ 16 | baos.write(buffer, 0, len); 17 | } 18 | is.close(); 19 | String content = new String(baos.toByteArray(), "gbk"); 20 | return content; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/extra/ImgLoader.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.extra; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.widget.ImageView; 6 | 7 | import com.nostra13.universalimageloader.core.DisplayImageOptions; 8 | import com.nostra13.universalimageloader.core.ImageLoader; 9 | import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 10 | 11 | /** 12 | * Created by LaravelChen on 2017/6/15. 13 | */ 14 | 15 | public class ImgLoader { 16 | private ImageLoader imageLoader; 17 | private DisplayImageOptions displayImageOptions; 18 | private ImageLoaderConfiguration imageLoaderConfiguration; 19 | 20 | public ImgLoader(Context context) { 21 | displayImageOptions = new DisplayImageOptions.Builder().cacheInMemory(true) 22 | .cacheOnDisk(true) 23 | .cacheInMemory(true) 24 | .bitmapConfig(Bitmap.Config.RGB_565) 25 | .build(); 26 | imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(context).build(); 27 | imageLoader = ImageLoader.getInstance(); 28 | imageLoader.init(imageLoaderConfiguration); 29 | } 30 | 31 | public void disPlayimg(String url, ImageView imageView) { 32 | imageLoader.displayImage(url, imageView, displayImageOptions); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laravelchen/toutiao/extra/ImgUtils.java: -------------------------------------------------------------------------------- 1 | package com.example.laravelchen.toutiao.extra; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.media.MediaScannerConnection; 7 | import android.net.Uri; 8 | import android.os.Environment; 9 | import android.util.Log; 10 | 11 | import java.io.File; 12 | import java.io.FileNotFoundException; 13 | import java.io.FileOutputStream; 14 | import java.io.IOException; 15 | 16 | /** 17 | * Created by LaravelChen on 2017/6/21. 18 | */ 19 | 20 | public class ImgUtils { 21 | public static boolean saveImageToGallery(Context context, Bitmap bmp) { 22 | // 首先保存图片 23 | String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy"; 24 | File appDir = new File(storePath); 25 | if (!appDir.exists()) { 26 | appDir.mkdir(); 27 | } 28 | String fileName = System.currentTimeMillis() + ".jpg"; 29 | File file = new File(appDir, fileName); 30 | try { 31 | FileOutputStream fos = new FileOutputStream(file); 32 | //通过io流的方式来压缩保存图片 33 | boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos); 34 | fos.flush(); 35 | fos.close(); 36 | 37 | //把文件插入到系统图库 38 | //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); 39 | 40 | //保存图片后发送广播通知更新数据库 41 | Uri uri = Uri.fromFile(file); 42 | context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); 43 | if (isSuccess) { 44 | return true; 45 | } else { 46 | return false; 47 | } 48 | } catch (IOException e) { 49 | e.printStackTrace(); 50 | } 51 | return false; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/login.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_article_content_show.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_joke_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 19 | 20 | 27 | 32 | 41 | 42 | 52 | 53 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 23 | 24 | 29 | 30 | 35 | 36 | 37 | 38 | 48 | 49 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_photo.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 19 | 20 | 24 | 30 | 31 | 37 | 48 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_user_login_and_register.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_user_setting.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 19 | 27 | 35 | 39 | 44 | 45 | 52 | 60 | 73 | 78 | 79 | 80 | 87 | 95 | 105 | 110 | 111 | 112 | 119 | 127 | 137 | 142 | 143 | 150 | 158 | 168 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_video.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 16 | 17 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_home.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 17 | 18 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_login.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 16 | 17 | 22 | 23 | 28 | 29 | 38 | 39 | 46 | 47 | 48 | 58 | 59 | 60 | 76 | 77 | 78 | 95 | 96 | 97 | 105 | 106 |