仿淘宝商品列表切换

仿淘宝商品列表切换

源码:http://download.csdn.net/detail/qq_32890771/9864526

布局文件




核心代码

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;
    }
}
}

你可能感兴趣的:(个人笔记)