Rv线性布局与网格切换-Butterknife-Fresco-EventBus-Retrofit

MainActivity.java
public class MainActivity extends AppCompatActivity implements IView{

    @BindView(R.id.main_img)
    ImageView mImageView;

    @BindView(R.id.main_rv)
    RecyclerView mRecyclerView;

    private IPrecenterImpl mIPrecenter;

    private Boolean B=true;
    private Boolean changeGrid=true;
    List data1;

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

        ButterKnife.bind(this);

        mIPrecenter = new IPrecenterImpl(this);
        initData();
    }

    @OnClick(R.id.main_img)
    public void onImgClickListener(){

        if (B){
            B=false;
            if (changeGrid){
                mRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.HORIZONTAL));
                changeGrid=false;
            }
            mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
        }else {
            B=true;
            mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        }

        ListShowsAdapter showsAdapter = new ListShowsAdapter(MainActivity.this, data1,B);
        mRecyclerView.setAdapter(showsAdapter);
        showsAdapter.setOnItemClickListener(new ListShowsAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int layoutPosition, String title, String price) {

                EventBusBean busBean = new EventBusBean(layoutPosition, title, price);

                EventBus.getDefault().postSticky(busBean);
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });
    }

    private void initData() {

        Map map = new HashMap<>();
        map.put("keywords","笔记本");
        map.put("page",1+"");
        map.put("sort",0+"");
        mIPrecenter.startRequestData(Apis.URL_COMPUTER_SHOW_LIST,map, ComputerBean.class,1);
    }

    @Override
    public void showData(Object data) {

        ComputerBean computerBean= (ComputerBean) data;
        data1 = computerBean.getData();

        ListShowsAdapter showsAdapter = new ListShowsAdapter(MainActivity.this, data1,B);

        mRecyclerView.setAdapter(showsAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));

        showsAdapter.setOnItemClickListener(new ListShowsAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int layoutPosition, String title, String price) {

                EventBusBean busBean = new EventBusBean(layoutPosition, title, price);

                EventBus.getDefault().postSticky(busBean);

                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });
    }
}

activity_main.xml



   

       

       

       
   
    

ListShowsAdapter.java
public class ListShowsAdapter extends RecyclerView.Adapter{

    private Context mContext;
    List list;
    private Boolean mBoolean;

    public ListShowsAdapter(Context context, List list, Boolean aBoolean) {
        mContext = context;
        this.list = list;
        mBoolean = aBoolean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mBoolean){

            View view = View.inflate(parent.getContext(), R.layout.item_linelayout, null);
            LineViewHolder holder = new LineViewHolder(view);
            return holder;

        }else {
            View view = View.inflate(parent.getContext(), R.layout.item_gridlayout, null);
            GridViewHolder holder = new GridViewHolder(view);
            return holder;
        }
    }

    public interface onItemClickListener{
        void onItemClick(int layoutPosition, String title, String price);
    }

    private onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        if (mBoolean){

            final LineViewHolder lineViewHolder = (LineViewHolder) holder;

            lineViewHolder.mTextView_title.setText(list.get(position).getTitle());
            lineViewHolder.mTextView_price.setText(list.get(position).getPrice()+"");

            String images = list.get(position).getImages();
            Pattern pattern = Pattern.compile("\\|");
            String[] img = pattern.split(images);

            Uri uri = Uri.parse(img[0]);
            lineViewHolder.mSimpleDraweeView.setImageURI(uri);
            lineViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener!=null){
                        int layoutPosition = lineViewHolder.getLayoutPosition();
                        String title = list.get(position).getTitle();
                        String price = list.get(position).getPrice()+"";

                        mOnItemClickListener.onItemClick(layoutPosition,title,price);
                    }
                }
            });

        }else {

            final GridViewHolder gridViewHolder = (GridViewHolder) holder;

            gridViewHolder.mTextView_title.setText(list.get(position).getTitle());
            gridViewHolder.mTextView_price.setText(list.get(position).getPrice()+"");

            String images = list.get(position).getImages();
            Pattern pattern = Pattern.compile("\\|");
            String[] img = pattern.split(images);

            Uri uri = Uri.parse(img[0]);
            gridViewHolder.mSimpleDraweeView.setImageURI(uri);

            gridViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener!=null){
                        int layoutPosition = gridViewHolder.getLayoutPosition();
                        String title = list.get(position).getTitle();
                        String price = list.get(position).getPrice()+"";

                        mOnItemClickListener.onItemClick(layoutPosition,title,price);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class LineViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_line_img)
        SimpleDraweeView mSimpleDraweeView;

        @BindView(R.id.item_line_text_title)
        TextView mTextView_title;

        @BindView(R.id.item_line_text_price)
        TextView mTextView_price;
        public LineViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this,itemView);
        }
    }

    public class GridViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_grid_img)
        SimpleDraweeView mSimpleDraweeView;

        @BindView(R.id.item_grid_text_title)
        TextView mTextView_title;

        @BindView(R.id.item_grid_text_price)
        TextView mTextView_price;
        public GridViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this,itemView);
        }
    }
}
item_linelayout.xml


    

    
        

        
    

