最近做项目要用到横向的listvew,找了下却实在没有水平方向的listview,
就想到了gallery,虽然Gallery is deprecated,但它的作用个人觉得还是很不错的,
(因本人不会写自定义的水平listview)
gallery实现的效果涂如下:
给gallery添加代码的只要代码如下:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { System.out.println("onItemSelected" + position); } @Override public void onNothingSelected(AdapterView<?> parent) { System.out.println("onNothingSelected"); } }); gallery.setAdapter(new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(getApplicationContext()); tv.setWidth(1800); tv.setBackgroundColor(Color.BLUE); tv.setGravity(Gravity.CENTER); tv.setHeight(120); tv.setText("test:" + position); return tv; } @Override public long getItemId(int position) { return 0; } @Override public Object getItem(int position) { return null; } @Override public int getCount() { return 10; } }); }
gallery.setOnItemSelectedListener其实是监听的是在屏幕中间的那个item
,中间的item在变的时候,这个onItemSelected会一直调用。
之前一直认为listview设置 listView.setOnItemSelectedListener也是一样,径调试当listview的item在滚动的时候
,onItemSelected方法是不会调用的,这是gallery和listview不同的一点。
时间: 2024-10-06 01:53:22