【问题标题】:For creating custom list view as a library in android用于在 android 中创建自定义列表视图作为库
【发布时间】:2013-04-04 04:34:44
【问题描述】:

我们想使用适配器创建一个列表视图,并且我们想在适配器之外设置列表项的属性。

例如,列表视图包含 200 行和 14 列,那么我们需要使用适配器创建列表项。这里适配器中的对象是根据屏幕上显示的项目创建的。

在创建适配器之后,在外部适配器之后,我们要为项目 198 放置 getter,setter 表示。如果最初在设备中显示最多 10 个项目,则表示然后在适配器中创建 10 个项目对象,但剩余的项目在用户创建时创建向下滚动

所以 198 项出现空指针执行。


我想创建一个 ListView 作为自定义组件。用户可以添加任意数量的行、任意数量的列等
任何项目作为列表视图等


我的目标是为列表视图创建库。在该列表视图中,用户可以添加任意数量的行、任意数量的列、任何项目文本视图、微调器等。我需要如何实现它的建议

欢迎大家提出自己的想法。

【问题讨论】:

  • 请发布您的代码。而这和android-2.3有什么关系?
  • 你想从本地实现一些库或代码来制作一个库项目吗?

标签: android android-listview custom-controls android-custom-view android-2.3-gingerbread


【解决方案1】:

你需要实现LazyListView。

我认为你有一个不错的 tutorial here 开头。

别忘了将此标记为答案。

【讨论】:

    【解决方案2】:

    尝试使用: 你的 xml 列表文件 item.xml:

    <LinearLayout>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckBox
    android:id="@+id/cbBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </CheckBox>
    <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_weight="1"
    android:orientation="vertical">
    <TextView
    android:id="@+id/tvDescr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text=""
    android:textSize="20sp">
    </TextView>
    <TextView
    android:id="@+id/tvPrice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:text="">
    </TextView>
    </LinearLayout>
    <ImageView
    android:id="@+id/ivImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher">
    </ImageView>
    <LinearLayout/>
    

    还有你的类适配器

    public class BoxAdapter extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Product> objects;
    
    BoxAdapter(Context context, ArrayList<Product> products) {
    ctx = context;
    objects = products;
    lInflater = (LayoutInflater) ctx
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
    return objects.size();
    }
    
    @Override
    public Object getItem(int position) {
    return objects.get(position);
    }
    
    @Override
    public long getItemId(int position) {
    return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
    view = lInflater.inflate(R.layout.item, parent, false);
    }
    
    Product p = getProduct(position);
    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
    
    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
    cbBuy.setOnCheckedChangeListener(myCheckChangList);
    cbBuy.setTag(position);
    cbBuy.setChecked(p.box);
    return view;
    }
    
    Product getProduct(int position) {
    return ((Product) getItem(position));
    }
    
    ArrayList<Product> getBox() {
    ArrayList<Product> box = new ArrayList<Product>();
    for (Product p : objects) {
    if (p.box)
    box.add(p);
    }
    return box;
    }
    
    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
     getProduct((Integer) buttonView.getTag()).box = isChecked;
    }
    }; 
    }
    

    还有你的产品类

    public class Product {
    String name;
    int price;
    int image;
    boolean box;
    
    Product(String _describe, int _price, int _image, boolean _box) {
    name = _describe;
    price = _price;
    image = _image;
    box = _box;
    }
    }
    

    您的主要活动使用创建适配器

    boxAdapter = new BoxAdapter(this, products);
    ArrayList<Product> products = new ArrayList<Product>();
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState) 
    ...
    //create adapter
    fillData()
    boxAdapter = new BoxAdapter(this, products);
    
    // use lists
    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(boxAdapter);
    }
    
    // data for adapter
    void fillData() {
    for (int i = 1; i <= 20; i++) {
    products.add(new Product("Product " + i, i * 1000, R.drawable.ic_launcher, false));
    }
    

    【讨论】:

    • ZHARIK86 你的解释对我非常有用。但我的目标是为列表视图创建库。在该列表视图中,用户可以添加任意数量的行、任意数量的列、任何项目 textview、spinner等。给你的建议如何实现它
    • SIVAKUMAR.J,据我了解的任务,有必要这样做。例如,如果您想要用户定义的列表外观,可以首先默认创建一个外观并将其用作 LayoutInflater。此外,我们在某个按钮上绑定了 AlertDialog 的打开事件(或者 FrameDialog,因为它更方便)。在这个对话框中,我们显示了我们最初的位置,并在我们添加其他元素的列表中退出。
    • SIVAKUMAR.J,有必要不要忘记删除不适合我们的元素。用户决定一个集合后,我们对其进行存储,使用getWindow()。其余操作与上面已经给出的类似。
    猜你喜欢
    • 2014-10-15
    • 2014-12-04
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多