FrameLayout布局+banner+fresco+recycleView多条目+retrofit请求网络

来源:互联网 发布:如何注册公司域名 编辑:程序博客网 时间:2024/05/17 22:50
compile 'com.liaoinstan.springview:library:1.2.6'compile 'com.github.liuguangqiang.SuperRecyclerView:super-recyclerview:0.1.2'compile 'com.youth.banner:banner:1.4.10'compile 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'implementation 'com.google.code.gson:gson:2.8.2'compile 'com.facebook.fresco:fresco:0.11.0'compile 'com.facebook.fresco:imagepipeline-okhttp3:0.11.0'compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'

---------------------------------------------------------------------------------------------------------------------------------------------------------

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">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#00bfff"        android:orientation="horizontal">        <ImageView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:layout_weight="1"            android:src="@drawable/shaoyishao" />        <EditText            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_weight="6"            android:drawableRight="@drawable/my_search"            android:hint="搜索一下" />        <ImageView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:layout_weight="1"            android:src="@drawable/my_tongzhi" />    </LinearLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <FrameLayout            android:id="@+id/frameLayout"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_above="@id/radioGroup"            android:layout_alignParentTop="true"></FrameLayout>        <RadioGroup            android:id="@+id/radioGroup"            android:layout_width="match_parent"            android:layout_height="60dp"            android:layout_alignParentBottom="true"            android:orientation="horizontal">            <RadioButton                android:id="@+id/homePage"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:button="@null"                android:drawableTop="@drawable/my_shouye"                android:gravity="center"                android:text="首页" />            <RadioButton                android:id="@+id/classify"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:button="@null"                android:drawableTop="@drawable/my_xtcd"                android:gravity="center"                android:text="分类" />            <RadioButton                android:id="@+id/shoppingcart"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:button="@null"                android:drawableTop="@drawable/my_shopcar"                android:gravity="center"                android:text="购物车" />            <RadioButton                android:id="@+id/person"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:button="@null"                android:drawableTop="@drawable/my_geren"                android:gravity="center"                android:text="个人" />        </RadioGroup>    </RelativeLayout></LinearLayout>



---------------------------------------------------------------------------------------------------------------------------------------------------------

layout_homepage.xml

<?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"    android:orientation="vertical">    <com.youth.banner.Banner        android:id="@+id/ban"        android:layout_width="match_parent"        android:layout_height="150dp" />    <android.support.v7.widget.RecyclerView        android:id="@+id/rv"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>


---------------------------------------------------------------------------------------------------------------------------------------------------------

item

<?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"    android:orientation="vertical">    <TextView        android:id="@+id/te1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="1111111" />    <com.facebook.drawee.view.SimpleDraweeView        android:id="@+id/img"        android:layout_width="60dp"        android:layout_height="60dp"        android:src="@mipmap/ic_launcher" /></LinearLayout>



---------------------------------------------------------------------------------------------------------------------------------------------------------

item2

<?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"    android:orientation="vertical">    <TextView        android:id="@+id/item2_te"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="1111111111" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <com.facebook.drawee.view.SimpleDraweeView            android:id="@+id/item2_img1"            android:layout_width="60dp"            android:layout_height="60dp"            android:src="@mipmap/ic_launcher" />        <com.facebook.drawee.view.SimpleDraweeView            android:layout_marginLeft="20dp"            android:id="@+id/item2_img2"            android:layout_width="60dp"            android:layout_height="60dp"            android:src="@mipmap/ic_launcher" />    </LinearLayout></LinearLayout>



---------------------------------------------------------------------------------------------------------------------------------------------------------

MainActivity

