显示界面切换样式

使用mvp
在view层中,Activity

package com.example.week2.View;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.week2.Moudel.bean.SearchBean;
import com.example.week2.Presenter.SearchPst;
import com.example.week2.R;
import com.example.week2.View.Adapater.MyAdapaterSearchSuccessListView;
import com.example.week2.View.Adapater.MyAdapterSearchSuccessGridView;
import com.example.week2.View.inter.SearchMainInterVw;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


public class SearchActivity extends AppCompatActivity implements SearchMainInterVw, View.OnClickListener {

    private EditText edname;
    private SearchPst searchPst;
    private String an="android";
    private String decode;
    private RecyclerView recyclerview;
    private List<SearchBean.DataBean> list=new ArrayList<>();
    private boolean flog=true;
    private MyAdapaterSearchSuccessListView adapaterSearchSuccessListView;
    private ImageView imagehuan;
    private MyAdapterSearchSuccessGridView gridView;
    private LinearLayout show;
    private LinearLayout yinchang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_search);
        edname = (EditText) findViewById(R.id.search_edname);
        imagehuan = (ImageView) findViewById(R.id.searchmainactivity_imagview);
        recyclerview = (RecyclerView) findViewById(R.id.search_recyclerview);
        show = (LinearLayout) findViewById(R.id.search_show);
        yinchang = (LinearLayout) findViewById(R.id.search_yinchang);
        searchPst = new SearchPst(this);
        imagehuan.setOnClickListener(this);
    }

    public void searchname(View view) {
        String ednamea = this.edname.getText().toString();
        edname.setText("");
        String strGBK = null;
        try {
            strGBK = URLEncoder.encode(ednamea, "GBK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ednamea.equals("笔记本")) {
            Log.d("aaa", "开始执行.............." + strGBK);
            searchPst.setdata("http://120.27.23.105/product/searchProducts", ednamea, 1, an);
            show.setVisibility(View.VISIBLE);
            yinchang.setVisibility(View.GONE);
        }else{
            show.setVisibility(View.GONE);
            yinchang.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onSearchSuccess(final SearchBean bean) {
        Log.d("aaa","Main接受到了P的bean:"+bean);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                list = bean.getData();
                setadapaterlistview();
            }
        });
    }

    private void setadapaterlistview() {
        recyclerview.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
        adapaterSearchSuccessListView = new MyAdapaterSearchSuccessListView(list,SearchActivity.this);
        recyclerview.setAdapter(adapaterSearchSuccessListView);
        adapaterSearchSuccessListView.notifyDataSetChanged();
    }
   private  void setadapatergridview(){
       recyclerview.setLayoutManager(new GridLayoutManager(SearchActivity.this,2));
       gridView = new MyAdapterSearchSuccessGridView(list,SearchActivity.this);
       recyclerview.setAdapter(gridView);
       gridView.notifyDataSetChanged();
   }

    @Override
    public void onClick(View view) {
        flog=!flog;
        if (flog==true){
            setadapaterlistview();
            imagehuan.setImageResource(R.drawable.gg);
        }else{
            setadapatergridview();
            imagehuan.setImageResource(R.drawable.lsv);
        }
    }
}
view下的inter

package com.example.week2.View.inter;

import com.example.week2.Moudel.bean.SearchBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface SearchMainInterVw {
    void onSearchSuccess(SearchBean bean);

}
view层下的appliction

package com.example.week2.View.Appliction;

import android.app.Application;

import com.example.week2.View.Utlis.ImageUtlis;


/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class MyAppliction extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageUtlis.initdata(this);
    }
}
view层下的adapter

