【问题标题】:is it possible to set adapter to linear layout?是否可以将适配器设置为线性布局?
【发布时间】:2014-09-14 07:47:36
【问题描述】:

是否可以将适配器设置为LinearLayout

我不想使用ListView,因为我使用的是ScrollView。 所以我使用LinearLayout...

我正在动态添加视图,现在我需要使用适配器.. 所以有人有什么建议吗?

我正在这样做……

LinearLayout clientList = (LinearLayout) findViewById(R.id.clients);
adapter = new Sample1AdapterActivity(this, arrayList);
View list = (ListView) getLayoutInflater().inflate(R.layout.user_details, getListView(), false);

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    是的,您可以通过添加自己的特定 LinearLayout 实现来从适配器获取子视图。但是,我的基本实现不会提供列表视图提供的所有视图回收代码。

    /**
     * A linear layout that will contain views taken from an adapter. It differs
     * from the list view in the fact that it will not optimize anything and
     * draw all the views from the adapter. It also does not provide scrolling.
     * However, when you need a layout that will render views horizontally and
     * you know there are not many child views, this is a good option.
    
     *
     * @author Vincent Mimoun-Prat @ MarvinLabs
     */
    public class AdapterLinearLayout extends LinearLayout {
    
        private Adapter adapter;
        private DataSetObserver dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                reloadChildViews();
            }
        };
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public AdapterLinearLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setOrientation(LinearLayout.HORIZONTAL);
        }
    
        public AdapterLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOrientation(LinearLayout.HORIZONTAL);
        }
    
        public AdapterLinearLayout(Context context) {
            super(context);
            setOrientation(LinearLayout.HORIZONTAL);
        }
    
        public void setAdapter(Adapter adapter) {
            if (this.adapter == adapter) return;
            this.adapter = adapter;
            if (adapter != null) adapter.registerDataSetObserver(dataSetObserver);
            reloadChildViews();
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            if (adapter != null) adapter.unregisterDataSetObserver(dataSetObserver);
        }
    
        private void reloadChildViews() {
            removeAllViews();
    
            if (adapter == null) return;
    
            int count = adapter.getCount();
            for (int position = 0; position < count; ++position) {
                View v = adapter.getView(position, null, this);
                if (v != null) addView(v);
            }
    
            requestLayout();
        }
    }
    

    【讨论】:

    • 很好的答案!但是有一个问题——作为一个安卓新手,你能告诉我为什么 setOrientation(LinearLayout.HORIZONTAL);似乎覆盖了我在 XML 中提供的方向 VERTICAL?某种优先级?
    【解决方案2】:

    不,你不能。您可以做的是膨胀单行,并添加到 LinearLayout。在伪代码中:

      LinearLayout linearLayout = (LinearLayout) findViewById(...);
      LayoutInflater inflater = LayoutInflater.from(this);
      for (item in arrayList) {
         View view  = inflater.inflate(R.layout.row, linearLayout, false); 
         // set item content in view
         linearLayout.addView(view)
      }
    

    【讨论】:

    • 您可以从我上面的代码中的一些适配器细节中受益(例如,当适配器更新时布局刷新)。
    • 这绝对是可能的,但您还要付出编写适配器的努力。在自定义 getView 中,无论如何您都必须充气并填充布局。 +1 来自我@MarvinLabs
    • 它的工作....但我可以对它做一些进一步的操作吗?比如 onClick 事件处理等?
    • 是的,您可以在每个膨胀视图上调用 setOnClickListener(如果这是您的意思)
    • 但是如何将不同的项目设置为不同的行视图?那是来自“for(arrayList 中的项目)”,我该如何使用它?因为我在单行上有多个项目..即文本、图像、按钮...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多