xrecycleview上下拉加载 搜索

依赖
implementation ‘com.squareup.okhttp3:okhttp:3.12.0’
implementation ‘com.github.bumptech.glide:glide:4.8.0’
implementation(‘com.jcodecraeer:xrecyclerview:1.5.9’) {
exclude group: ‘com.android.support’
}
implementation ‘com.google.code.gson:gson:2.8.5’
implementation ‘com.youth.banner:banner:1.4.10’
implementation ‘com.android.support:recyclerview-v7:28.0.0’

权限
android:name=".core.DTApplication"

1.MainActivity

public class MainActivity extends AppCompatActivity implements XRecyclerView.LoadingListener,
        DataCall<List<Goods>>, View.OnClickListener,GoodsListAdapter.OnItemClickListener,
        GoodsListAdapter.OnItemLongClickListener {

    private XRecyclerView mRecyclerView;
    private GoodsListAdapter mAdapter;
    private LinearLayoutManager mLinearManager;
    private GridLayoutManager mGridManager;

    private static final int GRID_LAYOUT_MANAGER = 1;
    private static final int LINEAR_LAYOUT_MANAGER = 2;


    private ImageView mBtnLayout;
    private EditText mKeywordsEdit;

    //新建商品列表Presenter
    private GoodsListPresenter mPresenter = new GoodsListPresenter(this);

    /**
     * 在onCreate方法里面查找Recylerview,并且设置上适配器。
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //检查权限 模拟器版本大于6.0用  所以没什么用 目前模拟器
        if (ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
        }
        mKeywordsEdit = findViewById(R.id.edit_keywords);
        mBtnLayout = findViewById(R.id.btn_layout);

        findViewById(R.id.btn_search).setOnClickListener(this);
        findViewById(R.id.shop_car).setOnClickListener(this);
        mBtnLayout.setOnClickListener(this);

        mRecyclerView = findViewById(R.id.list_goods);//查找mRecyclerView
        mRecyclerView.setLoadingListener(this);//添加下拉和刷新的监听器

  //      initRecycleViewAnimator();//初始化RecyclerView动画

//        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mGridManager = new GridLayoutManager(this, 2,
                GridLayoutManager.VERTICAL, false);//网格布局
        int spacing = 20;
        mRecyclerView.addItemDecoration(new SpacingItemDecoration(spacing));


        mLinearManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);//线性布局
        mRecyclerView.setLayoutManager(mLinearManager);

        mAdapter = new GoodsListAdapter(this);//新建适配器
        mAdapter.setOnItemClickListener(this);//点击事件
        mAdapter.setOnItemLongClickListener(this);//长按
        mRecyclerView.setAdapter(mAdapter);//设置适配器

        //加载数据
//        mRecyclerView.refresh();//刷新
    }



    /**
     * 刷新方法,获取关键字请求数据
     */
    @Override
    public void onRefresh() {
        String keywords = mKeywordsEdit.getText().toString();
        mPresenter.requestData(true, keywords);
    }

    /**
     * 加载更多方法,获取关键字请求数据
     */
    @Override
    public void onLoadMore() {
        String keywords = mKeywordsEdit.getText().toString();
        mPresenter.requestData(false, keywords);
    }

    @Override
    public void success(List<Goods> data) {
        mRecyclerView.refreshComplete();//结束刷新
        mRecyclerView.loadMoreComplete();//结束加载更多
        if (mPresenter.isResresh()) {//只有刷新需要清空数据
            mAdapter.clearList();
        }
        mAdapter.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void fail(Result result) {
        mRecyclerView.refreshComplete();//刷新完成,隐藏刷新view
        mRecyclerView.loadMoreComplete();//加载完成,隐藏加载view
        Toast.makeText(this, result.getCode() + "  " + result.getMsg(),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.unBindCall();//释放引用,防止内存溢出
    }

    private boolean isGrid = false;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_search) {//搜索
            mRecyclerView.refresh();//调用列表刷新,刷新方法中获取输入框的值进行自动请求;
        } else if (v.getId() == R.id.btn_layout) {//切换布局

//            if (!isGrid) {
            if (mRecyclerView.getLayoutManager().equals(mLinearManager)) {
//            if (mAdapter.getItemViewType(0) == GoodsListAdapter.LINEAR_TYPE) {
                isGrid = true;
                mAdapter.setViewType(GoodsListAdapter.GRID_TYPE);
                mRecyclerView.setLayoutManager(mGridManager);
            } else {
                isGrid = false;
                mAdapter.setViewType(GoodsListAdapter.LINEAR_TYPE);
                mRecyclerView.setLayoutManager(mLinearManager);
            }
            mAdapter.notifyDataSetChanged();
        }else if (v.getId() == R.id.shop_car){//进入购物车
//            Intent intent = new Intent(this,ShopCartActivity1.class);
            Intent intent = new Intent(this,ShopCartActivity2.class);
            startActivity(intent);
        }
    }

    @Override
    public void onItemClick(Goods goods) {
        Intent intent = new Intent(this,WebActivity.class);
        intent.putExtra("url",goods.getDetailUrl());
        startActivity(intent);
    }

    @Override
    public void onItemLongClick(int position) {
        mAdapter.remove(position);
        mAdapter.notifyItemRemoved(position+1);//xRecyclerView如果增加了header,childView的序号就需要增加headerview的数量
        if (position < mAdapter.getItemCount()+1) {
            mAdapter.notifyItemRangeChanged(position+1,mAdapter.getItemCount()-position);
        }
    }
}

