仿淘宝商品列表切换

时间:2021-01-04 14:46:02

仿淘宝商品列表切换

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

布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
tools:context="com.example.a3_.MainActivity">

<Button
android:id="@+id/change"
android:text="切换布局"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

<GridView
android:id="@+id/gv"
android:numColumns="2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
</FrameLayout>

核心代码

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