影视分享App(二)
一、开发进度二
第二步主要实现在首页页面左右滑动功能,以及搭建图片及数据访问框架来在页面在Top250页面上显示实现网站图片。
1、左右滑动功能
新建布局文件one_child_fragment_layout.layout以及首页的子fragment OneChildFragment.java
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一个Fragment的子Fragment" /> </LinearLayout>
package com.example.yingshifenxiang.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.yingshifenxiang.R; public class OneChildFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.one_child_fragment_layout, null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } }
重写OneFragment.java
package com.example.yingshifenxiang.fragment; import android.view.View; import android.view.LayoutInflater; import android.view.ViewGroup; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager; import com.example.yingshifenxiang.R; import com.google.android.material.tabs.TabLayout; public class OneFragment extends Fragment { private TabLayout tabLayout; private ViewPager viewPager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.one_fragment_layout, null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tabLayout=(TabLayout)view.findViewById(R.id.tabLayout); viewPager=(ViewPager)view.findViewById(R.id.viewPager); MyPagerAdapter myPagerAdapter = new MyPagerAdapter(this.getChildFragmentManager()); viewPager.setAdapter(myPagerAdapter); tabLayout.setupWithViewPager(viewPager); } String[] TITLES = {"正在热映", "即将上映", "北美票房"}; class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if(position==0){ } if(position==1){ } if(position==2){ } return new OneChildFragment(); } @Override public int getCount() { return TITLES.length; } @Nullable @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } } }
二、搭建图片及数据访问框架来在页面在Top250页面上显示实现网站图片
在AndroidManifest.xml以及build.gradle中添加部署及下载第三方库
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.yingshifenxiang" > <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions"/> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { applicationId "com.example.yingshifenxiang" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.navigation:navigation-fragment:2.2.1' implementation 'androidx.navigation:navigation-ui:2.2.1' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.3' implementation 'com.mcxiaoke.volley:library:1.0.19' implementation 'com.google.code.gson:gson:2.8.5' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
在这里遇到了一个小问题,下载库失败,通过在项目build.gradle中加入镜像解决
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { maven{ url 'https://maven.aliyun.com/repository/google/'} maven{ url 'https://maven.aliyun.com/repository/jcenter/'} google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.6.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { maven{ url 'https://maven.aliyun.com/repository/google/'} maven{ url 'https://maven.aliyun.com/repository/jcenter/'} google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
准备工作已完成,接下来就是具体的代码了
新建MyApplication.java这个java文件是一套模板代码,在获取图片时使用
package com.example.yingshifenxiang; import android.app.Application; import android.content.Context; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; public class MyApplication extends Application { //ImageLoader显示图片过程中的参数 private static DisplayImageOptions mLoaderOptions; //Volley网络请求的队列对象 private static RequestQueue mQueue; @Override public void onCreate() { super.onCreate(); //初始化ImageLoader initImageLoader(getApplicationContext()); //初始化Volley的队列对象 mQueue = Volley.newRequestQueue(getApplicationContext()); } public static void initImageLoader(Context context) { //初始化一个ImageLoaderConfiguration配置对象 ImageLoaderConfiguration config = new ImageLoaderConfiguration. Builder(context). denyCacheImageMultipleSizesInMemory(). threadPriority(Thread.NORM_PRIORITY - 2). diskCacheFileNameGenerator(new Md5FileNameGenerator()). tasksProcessingOrder(QueueProcessingType.FIFO). build(); //用ImageLoaderConfiguration配置对象完成ImageLoader的初始化,单例 ImageLoader.getInstance().init(config); //示图片过程中的参数 mLoaderOptions = new DisplayImageOptions.Builder(). showImageOnLoading(R.drawable.no_image).//正加载,显示no_image showImageOnFail(R.drawable.no_image).//加载失败时 showImageForEmptyUri(R.drawable.no_image).//加载的Uri为空 imageScaleType(ImageScaleType.EXACTLY_STRETCHED). cacheInMemory(true).//是否进行缓冲 cacheOnDisk(true). considerExifParams(true). build(); } public static RequestQueue getHttpQueue() { return mQueue; } public static DisplayImageOptions getLoaderOptions() { return mLoaderOptions; } //Volley请求,传入消息编号Tag。将request请求放入队列中缓存 public static void addRequest(Request request, Object tag) { request.setTag(tag); request.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); mQueue.add(request); } // 通过消息编号在队列中删除请求 public static void removeRequest(Object tag) { mQueue.cancelAll(tag); } }
重写three_fragment_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="three"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView"/> </LinearLayout>
重写ThreeFragment.java
package com.example.yingshifenxiang.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import androidx.fragment.app.Fragment; import com.example.yingshifenxiang.MyApplication; import com.example.yingshifenxiang.R; import com.nostra13.universalimageloader.core.ImageLoader; public class ThreeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.three_fragment_layout, null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ImageView iv=(ImageView)view.findViewById(R.id.imageView); ImageLoader.getInstance().displayImage("http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg", iv, MyApplication.getLoaderOptions()); } }
到这里,就可以运行程序看看效果啦,过程中有很多辛酸的经历就不回忆了。。。