Horizontal ListView in Android?

后端 未结 19 973
深忆病人
深忆病人 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

     <com.devsmart.android.ui.HorizontalListView
        android:id="@+id/hlistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    

    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;
    }
    
    0 讨论(0)
提交回复
热议问题