app
MainActivity
package bw.com.month_test;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
private Unbinder unbinder;
//TODO 使用ButterKnife 初始化控件
@BindView(value = R.id.tab_layout_id)
TabLayout mTabLayout;//导航
private List<String> titles = new ArrayList<>();//标题
@BindView(value = R.id.view_pager_id)
ViewPager mViewPager;//内容
private List<Fragment> data = new ArrayList<>();//数据源
private MyAdapter adapter;//适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TODO 绑定ButterKnife
unbinder = ButterKnife.bind(this);
//TODO 初始化标题
titles.add("生钱");
titles.add("花钱");
titles.add("部落");
titles.add("我的");
//TODO 初始化数据源
data.add(new ShengQianFragment());
data.add(new OtherFragment());
data.add(new OtherFragment());
data.add(new OtherFragment());
//TODO 初始化适配器
adapter = new MyAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
//TODO TabLayout 和ViewPager 结合
mTabLayout.setupWithViewPager(mViewPager);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();//解绑
}
//TODO 自定义适配器
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return data.get(position);
}
@Override
public int getCount() {
return data.size();
}
//TODO 设置导航的标题
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
}
CustomView
package bw.com.month_test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2018/3/3.
*/
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
//绘制圆
paint.setStyle(Paint.Style.STROKE);//设置空心
paint.setColor(Color.BLACK);
canvas.drawCircle(500,500,300,paint);//绘制内圆
canvas.drawCircle(500,500,400,paint);//绘制外圆
//绘制文字
paint.setColor(Color.RED);
paint.setTextSize(40);
paint.setStyle(Paint.Style.FILL);
canvas.drawText("已经选中商品!",300,500,paint);
}
}
InfoActivity
package bw.com.month_test;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class InfoActivity extends AppCompatActivity {
private WebView mWebView;
private String[] imagePaths = new String[3];
private boolean isLast = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
mWebView = (WebView) findViewById(R.id.web_view_id);
//TODO 获取地址
String imagePath1 = getIntent().getStringExtra("imagePath1");//上一张图片
String imagePath2 = getIntent().getStringExtra("imagePath2");//当前的图片
String imagePath3 = getIntent().getStringExtra("imagePath3");//下一张图片
imagePaths[0] = imagePath1;
imagePaths[1] = imagePath2;
imagePaths[2] = imagePath3;
if(imagePath2!=null)
{
mWebView.loadUrl(imagePath2);
}else
{
mWebView.loadUrl("http://www.baidu.com");
}
//TODO 在当前的页面中显示
mWebView.setWebViewClient(new WebViewClient());
//TODO 设置支持js
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public void onClick(View view) {
switch (view.getId())
{
case R.id.but_01:
//返回上一个地址
mWebView.loadUrl(imagePaths[0]);
break;
case R.id.but_02:
//下一个地址
if(isLast)
{
mWebView.loadUrl(imagePaths[2]);
isLast = false;
}
else
{
Toast.makeText(this, "到达最后一页", Toast.LENGTH_SHORT).show();
}
break;
case R.id.but_03:
//返回上一个页面
finish();
break;
}
}
}
MyApp
package bw.com.month_test;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* Created by Administrator on 2018/3/3.
*/
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
MyBaseAdapter
package bw.com.month_test;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by Administrator on 2018/3/3.
*/
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private List<Qubaobei.DataBean> data;
public MyBaseAdapter(Context context, List<Qubaobei.DataBean> data)
{
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data != null ? data.size() : 0;
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_gv,parent,false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder) convertView.getTag();
}
//赋值
viewHolder.mTv.setText(data.get(position).getTitle());
//图片 --- Fresco : 初始化
Uri uri = Uri.parse(data.get(position).getPic());
viewHolder.mSdv.setImageURI(uri);
viewHolder.mSdv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO 点击图片, 暗变化、旋转360度的效果 -- 补间动画的集合
AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.setDuration(3000);
viewHolder.mSdv.startAnimation(animationSet);
//TODO 点击图片, 跳转到内容页面
Intent intent = new Intent(context,InfoActivity.class);
if(position>0)
{
//上一个地址
Log.e("Tag",data.get(position-1).getPic());
intent.putExtra("imagePath1",data.get(position-1).getPic());//传值
}
intent.putExtra("imagePath2",data.get(position).getPic());//传值
if(position<data.size()-1)
{
//下一张地址
intent.putExtra("imagePath3",data.get(position+1).getPic());
}
context.startActivity(intent);
}
});
return convertView;
}
class ViewHolder
{
@BindView(value = R.id.sdv_id)
SimpleDraweeView mSdv;
@BindView(value = R.id.tv_id)
TextView mTv;
public ViewHolder(View view)
{
//TODO 绑定ButterKnife
ButterKnife.bind(this,view);
}
}
}
OtherFragment
package bw.com.month_test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class OtherFragment extends Fragment {
public OtherFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_other, container, false);
}
}
QubaobeiInterface
"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"
package bw.com.month_test;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by Administrator on 2018/3/3.
*/
public interface QubaobeiInterface {
//http://www.qubaobei.com/ios/ -----cf/dish_list.php?stage_id=1&limit=20&page=1
@GET("cf/dish_list.php?stage_id=1&limit=20&page=1")
Call<Qubaobei> getInfo();
}
ShengQianFragment
package bw.com.month_test;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import bw.com.server.MyAidl;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* A simple {@link Fragment} subclass.
*/
public class ShengQianFragment extends Fragment {
private MyAidl myAidl = null;
//TODO 检测服务示范绑定成功
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定成功的回调方法
myAidl = MyAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) { }
};
//TODO 通过ButterKnife 初始化控件
@BindView(value = R.id.gv_id)
GridView mGridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sheng_qian, container, false);
//TODO 绑定ButterKnife
ButterKnife.bind(this,view);
//TODO 通过Retrofit获取网络数据
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl("http://www.qubaobei.com/ios/");//设置基础地址
builder.addConverterFactory(GsonConverterFactory.create());//设置Gson解析
Retrofit retrofit = builder.build();//得到Retrofit对象
QubaobeiInterface qubaobeiInterface = retrofit.create(QubaobeiInterface.class);//得到获取数据的接口
Call<Qubaobei> call = qubaobeiInterface.getInfo();//得到Call 接口
//执行异步请求数据
call.enqueue(new Callback<Qubaobei>() {
@Override
public void onResponse(Call<Qubaobei> call, Response<Qubaobei> response) {
//获取到的数据
Qubaobei qubaobei = response.body();
//得到数据源
List<Qubaobei.DataBean> data = qubaobei.getData();
MyBaseAdapter adapter = new MyBaseAdapter(getContext(),data);
mGridView.setAdapter(adapter);
}
@Override
public void onFailure(Call<Qubaobei> call, Throwable t) {
}
});
//TODO 点击每个Item , 获取AIDL 的信息
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO 获取AIDL 的信息
try {
String str = myAidl.getInfo();
Toast.makeText(getContext(), str, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
//TODO 弹出自定义的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.custom_dialog,null);
builder.setView(dialogView);//设置自定义的视图
builder.show();
}
});
return view;
}
//TODO 绑定服务
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setAction("com.bw.test.aidl");//服务端Service的动作
intent.setPackage("bw.com.server");//服务端的包名
getContext().bindService(intent,connection, Context.BIND_AUTO_CREATE);
}
//TODO 解绑服务
@Override
public void onDestroy() {
super.onDestroy();
getContext().unbindService(connection);
}
}
Qubaobei 为实体类
布局
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.month_test.MainActivity">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/tab_layout_id"
/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager_id"/>
</LinearLayout>
activity_info
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.month_test.InfoActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:id="@+id/but_01"
android:text="上一页"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:id="@+id/but_02"
android:text="上一页"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:id="@+id/but_03"
android:text="返回"/>
</LinearLayout>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web_view_id"
/>
</LinearLayout>
custom_dialog
<?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"
android:gravity="center"
>
<bw.com.month_test.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cs_id"
/>
</LinearLayout>
fragment_other
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.month_test.OtherFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
fragment_sheng_qian
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.month_test.ShengQianFragment">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gv_id"
android:numColumns="2"/>
</FrameLayout>
item_gv
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/sdv_id"
android:layout_centerHorizontal="true"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:roundAsCircle="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_id"
android:text="标题"
android:textColor="@color/colorAccent"
android:textSize="26sp"
android:layout_marginTop="10dp"
android:layout_below="@id/sdv_id"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
依赖
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "bw.com.month_test"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.jakewharton:butterknife:8.8.1'
compile 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
testCompile 'junit:junit:4.12'
}
MyAidl.aidl
// MyAidl.aidl
package bw.com.server;
interface MyAidl {
String getInfo();
}
server
MainActivity
package bw.com.server;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MyService
package bw.com.server;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
* Created by Administrator on 2018/3/3.
*/
public class MyService extends Service {
MyAidl.Stub stub = new MyAidl.Stub() {
@Override
public String getInfo() throws RemoteException {
return "我对这个商品很感兴趣,想购买";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return stub;
}
}
布局
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="bw.com.server.MainActivity">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bw.com.server">
<application 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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册Service-->
<service android:name=".MyService">
<intent-filter>
<action android:name="com.bw.test.aidl"/>
</intent-filter>
</service>
</application>
</manifest>