2.WebActivity

public class WebActivity extends AppCompatActivity {
    WebView mWebView;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        mWebView = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = mWebView.getSettings();

        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        String url = getIntent().getStringExtra("url");
        Log.i("dt", url);

        //如果不设置WebViewClient,请求会跳转系统浏览器
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //该方法在Build.VERSION_CODES.LOLLIPOP以前有效,从Build.VERSION_CODES.LOLLIPOP起,建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
                //返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
                //返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
                //返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
                return false;
            }
        });

        // 先载入JS代码
        // 格式规定为:file:///android_asset/文件名.html
        mWebView.loadUrl(url);

        // webview只是载体,内容的渲染需要使用webviewChromClient类去实现
        // 通过设置WebChromeClient对象处理JavaScript的对话框
        //设置响应js 的Alert()函数
        mWebView.setWebChromeClient(new WebChromeClient());

    }

}

3.activity_main.xml

<?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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <ImageView
            android:id="@+id/btn_back"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:padding="15dp"
            android:src="@drawable/btn_back" />

        <ImageView
            android:id="@+id/btn_layout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:padding="15dp"
            android:src="@drawable/even_city_draw" />

        <EditText
            android:id="@+id/edit_keywords"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/btn_layout"
            android:layout_toRightOf="@+id/btn_back"
            android:background="@drawable/search_edit_bg"
            android:hint="搜索"
            android:paddingLeft="40dp"
            android:singleLine="true"
            android:text="手机"
            android:textColor="@color/custom_gray"
            android:textColorHint="@color/grayblack"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/btn_search"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/edit_keywords"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@android:drawable/ic_search_category_default" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignRight="@+id/edit_keywords"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/person_list_sound_up" />
    </RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:src="@android:color/darker_gray" />

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

        <TextView
            android:id="@+id/cate_text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="综合▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="价格▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="销量▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>

        <TextView
            android:id="@+id/cate_text4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="筛选▲"
            android:textColor="@color/set_font_color"
            android:textSize="14sp"></TextView>
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:src="@android:color/darker_gray" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.jcodecraeer.xrecyclerview.XRecyclerView
            android:id="@+id/list_goods"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.jcodecraeer.xrecyclerview.XRecyclerView>
        <ImageView
            android:id="@+id/shop_car"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="50dp"
            android:src="@drawable/gouwuc_r"/>
    </RelativeLayout>
</LinearLayout>

4.activity_web.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

