【问题标题】:Android AdapterView?安卓适配器视图?
【发布时间】:2013-01-18 19:33:40
【问题描述】:

Documents说:

当您的布局内容是动态的或不是预先确定的时,您 可以使用子类 AdapterView 的布局来填充布局 在运行时使用视图。 AdapterView 类的子类使用 适配器将数据绑定到其布局。

但是大部分教程都在ListView,GridView,SpinnerGallery

我希望直接从AdapterView 扩展一个子类。我必须创建一个自定义视图,它的内容取决于适配器。

我该怎么做,必须重写哪些方法?

【问题讨论】:

    标签: android android-adapterview


    【解决方案1】:

    首先,您应该绝对确定AdapterView 是您想要的,因为并非所有“动态或非预定”视图都可以通过AdapterView 实现。有时您最好创建扩展 ViewGroup 的视图。

    如果您想使用AdapterView,请查看this really nice example。 GitHub 上有很多带有适配器的自定义视图。 Check out this one (extends ViewGroup).

    【讨论】:

      【解决方案2】:

      这可能不是您问题的完整答案,但我很可能向您展示了一个可以指导您的起点或指针:

      Sony Developer Tutorials - 3D ListView

      【讨论】:

        【解决方案3】:

        ListView 扩展 AbsListView 进而扩展 AdapterView<ListAdapter>。因此,如果您绝对必须从头开始实现这样的自定义视图,您可以查看这些类的源代码:

        但请注意,这是一项艰巨的任务。也许使用现有类之一并调整外观就足够了。

        【讨论】:

          【解决方案4】:

          AdapterView 派生可以工作,但它可能没有您希望的那么有益。 AdapterView 提供的一些基础设施是包私有的,这意味着我们无权访问它。

          例如,AdapterView 管理AbsListViewListView 的选定项索引。但是,因为像setNextSelectedPositionInt(int position)(这是设置mNextSelectedPosition 的唯一途径)这样的方法是包私有的,所以我们无法使用它们。 AbsListViewListView 可以找到它们,因为它们在同一个包中,但我们不能。

          (如果您深入了解AdapterView 源代码,您会发现setNextSelectedPositionInt() 是从handleDataChanged() 调用的。不幸的是,handleDataChanged() 也是包私有的,并且是_not_called 来自AdapterView 内的任何其他位置可以用来启用设置位置。)

          这意味着,如果您需要管理选定的职位,则需要在派生类中重新创建该基础架构(或者您需要从 ListViewAbsListView 派生......虽然我怀疑你'会遇到来自AbsListView 的类似问题)。这也意味着任何围绕项目选择的AdapterView 功能都可能无法完全运行。

          【讨论】:

            【解决方案5】:

            你可以创建这样的东西:

            public class SampleAdapter extends BaseAdapter {
            
            public SampleAdapter() {
              // Some constructor
            }
            
            public int getCount() {
              return count; // Could also be a constant. This indicates the # of times the getView gets invoked.
            }
            
            public Object getItem(int position) {
                return position; // Returns the position of the current item in the iteration
            }
            
            public long getItemId(int position) {
              return GridView.INVALID_ROW_ID;
            }
            
            public View getView(int position, View convertView, ViewGroup parent) {
              View view = null;
            
              view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.some_layout, null);
              view.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            view.setBackgroungColor(Color.RED);
            
              return view;
            }
            

            }

            这可以像这样调用:

            GridView sampleView = (GridView) linearLayout.findViewById(R.id.sample_layout);
            sampleView.setAdapter(new SampleAdapter());
            

            【讨论】:

            • 谢谢,但我的问题不是关于how create an adapter for a view,我的问题是关于how to create a custom view that it's content determined by an adapter?(这个观点叫做AdapterView
            • 这不是他@StudentStudent 所问的。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-15
            相关资源
            最近更新 更多