ListView每个屏幕一项

时间:2012-12-12 07:28:26

标签: android android-layout

由于DirectionalViewPager现在已被弃用,我需要一些方法来模仿其垂直分页功能。

我想到的第一件事是ListView,但我想要实现每屏幕行功能的一些技巧 - 我从哪里开始?

感谢。

1 个答案:

答案 0 :(得分:0)

public class Top extends ListActivity {

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    String[] rows = {1,2,3,4}; //This rows array Determine the no of rows to be displayed on the list
    setContentView(R.layout.top);//layout for activity
    setListAdapter(new MyAdapter(this, R.layout.top_list, R.id.topName, rows);  
}

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId, String[] objects) 
    {
        super(context, resource, textViewResourceId, objects);          
    }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
        {               
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View row = inflater.inflate(R.layout.top_list, parent, false); //top_list is the seperate layout that represents your list consider it has one TextView.
            String[] rows = {1,2,3,4};          
            TextView tv = (TextView)findViewById(R.id.TextView1);   
            tv.setText(rows[position]);

            return row;
        }           
    }
}

1。扩展ListActivity而不是Activity 2.为活动创建两个布局,为列表创建另一个布局 3. rows数组确定listview中列表的no 4.该职位确定了清单编号。

我希望这有助于询问是否需要其他澄清。