代码之家  ›  专栏  ›  技术社区  ›  Michael Winther

创建Android自定义视图,而不是使用不同数据的多个视图

  •  1
  • Michael Winther  · 技术社区  · 6 年前

    我在Java类中创建了5种不同的视图。它们由构造器、所有构造器调用的init方法和绘制图形的ondraw方法组成。

    在每个视图中,init方法和ondraw都加载了不同的数据,因为图形不同。但init和ondraw基本相同。

    我可以创建一个可以重用的自定义视图吗?

    在视图中,每个视图都有一个文本视图。TeXVIEW也非常相似,除了在Java类中加载到它的数据之外,还可以创建自定义 文本视图,由自定义视图使用?我看到的所有示例都使用自定义的textview,在加载数据时没有任何区别,我必须确定调用的是哪个视图, 以便加载正确的数据。

    以下是布局的示例(其中包括一个2个视图以简化,还清除了不必要的布局特定内容):

    <TableRow
        android:layout_width="match_parent" >
        <com.company.views.FirstView
            android:id="@+id/firstView"
            android:layout_height="match_parent" />
        <com.company.views.FirstTextView
            android:id="@+id/firstTextView"
            android:layout_height="match_parent"/>
    </TableRow>
    <TableRow
        android:layout_width="match_parent" >
        <com.company.views.SecondView
            android:id="@+id/secondView"
            android:layout_height="match_parent" />
        <com.company.views.SecondTextView
            android:id="@+id/secondTextView"
            android:layout_height="match_parent"/>
    </TableRow>
    

    以下是其中一个视图的示例(也被刮掉):

    public class FirstView extends View {
      Paint paint;
      int y = 0;
    
      public FirstView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
      }
    
      private void init(Context context, AttributeSet attrs) {
            paint = com.company.helpers.Drawing.LineStyle("#00FF00", screenResolution.lineThickness); // <- the color is one of the differences for each view
      }
    
      @Override
      protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        y=GraphType.SpecificGraphType; // <- this is one of the other differences for each view
    
        // drawing part is irrelevant
    
        invalidate();
      }
    }
    

    以下是其中一个文本视图的示例:

    public class FirstTextView extends AppCompatTextView {
        private string defaultText;
    
        public FirstTextView(Context context) {
            super(context);
            init(null, 0);
        }
    
        public FirstTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs, 0);
        }
    
        public FirstTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(attrs, defStyle);
        }
    
        private void init(AttributeSet attrs, int defStyle) {
            textView = (TextView)findViewById(R.id.firstTextView);
            defaultText=Model.getInstance().SpecificGraphType; // <- this is one of the differences for each textview
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Michael Winther    6 年前

    发现它的工作很正常,但是我在setText()的重写中犯了一个minnor代码错误。所以如果有人想分享一个观点,你可以看看我上面的例子。

        2
  •  1
  •   Hassan Ata Ullah    6 年前

    由于自定义视图扩展了textview,因此不需要执行findView byid。对象本身就是视图。

    因此,您只需在init()中调用settext(“even awesomer”);

    您的代码会中断,因为findViewByID返回空值,然后您会得到一个NPE,尝试对此调用settext()。

    要区分视图中的视图,只需使用getid()。

    private void init(AttributeSet attrs, int defStyle) {
    if (getId() == R.id.firstTextView) {
        ...
    } else if (getId() == R.id.secondTextView) {
        ...
    }
    ...
    }