Android常用控件之ListView

目录:android.widget.ListView

xml布局




    

调用代码:

public class MainActivity extends AppCompatActivity {
    private ListView lv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {

        //数据源
        List list = new ArrayList<>();
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");

        lv_list = (ListView) findViewById(R.id.lv_list);
        lv_list.setAdapter(new ListAdapter(list));
    }

    private class ListAdapter extends BaseAdapter{
        private List list;
        public ListAdapter(List list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item_icon_text, parent, false);
                holder = new ViewHolder();
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //处理逻辑
            holder.processData(position);

            return convertView;
        }

    }

    private static class ViewHolder {
        private TextView tv_title;

        private void processData(int position) {
            tv_title.setText("我是item"+position);
        }
    }

}

解决ScrollView嵌套ListView冲突解决方案

你可能感兴趣的:(Android常用控件之ListView)