line_gridlayout.xml



    

    
        

        
    
IModelImpl.java
public class IModelImpl implements IModel{

    @Override
    public void requestData(String url, Map map, final Class clazz, int z, final MyCallBack myCallBack) {

        RetrofitManager.getInstance().post(url,map).result(new RetrofitManager.HttpListener() {
            @Override
            public void onFail() {

            }

            @Override
            public void onResponse(String json) {

                Object o = new Gson().fromJson(json, clazz);
                myCallBack.setData(o);
            }
        });
    }
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity{
    private String mTitle;
    private String mPrice;

    @BindView(R.id.second_title)
    TextView mTextView_title;

    @BindView(R.id.second_price)
    TextView mTextView_price;

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

        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        mTextView_title.setText(mTitle);
        mTextView_price.setText(mPrice);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void sjx(EventBusBean bus){
         mTitle = bus.getTitle();
         mPrice = bus.getPrice();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
BaseApis.java
public interface BaseApis {

    @GET
    Observable get(@Url String url);

    @POST
    Observable post(@Url String url, @QueryMap Map map);

}
RetrofitManager.java
public class RetrofitManager {

    private static RetrofitManager mRetrofitManager;
    private final String BASE_URL="http://www.zhaoapi.cn";
    private final OkHttpClient mOkHttpClient;
    private final BaseApis mBaseApis;

    public RetrofitManager() {

         mOkHttpClient = new OkHttpClient.Builder()
                 .readTimeout(5000, TimeUnit.MILLISECONDS)
                 .writeTimeout(5000,TimeUnit.MILLISECONDS)
                 .connectTimeout(5000,TimeUnit.MILLISECONDS)
                 .retryOnConnectionFailure(true)
                 .build();

        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .client(mOkHttpClient)
                .build();

         mBaseApis = retrofit.create(BaseApis.class);
    }

    public static synchronized RetrofitManager getInstance(){

        if (mRetrofitManager==null){
            mRetrofitManager=new RetrofitManager();
        }
        return mRetrofitManager;
    }

    private Observer mObserver=new Observer(){
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(ResponseBody responseBody) {

            try {
                String json = responseBody.string();

                if (mHttpListener!=null){
                    mHttpListener.onResponse(json);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    public interface HttpListener{
        void onFail();
        void onResponse(String json);
    }

    private HttpListener mHttpListener;

    public void result(HttpListener httpListener){
        mHttpListener=httpListener;
    }


    public RetrofitManager get(String url){
        mBaseApis.get(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mObserver);
        return mRetrofitManager;
    }

    public RetrofitManager post(String url, Map map){
        if (map==null){
            map = new HashMap<>();
        }

        mBaseApis.post(url,map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mObserver);
        return mRetrofitManager;
    }

}

 

你可能感兴趣的:(Rv线性布局与网格切换-Butterknife-Fresco-EventBus-Retrofit)