java - 我如何在其他 View 之后绘制某些 View ?

标签 java android android-layout android-custom-view

我有一个简单的 LinearLayout,它只包含一个扩展 TextView 的自定义 View (我将开始调用“IdiomView”)和一个 ListView IdiomView 与普通 TextView 的唯一区别是我重写了 onDraw() 方法以迭代减小文本大小直到文本少于 3 行。我的问题是当绘制 View 时,用户会看到:

 ______________
|__ACTION_BAR__|
|  IdiomView   |
|______________|
|              |
|   ListView   |
|              |
|              |
|______________|

很快变成:

 ______________
|__ACTION_BAR__|
|__IdiomView __|
|              |
|   ListView   |
|              |
|              |
|              |
|______________|

即ListView 被绘制,然后在 IdiomView 整理出它的大小后跳起来。

我想要的是在绘制 ListView 之前等待我的 IdiomView 完全绘制的方法。本帖What event is fired after all views are fully drawn?解释了如何在绘图完成后通过调用 View.post(Runnable) 来排列线程。问题是我重写的 onDraw() 方法多次调用 onDraw() 以计算较小的文本是否覆盖少于 3 行,所以这个元素可能“完成在我希望 ListView 出现之前多次绘制”。

我感谢所有的评论和回答。这是我当前的代码:

布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/off_white"
    android:orientation="vertical" >

    <carter.cwords.idioms.IdiomView
        android:id="@+id/idiom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:textColor="@color/transparent"
        android:textSize="28sp"
        android:textStyle="italic"
        android:visibility="invisible" />

    <ListView
        android:id="@+id/quote_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="none"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:visibility="invisible" />

</LinearLayout>

Activity :

private IdiomView mIdiomTextView;
private ListView mQuoteList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.idiom_of_the_day);
    mIdiomTextView = (IdiomView) findViewById(R.id.idiom);
    mQuoteList = (ListView) findViewById(R.id.quote_list);      

    // Populate page data onResume()
}

@Override
protected void onResume() {
    super.onResume();

    sendRequest(R.string.url_idiom_of_the_day, new AfterRequest(){
        @Override
        public void useResults(Document resultXml) {
            if(resultXml != null){

                Log.i(getClass().getSimpleName(), "useResults()");

                String idiomString = XmlUtilities.getTextValue(resultXml, NetworkHelper.XML_TAG_IDIOM_CONTENT);

                logDebug("idiomString: " + idiomString);
                mIdiomTextView.setText("\"" + idiomString + "\"");
                mQuoteList.setAdapter(new ContentAdapter(mContext, resultXml));
                mIdiomTextView.setVisibility(View.VISIBLE);

                mIdiomTextView.post(new Runnable(){
                    @Override
                    public void run() {
                        mQuoteList.setVisibility(View.VISIBLE);
                    }
                });
            }

        }
    });

}

IdiomView:

public class IdiomView extends TextView {

    public IdiomView(Context context) {     
        super(context);
    }

    public IdiomView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(getClass().getSimpleName(), "onDraw(): " + this.getLineCount());
        if(this.getLineCount() > 2){
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.getTextSize()-1);
        }
        else{
            this.setTextColor(getResources().getColor(R.color.text));
        }
        Log.i(getClass().getSimpleName(), "onDraw(): " + this.getLineCount());

    }


}

非常感谢。

最佳答案

我以前从未在 View 上尝试过此操作,但我认为它可以帮助指导您的应用仅在 IdiomView 之后使用 ListView 被绘制。您可以使用监听器来实现这一点。但是,它需要您使用代码隐藏方法。

IdiomView中创建界面如

public static interface OnDrawComplete {
    public abstract void onDrawComplete();
}

private OnDrawComplete mListener;

public IdiomView(Context context) {     
    super(context);
    // this forces it to make sure it's actually an activity
    // context so you can get a listener
    if (context instanceof Activity)
        this.mListener = context;
}

现在在你的绘图结束时,调用它

if (this.mListener != null)
    this.mListener.onDrawComplete();

这将调用创建该对象的 Activity 并通知它正在绘制。所以在你的 Activity 中,你实现了接口(interface)

public void onDrawComplete() {
    // After the draw completes, it calls this callback.
    // setup the rest of your layout now
    mQuoteList = (ListView) findViewById(R.id.quote_list);
    this.populateQuoteList();
}

您也可以在某种其他 View 事件中调用 this.mListener.onDrawComplete();,但我找不到合适的.你提到了 View.post(runnable),它可能有用,但我也从未使用过那种东西。

最大的潜在问题是我不知道 IdiomView 将如何接收上下文或接收哪个上下文。在这种情况下,您需要实现接口(interface)的 Activity 上下文。

如果它没有获得正确的上下文,可能需要手动实例化并将其添加到布局中才能使用此方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mIdiomTextView = new mIdiomTextView(this);
    // add to the Layout manually.  
}

这里有更多内容

IdiomView

public class IdiomView extends TextView {
    public static interface OnDrawComplete {
        public abstract void onDrawComplete();
    }

    private OnDrawComplete mListener;

    public IdiomView(Context context) {     
        this(context, 0);
    }

    public IdiomView(Context context, AttributeSet attrs){
        super(context, attrs);
        if (context instanceof Activity)
            this.mListener = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // do your drawing code here
        // ...
        // after drawing, call the listener, which tells your
        // Activity to deal with the ListView.
        if (this.mListener != null)
            this.mListener.onDrawComplete();
    }
}

Activity

public class MyActivity extends Activity implements IdiomView.OnDrawComplete {
    //...

    private IdiomView mIdiomTextView;
    private ListView mQuoteList;

    public void onDrawComplete() {
        // After the draw completes, it calls this callback.
        // setup the rest of your layout now
        mQuoteList = (ListView) findViewById(R.id.quote_list);
        this.populateQuoteList();
        // now show the ListView
        mQuoteList.setVisibility(View.Visible);
    }

    private void populateQuoteList() {
        // populate your ListView here
    }
}

关于java - 我如何在其他 View 之后绘制某些 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14989496/

相关文章:

java - 当使用并行流时,归约行为很奇怪,但对于 Java 8u5 中的顺序流却工作得很好

java - 如何使用 easymock Capture 进行测试

android - 创建蜂窝操作栏选项卡后更改选项卡文本

android - 如何使用 Http POST 请求发送表情符号

android - 自动填充框架更新 8.1 导致 EditText 崩溃

java - Liquibase:使用序列

Java 数独解算器不更改空单元格

android - Firebase 真实数据库 - 查询多个标签(可能在数组中)

android - 带有 AppBarLayout 和工具栏的新 Android 设计库错误

android - 如何使用 Mapview 设置布局