前几天看到京东商城的分类页面,感觉还不错
效果图:
于是自己写了一个,说一下具体的功能吧。
1、点击左侧列表可以更新右边的布局,而且item可以自动滑动到最上面。
2、Listvew可以弹性滑动
关键代码:
/**
*
* @author lly
*
*/
public class JDActivity extends FragmentActivity {
private String[] classifys = { "常用分类", "潮流女装", "品牌男装", "内衣配饰", "家用电器",
"手机数码", "电脑办公", "个护化妆", "母婴频道", "食品生鲜", "酒水饮料", "家居家纺", "整车车品",
"鞋靴箱包", "运动户外", "图书", "玩具乐器", "钟表", "居家生活", "珠宝饰品", "音像制品", "家居建材",
"计生情趣", "营养保健" };
private ElasticListview mListView;
private JDAdapter mJdAdapter;
private FragmentTransaction mFragmentTransaction;
private JDFragment jdFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.jd_layout);
initview();
setAdapter();
createFragment();
}
public void createFragment() {
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
jdFragment = new JDFragment();
mFragmentTransaction.add(R.id.lin_fragment, jdFragment);
mFragmentTransaction.commit();
}
public void setAdapter() {
mJdAdapter = new JDAdapter(this, classifys);
mListView.setAdapter(mJdAdapter);
}
private void initview() {
mListView = (ElasticListview) findViewById(R.id.listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (jdFragment != null) {
jdFragment.setData(classifys[position]);
mJdAdapter.setIndex(position);
//位移的代码,获取当前的Y坐标,然后调用smoothScrollBy方法移动Listview
int Pos[] = { -1, -1 }; // 保存当前坐标的数组
view.getLocationOnScreen(Pos);
float OldListY = (float) Pos[1]; // 我们只取 Y 坐标就行了
mListView.smoothScrollBy(
(int) OldListY
- (px2dip(JDActivity.this, view.getHeight())),
1000);
}
}
});
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, int pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
效果图
右边我用的是Fragment做的,每次点击更新数据就行了,这个很简单就不多说了。
Demo下载地址:http://download.csdn.net/detail/ght199266/8494477
PS: 本人新手,大牛勿喷