5.adapter里的GoodsListAdapter

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsHodler> {

    private List<Goods> mList = new ArrayList<>();//数据集合
    private Context context;


    public final static int LINEAR_TYPE = 0;//线性
    public final static int GRID_TYPE = 1;//网格

    private int viewType = LINEAR_TYPE;

    private OnItemClickListener onItemClickListener;

    private OnItemLongClickListener onItemLongClickListener;

    public GoodsListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return viewType;
    }

    //设置item的视图类型
    public void setViewType(int viewType) {
        this.viewType = viewType;
    }


    @NonNull
    @Override
    public GoodsHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = null;
        if (viewType == LINEAR_TYPE) {//通过第二个参数viewType判断选用的视图
            view = View.inflate(viewGroup.getContext(), R.layout.goods_linear_item, null);//加载item布局
        } else {
            view = View.inflate(viewGroup.getContext(), R.layout.goods_grid_item, null);//加载item布局
        }
        GoodsHodler goodsHodler = new GoodsHodler(view);

        return goodsHodler;
    }

    @Override
    public void onBindViewHolder(@NonNull final GoodsHodler goodsHodler, final int position) {
        final Goods goods = mList.get(position);//拿到商品,开始赋值

        goodsHodler.itemView.setTag(mList.get(position));

        //增加点击事件
        goodsHodler.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //*******跳转webActivity进行网页访问
//                Intent intent = new Intent(context,WebActivity.class);
//                intent.putExtra("url",goods.getDetailUrl());
//                context.startActivity(intent);

                //————————跳转自定义的详情页面
                Intent intent = new Intent(context,GoodsDetailActivity.class);
                Bundle bundle = new Bundle();//使用bundle传递引用数据类型的对象
                bundle.putSerializable("goods",goods);
                intent.putExtras(bundle);//一定要把值放入了。
                context.startActivity(intent);

//                if (onItemClickListener!=null) {
//                onItemClickListener.onItemClick(goods);
//                }
            }
        });
        goodsHodler.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //*****************方案1*************
//                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                        goodsHodler.itemView,"alpha",1,0
//                );
//                objectAnimator.setDuration(1000);
//                objectAnimator.setInterpolator(new LinearInterpolator());
//                objectAnimator.addListener(new Animator.AnimatorListener() {
//                    @Override
//                    public void onAnimationStart(Animator animation) {
//
//                    }
//
//                    @Override
//                    public void onAnimationEnd(Animator animation) {
//                        mList.remove(position);//动画执行结束,移除
//                        notifyDataSetChanged();//刷新列表
//                        goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
//                    }
//
//                    @Override
//                    public void onAnimationCancel(Animator animation) {
//                        mList.remove(position);//动画执行结束,移除
//                        notifyDataSetChanged();//刷新列表
//                        goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
//                    }
//
//                    @Override
//                    public void onAnimationRepeat(Animator animation) {
//
//                    }
//                });
//                objectAnimator.start();

                //***********方案二**************
                if (onItemLongClickListener!=null) {
                    onItemLongClickListener.onItemLongClick(position);
                }
                return true;
            }
        });

        goodsHodler.text.setText(goods.getTitle());
        //由于我们的数据图片提供的不标准,所以我们需要切割得到图片
        String imageurl = "https" + goods.getImages().split("https")[1];
        Log.i("dt", "imageUrl: " + imageurl);
        imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
        Glide.with(context).load(imageurl).into(goodsHodler.imageView);//加载图片
    }

    public static void main(String[] args) {
        String aa = "a111a222a333a";
        String[] b = aa.split("a");
        System.out.println(b[0]);
    }

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

    /**
     * 添加集合数据
     */
    public void addAll(List<Goods> data) {
        if (data != null) {
            mList.addAll(data);
        }
    }

    /**
     * 清空数据
     */
    public void clearList() {
        mList.clear();
    }

    /**
     * 设置点击方法
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void remove(int position) {
        mList.remove(position);
    }

    public class GoodsHodler extends RecyclerView.ViewHolder {
        TextView text;
        ImageView imageView;

        public GoodsHodler(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            imageView = itemView.findViewById(R.id.image);
        }
    }

    /**
     * @author dingtao
     * @date 2018/12/15 10:28 AM
     * 点击接口
     */
    public interface OnItemClickListener {
        void onItemClick(Goods goods);
    }

    /**
     * @author dingtao
     * @date 2018/12/15 10:28 AM
     * 点击接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}

5.1goods_grid_item.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:background="@drawable/search_edit_bg"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aa"
        android:padding="10dp"/>

</LinearLayout>

5.2goods_linear_item.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:background="@drawable/search_edit_bg"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_margin="10dp"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aa"
        android:padding="10dp"/>

</LinearLayout>

6.core里的DataCall

public interface DataCall<T> {

    void success(T data);

    void fail(Result result);

}

7.core里的BasePresenter

public abstract class BasePresenter {

    DataCall dataCall;

    public BasePresenter(DataCall dataCall){
        this.dataCall = dataCall;
    }


    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {

            Result result = (Result) msg.obj;
            if (result.getCode()==0){
                dataCall.success(result.getData());
            }else{
                dataCall.fail(result);
            }
        }
    };



    public void requestData(final Object...args){
        new Thread(new Runnable() {
            @Override
            public void run() {


                Message message = mHandler.obtainMessage();
                message.obj = getData(args);
                mHandler.sendMessage(message);

            }
        }).start();
    }

    protected abstract Result getData(Object...args);

    public void unBindCall(){
        this.dataCall = null;
    }

}

8.core里的DTApplication

public class DTApplication extends Application {

    private static DTApplication instance;
    private SharedPreferences mSharedPreferences;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mSharedPreferences = getSharedPreferences("application",
                Context.MODE_PRIVATE);
       /* JPushInterface.setDebugMode(true);
        JPushInterface.init(this);     	*/	// 初始化 JPush
    }

    public static DTApplication getInstance() {
        return instance;
    }

    public SharedPreferences getShare() {
        return mSharedPreferences;
    }

}

