ButterKnife 加载控件 + xUtils 获取数据+ Fresco 显示图片 + 通过事件分发控制移动 + 通过AIDL获取数据



一, 导入jar包

      xUtils.jar  +  gson.jar


二, 添加依赖

compile 'com.jakewharton:butterknife:8.8.1'
compile 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.facebook.fresco:fresco:1.5.0'

三, MyApp.java

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        //初始化Fresco
        Fresco.initialize(this);

        //初始化xUtils
        x.Ext.init(this);
        x.Ext.setDebug(true);
    }
}

   四, 清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="bw.com.week2_test">

        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

        <application
            android:name=".MyApp"
            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_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scroll_view_id"
    tools:context="bw.com.week2_test.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ListView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="1000dp"
            android:id="@+id/lv_id_01"/>

        <ListView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:id="@+id/lv_id_02"/>

        <ListView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:id="@+id/lv_id_03"/>
    </LinearLayout>
</ScrollView>

item_rv.xml

<?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="100dp"
        android:layout_height="100dp"
        fresco:placeholderImage="@mipmap/ic_launcher"
        android:id="@+id/sdv_id"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_id"
        android:text="标题"
        android:textSize="24sp"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/sdv_id"
        />
</RelativeLayout>


六, MainActivity.java

    

/**
 * 1, ButterKnife 加载控件
 * 2, 通过xUtils 获取数据, Fresco 显示图片
 * 3, 通过事件分发, 左侧控制左侧的移动, 右侧控制右侧的移动, 中间控制一起的移
 * 4, 点击 每个条目, 通过aidl 获取服务端返回的数据  -- 两个app , Servier -- App
 */
public class MainActivity extends AppCompatActivity {

    //TODO 初始化控件
    @BindView(value = R.id.lv_id_01)
    ListView mLv1;
    @BindView(value = R.id.lv_id_02)
    ListView mLv2;
    @BindView(value = R.id.lv_id_03)
    ListView mLv3;
    @BindView(value = R.id.scroll_view_id)
    ScrollView mScrollView;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       //TODO 绑定ButterKnife
        unbinder = ButterKnife.bind(this);


        //TODO xUtils 获取网络数据
        RequestParams params = new RequestParams("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

                //TODO 解析数据, 获取的数据源
                Qubaobei qubaobei = new Gson().fromJson(s,Qubaobei.class);
                List<Qubaobei.DataBean> dataBean = qubaobei.getData();

                //TODO 初始化适配器
                MyAdapter adapter = new MyAdapter(getApplicationContext(),dataBean);
                mLv1.setAdapter(adapter);

                //TODO 设置其余的RecycleView
                mLv2.setAdapter(adapter);
                mLv3.setAdapter(adapter);

            }
            @Override
            public void onError(Throwable throwable, boolean b) {
            }
            @Override
            public void onCancelled(CancelledException e) {
            }
            @Override
            public void onFinished() {
            }
        });

        //TODO -----设置事件的处理
        mLv1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //如果RecyclerView 触摸事件监听器中, 事件如果抬起的状态 --则让ScrollView 来处理事件
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    //否则, 让RecyclerView来处理
                    mScrollView.requestDisallowInterceptTouchEvent(true);//不消费
                }
                return false;
            }
        });

        mLv2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    //否则, 让RecyclerView来处理
                    mScrollView.requestDisallowInterceptTouchEvent(false);//消费
                }
                return false;
            }
        });

        mLv3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //如果RecyclerView 触摸事件监听器中, 事件如果抬起的状态 --则让ScrollView 来处理事件
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    //否则, 让RecyclerView来处理
                    mScrollView.requestDisallowInterceptTouchEvent(true);//不消费
                }
                return false;
            }
        });

        //TODO 点击条目, 显示AIDL 返回的数据
        mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    //TODO 获取AIDL 返回的数据
                    String str = myAidl.getInfo();
                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        mLv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    //TODO 获取AIDL 返回的数据
                    String str = myAidl.getInfo();
                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        mLv3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    //TODO 获取AIDL 返回的数据
                    String str = myAidl.getInfo();
                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //解绑ButterKnife
        unbinder.unbind();
    }

    //TODO ---------------AIDL 实现--------
    private MyAidl myAidl;
    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 绑定服务
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent("com.bw.aidl");
        intent.setPackage("bw.com.server");
        bindService(intent,connection,BIND_AUTO_CREATE);
    }
    //TODO 解绑服务
    @Override
    protected void onStop() {
        super.onStop();
        unbindService(connection);
    }



}

七, MyAdapter.java

public class MyAdapter extends BaseAdapter{
    private List<Qubaobei.DataBean> data;
    private Context context;

    public MyAdapter(Context context,List<Qubaobei.DataBean> data)
    {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if(convertView==null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_rv,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //TODO Fresco 获取图片
        Uri uri = Uri.parse(data.get(position).getPic());
        viewHolder.sdv.setImageURI(uri);

        viewHolder.titleTv.setText(data.get(position).getTitle());

        return convertView;
    }

    class ViewHolder
    {
        @BindView(value = R.id.sdv_id)
        SimpleDraweeView sdv;

        @BindView(value = R.id.title_id)
        TextView titleTv;

        public ViewHolder(View itemView) {
            //TODO 绑定ButterKnife
            ButterKnife.bind(this,itemView);
        }
    }
}

八, Qubaobei.java   - - GsonFormant 生成的实体类

九, 复制服务端的src/main 目录中的aidl文件夹  -- 然后编译

十, 服务器端

        1, 在src /main 目录下创建 aidl 文件 

        MyAidl.aidl

package bw.com.server;

interface MyAidl {

    String getInfo();
}

       

  2,  编译生成同名的的.java 文件  -- MyAidl.java


 3,  定义 MyService.java 类

        

public class MyService extends Service {

    private MyAidl.Stub stub = new MyAidl.Stub() {
        @Override
        public String getInfo() throws RemoteException {
            return "AIDL 返回的数据";
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return stub;
    }
}

4,在服务器的清单文件夹中, 注册Service

<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.aidl"/>
            </intent-filter>
        </service>
    </application>

</manifest>


十一,  运行的时候, 先运行Service ,  然后在运行App




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值