package com.example.week2.View.Adapater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.week2.Moudel.bean.SearchBean;
import com.example.week2.R;
import com.example.week2.View.Holoder.MyHoloderSearchSuccessListView;
import com.example.week2.View.SearchActivity;
import com.example.week2.View.Utlis.ImageUtlis;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class MyAdapaterSearchSuccessListView extends RecyclerView.Adapter<MyHoloderSearchSuccessListView> {
    List<SearchBean.DataBean> list;
    Context context;
    List<String> img=new ArrayList<>();

    public MyAdapaterSearchSuccessListView(List<SearchBean.DataBean> list, SearchActivity context) {
        this.list=list;
        this.context=context;
    }

    @Override
    public MyHoloderSearchSuccessListView onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHoloderSearchSuccessListView holoder=new MyHoloderSearchSuccessListView(LayoutInflater.from(context).inflate(R.layout.layout_searchsuccesslistview_item,parent,false));
        return holoder;
    }

    @Override
    public void onBindViewHolder(MyHoloderSearchSuccessListView holder, int position) {
        String[] split = list.get(position).getImages().split("\\|");
        for (int i=0;i<split.length;i++){
            img.add(split[i]);
        }
        ImageLoader.getInstance().displayImage(img.get(position),holder.imageview, ImageUtlis.imageOptions());
        holder.title.setText(list.get(position).getTitle());
        holder.price.setText("折扣价:"+list.get(position).getPrice());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.View.Adapater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.week2.Moudel.bean.SearchBean;
import com.example.week2.R;
import com.example.week2.View.Holoder.MyHoloderSearchSuccessGridView;
import com.example.week2.View.Utlis.ImageUtlis;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class MyAdapterSearchSuccessGridView extends RecyclerView.Adapter<MyHoloderSearchSuccessGridView>{
    List<SearchBean.DataBean> list;
    Context context;
    private  List<String> img=new ArrayList<>();

    public MyAdapterSearchSuccessGridView(List<SearchBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyHoloderSearchSuccessGridView onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHoloderSearchSuccessGridView holoder=new MyHoloderSearchSuccessGridView(LayoutInflater.from(context).inflate(R.layout.layout_searchsuccessgridview,parent,false));
        return holoder;
    }

    @Override
    public void onBindViewHolder(MyHoloderSearchSuccessGridView holder, int position) {
        String[] split = list.get(position).getImages().split("\\|");
        for (int i=0;i<split.length;i++){
            img.add(split[i]);
        }
        ImageLoader.getInstance().displayImage(img.get(position),holder.imageview, ImageUtlis.imageOptions());
        holder.title.setText(list.get(position).getTitle());
        holder.price.setText("折扣价:"+list.get(position).getPrice());
    }

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

view层下的holder

package com.example.week2.View.Holoder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.week2.R;


/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class MyHoloderSearchSuccessGridView extends RecyclerView.ViewHolder{

    public   ImageView imageview;
    public  TextView title;
    public  TextView price;

    public MyHoloderSearchSuccessGridView(View itemView) {
        super(itemView);
        imageview = (ImageView) itemView.findViewById(R.id.search_success_gridview_item_imageview);
        title = (TextView)itemView.findViewById(R.id.search_success_gridview_item_title);
        price = (TextView)itemView.findViewById(R.id.search_success_gridview_item_price);
    }
}
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.View.Holoder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.week2.R;


/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class MyHoloderSearchSuccessListView extends RecyclerView.ViewHolder{

    public   ImageView imageview;
    public  TextView title;
    public  TextView price;

    public MyHoloderSearchSuccessListView(View itemView) {
        super(itemView);
        imageview = itemView.findViewById(R.id.search_success_listview_item_imageview);
        title = itemView.findViewById(R.id.search_success_listview_item_title);
        price = itemView.findViewById(R.id.search_success_listview_item_price);
    }
}

到这里view层就结束了

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

presenter中的inter

package com.example.week2.Presenter.inter;


import com.example.week2.Moudel.bean.SearchBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface SearchInterPst {
    void onSearchSuccess(SearchBean bean);
}

presenter层

package com.example.week2.Presenter;

import android.util.Log;

import com.example.week2.Moudel.SearchMod;
import com.example.week2.Moudel.bean.SearchBean;
import com.example.week2.Presenter.inter.SearchInterPst;
import com.example.week2.View.inter.SearchMainInterVw;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class SearchPst implements SearchInterPst {

    private  SearchMod searchMod;
    private SearchMainInterVw mainInterVw;

    public SearchPst(SearchMainInterVw mainInterVw) {
        this.mainInterVw=mainInterVw;
        searchMod = new SearchMod(this);
    }

    public void setdata(String s, String edname, int i, String an) {
        Log.d("aaa","执行到了P:"+s+edname+i+an);
        searchMod.setdata(s,edname,i,an);
    }

    @Override
    public void onSearchSuccess(SearchBean bean) {
        Log.d("aaa","P接受到了M的bean:"+bean);
        mainInterVw.onSearchSuccess(bean);
    }
}
moudle层

package com.example.week2.Moudel;

import android.util.Log;

import com.example.week2.Moudel.bean.SearchBean;
import com.example.week2.Moudel.utlis.OkHttpUtlis;
import com.example.week2.Presenter.inter.SearchInterPst;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class SearchMod {
    private SearchInterPst interPst;

    public SearchMod(SearchInterPst interPst) {
        this.interPst=interPst;
    }

    public void setdata(String s, String edname, int a, String an) {
        Log.d("aaa","执行到了M:"+s+edname+a+an);
        OkHttpUtlis.doGet(s + "?keywords=" + edname + "&page=" + a + "&source=" + an, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                 if (response.isSuccessful()){
                     String string = response.body().string();
                     Log.d("aaa","访问成功:"+string);
                     SearchBean bean = new Gson().fromJson(string, SearchBean.class);
                     interPst.onSearchSuccess(bean);
                 }
            }
        });
    }
}
utils类

第一个封装okhttp

package com.example.week2.Moudel.utlis;

