Horizontal ListView in Android?

后端 未结 19 1193
深忆病人
深忆病人 2020-11-22 03:54

Is it possible to make the ListView horizontally? I have done this using a gallery view, but the selected item comes to the center of the screen automatically.

19条回答
  •  温柔的废话
    2020-11-22 04:42

    Download the jar file from here

    now put it into your libs folder, right click it and select 'Add as library'

    now in main.xml put this code

     
    

    now in Activity class if you want Horizontal Listview with images then put this code

      HorizontalListView hListView = (HorizontalListView) findViewById(R.id.hlistview);
        hListView.setAdapter(new HAdapter(this));
    
    
     private class HAdapter extends BaseAdapter {
    
        LayoutInflater inflater;
    
        public HAdapter(Context context) {
            inflater = LayoutInflater.from(context);
    
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Const.template.length;
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listinflate, null);
                holder = new HViewHolder();
                convertView.setTag(holder);
    
            } else {
                holder = (HViewHolder) convertView.getTag();
            }
            holder.img = (ImageView) convertView.findViewById(R.id.image);
            holder.img.setImageResource(Const.template[position]);
            return convertView;
        }
    
    }
    
    class HViewHolder {
        ImageView img;
    }
    

提交回复
热议问题