本文实例为大家分享了Android仿淘宝商品列表切换,供大家参考,具体内容如下<
源码:Android实现淘宝商品列表切换
布局文件
<&#63;xml version="1.0" encoding="utf-8"&#63;>
核心代码
package com.example.a3_; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private ListView lv; private GridView gv; private Button button; //设置默认的显示或隐藏 private boolean isLvShow = true; private boolean isGvShow = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 lv = (ListView) findViewById(R.id.lv); gv = (GridView) findViewById(R.id.gv); button = (Button) findViewById(R.id.change); //设置适配器 lv.setAdapter(new MyAdapter()); gv.setAdapter(new MyAdapter()); //为按钮设置点击事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isLvShow){ lv.setVisibility(View.GONE); gv.setVisibility(View.VISIBLE); }else { lv.setVisibility(View.VISIBLE); gv.setVisibility(View.GONE); } //对显示或隐藏进行取反 isLvShow = !isLvShow; isGvShow = !isGvShow; } }); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //为listview设置布局 if (cOnvertView==null){ cOnvertView= View.inflate(getApplicationContext(),R.layout.item,null); } return convertView; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。