一个小demo

一个使用友盟登录+Xutils3.0网络请求+XListView多条目加载。


MainActivity


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lixin.zhoukaolx.fragment.MyFragment;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class MainActivity extends AppCompatActivity{

    private ImageView iv;
    private String[] strings;
    private TabLayout tabLayout;
    private ViewPager viewPager;


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

        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(8);
        iv = (ImageView) findViewById(R.id.iv);
        tabLayout.setupWithViewPager(viewPager);
        strings = new String[]{"川菜","湘菜","东北菜","新疆菜","北京菜","天津菜","唐山菜","湖南菜",};

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
            }
        });

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                MyFragment myfragment = new MyFragment();
                return myfragment;
            }

            @Override
            public int getCount() {
                return strings.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return strings[position];
            }
        });
    }
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
        }
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}

MyFragment

package com.example.lixin.zhoukaolx.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lixin.zhoukaolx.R;
import com.example.lixin.zhoukaolx.bean.MenuInfo;
import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by hua on 2017/8/4.
 */

public class MyFragment extends Fragment implements XListView.IXListViewListener {


    private int index = 1;
    private XListView xListView;
    private boolean flag;
    private MyBaseAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.myfragment, container, false);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        xListView = (XListView) getView().findViewById(R.id.xlistview);
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
        initData();

    }
    private void initData(){
        RequestParams params=new RequestParams("http://apis.juhe.cn/cook/query.php");
        params.addQueryStringParameter("key","4828e85e3b04e4b0d7ac8afab28a39cf");
        params.addQueryStringParameter("menu","小鸡炖蘑菇");
        params.addQueryStringParameter("pn",index+"");
        params.addQueryStringParameter("rn","10");

            x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("----------------------------"+result);
                Gson gson = new Gson();
                MenuInfo menuInfo = gson.fromJson(result, MenuInfo.class);
                List<MenuInfo.ResultBean.DataBean> data = menuInfo.getResult().getData();
                System.out.println("-----------------------"+data);
                if (adapter==null){
                    adapter = new MyBaseAdapter(data);
                    xListView.setAdapter(adapter);
                }else{
                    adapter.add(data,flag);
                    adapter.notifyDataSetChanged();
                }

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onRefresh() {
        index++;
        initData();
        flag=true;
        xListView.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
        index++;
        initData();
        flag=false;
        xListView.stopLoadMore();
    }

    class MyBaseAdapter extends BaseAdapter {

        private List<MenuInfo.ResultBean.DataBean> data;
        private MyBaseAdapter(List<MenuInfo.ResultBean.DataBean> data){
            this.data=data;
        }

        public void add(List<MenuInfo.ResultBean.DataBean> datas,boolean flag){
            for (MenuInfo.ResultBean.DataBean b:datas) {
                if (flag){
                    data.add(0,b);
                }else{
                    data.add(b);
                }
            }
        }

        ImageOptions imageOptions=new ImageOptions.Builder()
                .setLoadingDrawableId(R.mipmap.ic_launcher)
                .setUseMemCache(true)
                .setSize(200,200)
                .build();

        @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 int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position%2==0){
                return 0;
            }else {
                return 1;
            }

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            ViewHolder1 holder1 = null;
            switch (getItemViewType(position)){

                case 0:
                    if (convertView == null){
                        convertView = View.inflate(getActivity(),R.layout.item1,null);
                        holder = new ViewHolder();
                        holder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
                        holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
                        convertView.setTag(holder);
                    }else {
                        holder = (ViewHolder) convertView.getTag();
                    }
                    holder.tv.setText(data.get(position).getImtro());
                    x.image().bind(holder.iv,data.get(position).getAlbums().get(0),imageOptions);
                    break;
                case 1:
                    if (convertView == null){
                        convertView = View.inflate(getActivity(),R.layout.item2,null);
                        holder1 = new ViewHolder1();
                        holder1.iv = (ImageView) convertView.findViewById(R.id.item2_iv);
                        holder1.tv = (TextView) convertView.findViewById(R.id.item2_tv);
                        convertView.setTag(holder1);
                    }else {
                        holder1 = (ViewHolder1) convertView.getTag();
                    }
                    holder1.tv.setText(data.get(position).getImtro());
                    x.image().bind(holder1.iv,data.get(position).getAlbums().get(0),imageOptions);
                    break;
            }
            return convertView;
        }
        class ViewHolder{
            TextView tv;
            ImageView iv;
        }
        class ViewHolder1{
            TextView tv;
            ImageView iv;
        }
    }
}

MyApplication

package com.example.lixin.zhoukaolx;

import android.app.Application;

import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import org.xutils.x;

/**
 * Created by hua on 2017/8/4.
 */

public class MyApplication extends Application {

    {
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        UMShareAPI.get(this);
        Config.DEBUG = true;

    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lixin.zhoukaolx">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />



    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.umeng.qq.tencent.AuthActivity"
            android:launchMode="singleTask"
            android:noHistory="true" >

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tencent100424468" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.umeng.qq.tencent.AssistActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|screenSize"/>

        <meta-data
            android:name="UMENG_APPKEY"
            android:value="561cae6ae0f55abd990035bf" >
        </meta-data>
    </application>

</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值