如何创建动态水平列表视图?

时间:2013-07-31 11:09:57

标签: android

如何像这张图片http://imgur.com/hMSZxbp一样显示listview。我想水平显示listview图像和文本并动态加载listview。这个例子http://www.androidconnect.org/2012/05/10/como-hacer-listview-horizontales/加载单个图像形式所有textview和alll textview都是硬编码没有图像加载并行为每个字符串所有数据都是静态的

HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
    listview.setAdapter(new HAdapter());

}

private static String[] dataObjects = new String[]{ "Text #1",
    "Text #2",
    "Text #3",
    "Text android",
    "Text connnect",
    "www.androidconnect.org"};

private class  HAdapter extends BaseAdapter {

    public HAdapter(){
        super();
    }
    private OnClickListener mOnButtonClicked = new OnClickListener() {

        public void onClick(View v) {
            Button bt = (Button) v;

            Toast.makeText(getApplicationContext(), "Hola desde " + bt.getText(),  
 Toast.LENGTH_SHORT).show();          
        }
    };


    public int getCount() {
        return dataObjects.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View retval =    
    LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        Button button = (Button) retval.findViewById(R.id.clickbutton);
        button.setOnClickListener(mOnButtonClicked);
        title.setText(dataObjects[position]);

        return retval;
    }

};

}

0 个答案:

没有答案