9.model里的GoodsListModel

public class GoodsListModel {

    public static Result goodsList(String keywords, final String page) {
        String resultString = HttpUtils.postForm("http://www.zhaoapi.cn/product/searchProducts",
                new String[]{"keywords", "page"}, new String[]{keywords, page});

        try {
            Gson gson = new Gson();

            Type type = new TypeToken<Result<List<Goods>>>() {
            }.getType();

            Result result = gson.fromJson(resultString, type);
            return result;
        } catch (Exception e) {

        }
        Result result = new Result();
        result.setCode(-1);
        result.setMsg("数据解析异常");
        return result;
    }

}

10.presenter里的GoodsListPresenter


public class GoodsListPresenter extends BasePresenter {

    private int page=1;
    private boolean isRefresh=true;

    public GoodsListPresenter(DataCall dataCall) {
        super(dataCall);
    }

    @Override
    protected Result getData(Object... args) {
        isRefresh = (boolean) args[0];//是否需要刷新
        if (isRefresh){//刷新
            page = 1;
        }else{
            page++;
        }
        Result result = GoodsListModel.goodsList((String)args[1],page+"");//调用网络请求获取数据
        return result;
    }

    public boolean isResresh(){
        return isRefresh;
    }
}

11.条目点击跳转的页面 加入购车GoodsDetailActivity

public class GoodsDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private Goods mGoods;//商品详情

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

        Bundle bundle = getIntent().getExtras();
        mGoods = (Goods) bundle.getSerializable("goods");//读取列表传递过来的商品数据

        initBanner();

        findViewById(R.id.goods_add_cart_btn).setOnClickListener(this);

    }

    /**
     * 初始化banner
     */
    private void initBanner() {

        List<String> imageList = new ArrayList<>();//图片url集合
        String[] imageurls = mGoods.getImages().split("https");//对图片进行切割
        for (int i = 0; i < imageurls.length; i++) {
            if (!TextUtils.isEmpty(imageurls[i])) {
                String url = "https" + imageurls[i];
                Log.i("dt", "imageUrl: " + url);
                url = url.substring(0, url.lastIndexOf(".jpg") + ".jpg".length());
                imageList.add(url);//图片路径拼接完成,重新赋值给数组
            }
        }

        Banner banner = findViewById(R.id.goods_banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置banner样式
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);

        //设置图片集合
        banner.setImages(imageList);
        //设置标题集合(当banner样式有显示title时)
//        banner.setBannerTitles(titleList);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播,默认为true
        banner.isAutoPlay(false);
        //设置轮播时间
//        banner.setDelayTime(1500);
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.goods_add_cart_btn){
            Toast.makeText(this,"加入购物车",Toast.LENGTH_LONG).show();
        }
    }
}


12.条目点击跳转的页面 加入购车GoodsDetailActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <com.youth.banner.Banner
                android:id="@+id/goods_banner"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
        </LinearLayout>

    </ScrollView>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <Button
            android:id="@+id/goods_add_cart_btn"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@color/mark_red"
            android:textColor="@color/white"
            android:layout_alignParentRight="true"
            android:text="加入购物车"/>
    </RelativeLayout>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_43603372/article/details/85137143