做的android app中需要嵌入一个产品商城的功能。简单用com.handmark.pulltorefresh.library.PullToRefreshGridView显示一个产品列表完了。现在增加功能,需要把产品分成两个部分显示,一个是热卖产品,一个是所有产品。于是想到使com.handmark.pulltorefresh.library.PullToRefreshScrollView中嵌套两个GridView,GridView自定义,显示时计算条目的高度。
效果图:
主要代码:
MyGridView.java:
public class MyGridView extends GridView {
public MyGridView (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView (Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
布局文件product_home.xml主要代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/ptr_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.demo.view.MyGridView
android:id="@+id/productlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:numColumns="2" />
<com.demo.view.MyGridView
android:id="@+id/allproductlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:numColumns="2" />
</LinearLayout> </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</RelativeLayout>
MainActivity代码:
“`
package com.demo.activity;
public class MainActivity extends Activity {
private PullToRefreshScrollView ptr_scrollview;//
private MyGridView productGridView;//
private MyGridView productAllGridView;//
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_home);
initView();
}
private void initView() {
ptr_scrollview = (PullToRefreshScrollView) findViewById(R.id.ptr_scrollview);
ptr_scrollview.setMode(Mode.PULL_FROM_END);// 上拉刷新
productGridView = (MyGridView) findViewById(R.id.productlist);
productAllGridView = (MyGridView) findViewById(R.id.allproductlist);
ptr_scrollview
.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
@Override
public void onRefresh(
PullToRefreshBase<ScrollView> refreshView) {
// TODO Auto-generated method stub
}
});
productGridView
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1,
int pos, long id) {
}
});
productAllGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
}
});
}
}
做个记录。