import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.widget.FrameLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;import panjiangang.bwie.com.panjiangang20171204.fragment.Classify;import panjiangang.bwie.com.panjiangang20171204.fragment.HomePage;import panjiangang.bwie.com.panjiangang20171204.fragment.Person;import panjiangang.bwie.com.panjiangang20171204.fragment.Shoppingcart;public class MainActivity extends AppCompatActivity {    @BindView(R.id.frameLayout)    FrameLayout frameLayout;    @BindView(R.id.homePage)    RadioButton homePage;    @BindView(R.id.classify)    RadioButton classify;    @BindView(R.id.shoppingcart)    RadioButton shoppingcart;    @BindView(R.id.person)    RadioButton person;    @BindView(R.id.radioGroup)    RadioGroup radioGroup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        HomePage hp = new HomePage();        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();        fragmentTransaction.replace(R.id.frameLayout, hp);        fragmentTransaction.commit();    }    @OnClick(R.id.homePage)    public void onHomePageClicked() {        HomePage hp = new HomePage();        homePage.setBackgroundColor(Color.YELLOW);        classify.setBackgroundColor(Color.WHITE);        shoppingcart.setBackgroundColor(Color.WHITE);        person.setBackgroundColor(Color.WHITE);        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();        fragmentTransaction.replace(R.id.frameLayout, hp);        fragmentTransaction.commit();    }    @OnClick(R.id.classify)    public void onClassifyClicked() {        Classify hp = new Classify();        homePage.setBackgroundColor(Color.WHITE);        classify.setBackgroundColor(Color.YELLOW);        shoppingcart.setBackgroundColor(Color.WHITE);        person.setBackgroundColor(Color.WHITE);        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();        fragmentTransaction.replace(R.id.frameLayout, hp);        fragmentTransaction.commit();    }    @OnClick(R.id.shoppingcart)    public void onShoppingcartClicked() {        Shoppingcart hp = new Shoppingcart();        homePage.setBackgroundColor(Color.WHITE);        classify.setBackgroundColor(Color.WHITE);        shoppingcart.setBackgroundColor(Color.YELLOW);        person.setBackgroundColor(Color.WHITE);        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();        fragmentTransaction.replace(R.id.frameLayout, hp);        fragmentTransaction.commit();    }    @OnClick(R.id.person)    public void onPersonClicked() {        Person hp = new Person();        homePage.setBackgroundColor(Color.WHITE);        classify.setBackgroundColor(Color.WHITE);        shoppingcart.setBackgroundColor(Color.WHITE);        person.setBackgroundColor(Color.YELLOW);        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();        fragmentTransaction.replace(R.id.frameLayout, hp);        fragmentTransaction.commit();    }}


------------------------------------------------------------------------------------------------------------------------------------------------------------------

HomePage