import android.os.Environment;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class OkHttpUtlis {
    public OkHttpUtlis() {
    }

    private static OkHttpClient okHttpClient=null;
    public static OkHttpClient getInstance(){
        if (okHttpClient==null){
            synchronized (OkHttpUtlis.class){
                if (okHttpClient==null){
                    File file = new File(Environment.getExternalStorageDirectory(), "cache");
                   int cacheSize=10*1024*1024;
                    okHttpClient=new OkHttpClient.Builder()
                            .connectTimeout(15, TimeUnit.SECONDS)
                            .writeTimeout(20,TimeUnit.SECONDS)
                            .readTimeout(20,TimeUnit.SECONDS)
                            .cache(new Cache(file.getAbsoluteFile(),cacheSize))
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    public static void doGet(String url, Callback callback){
            OkHttpClient okHttpClient=getInstance();
        Request request=new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    public static void doPost(String url, Map<String,String> parmas,Callback callback){
         OkHttpClient okHttpClient=getInstance();
        FormBody.Builder fb=new FormBody.Builder();
        for (String key:parmas.keySet()){
            fb.add(key,parmas.get(key));
        }
        Request request=new Request.Builder().post(fb.build()).url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }
}
第二个inageloader

package com.example.week2.View.Utlis;

import android.content.Context;
import android.graphics.Bitmap;

import com.example.week2.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class ImageUtlis {
    public static   void initdata(Context context){
        File cacheDir = StorageUtils.getCacheDirectory(context);  //缓存文件夹路径
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPoolSize(3) // default  线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)  // 内存缓存的最大值
                .memoryCacheSizePercentage(13) // default
                .diskCache(new UnlimitedDiskCache(cacheDir)) // default 可以自定义缓存路径
                .diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
                .diskCacheFileCount(100)  // 可以缓存的文件数量
                // default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .imageDownloader(new BaseImageDownloader(context)) // default
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                .writeDebugLogs() // 打印debug log
                .build(); //开始构建
        ImageLoader.getInstance().init(config);
    }
    public  static  DisplayImageOptions imageOptions(){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
                .resetViewBeforeLoading(true)  // default 设置图片在加载前是否重置、复位
                .delayBeforeLoading(1000)  // 下载前的延迟时间
                .cacheInMemory(true) // default  设置下载的图片是否缓存在内存中
                .cacheOnDisk(true) // default  设置下载的图片是否缓存在SD卡中
                .considerExifParams(true) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
                .displayer(new CircleBitmapDisplayer()) // default  还可以设置圆角图片new RoundedBitmapDisplayer(20);;CircleBitmapDisplayer()圆形
                .build();
        return options;
    }
}
activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.week2.View.SearchActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:layout_weight="3"
            android:text="搜索商品"
            android:layout_marginTop="5dp"
            android:textSize="25dp"
            android:textColor="#263238"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <ImageView
            android:id="@+id/searchmainactivity_imagview"
            android:layout_weight="1"
            android:src="@drawable/gg"
            android:layout_marginTop="2dp"
            android:layout_width="40dp"
            android:layout_height="40dp" />
    </LinearLayout>
    <TextView
        android:background="#E3E3E5"
        android:layout_width="match_parent"
        android:layout_marginTop="3dp"
        android:layout_height="5dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/search_edname"
            android:hint="笔记本"
            android:layout_weight="10"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/yuan"
            android:layout_height="match_parent" />
        <Button
            android:layout_weight="1"
            android:text="搜素"
            android:onClick="searchname"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:background="#E3E3E5"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="5dp" />
    <LinearLayout
        android:id="@+id/search_show"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
      <android.support.v7.widget.RecyclerView
          android:id="@+id/search_recyclerview"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      </android.support.v7.widget.RecyclerView>
    </LinearLayout>
<include
    android:id="@+id/search_yinchang"
    android:visibility="gone"
    layout="@layout/layout_login"
    />
</LinearLayout>

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


layout_login布局

<?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">
 <LinearLayout
     android:layout_gravity="center"
     android:layout_width="match_parent"
     android:orientation="vertical"
     android:layout_height="wrap_content">
 <pl.droidsonroids.gif.GifImageView
     android:src="@drawable/dongtiao"
     android:layout_gravity="center_vertical"
     android:layout_width="match_parent"
     android:layout_height="50dp" />
 <TextView
     android:text="正在努力开发中哦....."
     android:textSize="20dp"
     android:textColor="#263238"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />
 </LinearLayout>
</LinearLayout>

adapter的两个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#E3E3E3"
    android:layout_margin="16dp"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/search_success_gridview_item_imageview"
        android:layout_width="150dp"
        android:layout_gravity="center_horizontal"
        android:layout_height="150dp" />
    <TextView
        android:id="@+id/search_success_gridview_item_title"
        android:text="标题"
        android:textSize="25dp"
        android:textColor="#263238"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/search_success_gridview_item_price"
        android:text="价钱"
        android:textSize="20dp"
        android:textColor="#FC0000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_margin="16dp"
    android:background="#E3E3E3"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/search_success_listview_item_imageview"
    android:layout_width="150dp"
    android:layout_height="150dp" />
    <LinearLayout
        android:layout_weight="3"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_height="wrap_content">
    <TextView
        android:text="标题"
        android:textSize="25dp"
        android:textColor="#263238"
        android:id="@+id/search_success_listview_item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
        <TextView
            android:text="价格"
            android:textSize="20dp"
            android:textColor="#FF0000"
            android:gravity="center_vertical"
            android:id="@+id/search_success_listview_item_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值