import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.facebook.drawee.view.SimpleDraweeView;import com.youth.banner.Banner;import com.youth.banner.loader.ImageLoader;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;import panjiangang.bwie.com.panjiangang20171204.GitHubService;import panjiangang.bwie.com.panjiangang20171204.MultiBean;import panjiangang.bwie.com.panjiangang20171204.R;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;/** * Created by lenovo on 2017/12/04. */public class HomePage extends Fragment {    RecyclerView rv;    Banner ban;    private LinearLayoutManager manager;    //private MultiBean.DataBean.SubjectsBean.GoodsListBeanX data;    private List<MultiBean.DataBean.SubjectsBean> list = new ArrayList<>();    String[] urls = {            "https://image.yunifang.com/yunifang/images/goods/2076/goods_img/170930212555714134276722977.jpg",            "https://image.yunifang.com/yunifang/images/goods/492/goods_img/171011191068814258195256706.jpg",            "https://image.yunifang.com/yunifang/images/goods/temp/17051609265319705925918251.jpg",            "https://image.yunifang.com/yunifang/images/goods/temp/17101119312216937168845205.jpg",            "https://image.yunifang.com/yunifang/images/goods/temp/171011162655217457875119759.jpg"    };    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.layout_homepage, null);        ban = view.findViewById(R.id.ban);        rv = view.findViewById(R.id.rv);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);        rv.setLayoutManager(manager);        ban.setImageLoader(new ImageLoader() {            @Override            public void displayImage(Context context, Object path, ImageView imageView) {                imageView.setImageURI(Uri.parse((String) path));               // com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage((String) path, imageView);            }            @Override            public ImageView createImageView(Context context) {                SimpleDraweeView simpleDraweeView = new SimpleDraweeView(context);                return simpleDraweeView;            }        });        ban.setImages(Arrays.asList(urls));        ban.start();        gethttp();    }    private void gethttp() {        Retrofit retrofit = new Retrofit.Builder()                .baseUrl("http://result.eolinker.com/")                .addConverterFactory(GsonConverterFactory.create())                .build();        GitHubService gitHubService = retrofit.create(GitHubService.class);        Call<MultiBean> call = gitHubService.ListRepos();        call.enqueue(new Callback<MultiBean>() {            @Override            public void onResponse(Call<MultiBean> call, Response<MultiBean> response) {                MultiBean bean = response.body();                list = bean.getData().getSubjects();                MyAdapter adapter = new MyAdapter();                rv.setAdapter(adapter);            }            @Override            public void onFailure(Call<MultiBean> call, Throwable t) {                System.out.println("失败");            }        });    }    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {        @Override        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {            if (viewType == 1) {                View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.item, parent, false);                return new MyViewHolder(view1);            } else {                View view2 = LayoutInflater.from(getActivity()).inflate(R.layout.item2, parent, false);                return new MyViewHolder2(view2);            }        }        @Override        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {            if (holder instanceof MyViewHolder) {                MyViewHolder holder1 = (MyViewHolder) holder;                holder1.te1.setText(list.get(position).getTitle());                Uri uri = Uri.parse(list.get(position).getImage());                ((MyViewHolder) holder).img.setImageURI(uri);                //com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(list.get(position).getImage(), holder1.img);            } else if (holder instanceof MyViewHolder2) {                MyViewHolder2 holder2 = (MyViewHolder2) holder;                holder2.te1.setText(list.get(position).getTitle());                Uri uri = Uri.parse(list.get(position).getImage());                holder2.img1.setImageURI(uri);                Uri uri2 = Uri.parse(list.get(position).getDescImage());                holder2.img2.setImageURI(uri2);                //com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(list.get(position).getImage(), holder2.img1);                //com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(list.get(position).getDescImage(), holder2.img2);            }        }        @Override        public int getItemViewType(int position) {            if (position % 2 == 0) {                return 0;            } else {                return 1;            }        }        @Override        public int getItemCount() {            return list == null ? 0 : list.size();        }        public class MyViewHolder extends RecyclerView.ViewHolder {            @BindView(R.id.img)            SimpleDraweeView img;            @BindView(R.id.te1)            TextView te1;            public MyViewHolder(View itemView) {                super(itemView);                ButterKnife.bind(this, itemView);            }        }        public class MyViewHolder2 extends RecyclerView.ViewHolder {            @BindView(R.id.item2_img1)            SimpleDraweeView img1;            @BindView(R.id.item2_img2)            SimpleDraweeView img2;            @BindView(R.id.item2_te)            TextView te1;            public MyViewHolder2(View itemView) {                super(itemView);                ButterKnife.bind(this, itemView);            }        }    }   }---------------------------------------------------------------------------

GitHubService

import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.Path;/** * Created by lenovo on 2017/11/29. */public interface GitHubService {    //http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage    @GET("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage")           Call<MultiBean> ListRepos();}

------------------------------------------------------------------------------------------------

MyApp

import android.app.Application;import com.facebook.drawee.backends.pipeline.Fresco;/** * Created by lenovo on 2017/12/04. */public class MyApp extends Application {    @Override    public void onCreate() {        super.onCreate();        Fresco.initialize(this);    }}

---------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="************************">    <uses-permission android:name="android.permission.INTERNET" />    <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 android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


 



阅读全文
0 0
原创粉丝点击