在ListView中未调用Subclassed RelativeLayout onDraw()方法

时间:2014-03-25 18:06:06

标签: android android-layout android-listview

这里有一些关于我想要实现的目标的背景知识,以帮助我更清楚地提出问题......我正在创建一个导航抽屉,ListView中的每个项目都是如此看起来类似于以下内容:

ListItem

但是,我需要能够以编程方式将右侧边框(蓝色)的颜色更改为各种不同的颜色,因此在使用解决方案时,我决定扩展RelativeLayout和在onDraw(Canvas c);方法中绘制线条。我的RelativeLayout代码如下:

public class CustomRelativeLayout extends RelativeLayout {

    private final Paint paint = new Paint();

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

        //Other Constructors

    private void init() {
        setPaintColor(Color.argb(128, 0, 0, 0));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(getMeasuredWidth() - 1, 0, getMeasuredWidth() - 1, getMeasuredHeight(), paint);
    }

    public void setPaintColor(int color){
        paint.setColor(color);
        invalidate();
    }
}

我的NavigationDrawer&#39; s ListView也包含使用此类的标头,它可以作为标题视图正常工作。但是,对于每个单独的ListView项,边框都不存在。我已经调试了我的解决方案,发现我的子类RelativeLayout onDraw(Canvas c);方法被调用用于标题视图,但是并没有为每个{{1}调用ListView ArrayAdapter<String>方法。 1}}我的View提供的子视图。

我知道还有其他方法可以解决这个问题,比如使用默认的CustomRelativeLayout,将背景设置为我想要的颜色,然后将其调整到右边 - 但是这样做了不是我的问题。我的问题是为什么我的onDraw(Canvas c); ListView方法被调用CustomArrayAdapter的标题视图,而不是我的适配器提供的每个视图?任何洞察这种行为将不胜感激。此外,以下是nav_drawer_item.xmlListViewpublic class SimpleDrawerAdapter extends ArrayAdapter<String> { public SimpleDrawerAdapter(Context context, int resource, String[] sections) { super(context, resource, sections); } @Override public View getView(int position, View convertView, ViewGroup parent) { RelativeLayout container = null; if(convertView == null){ LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); container = (CustomRelativeLayout) inflater.inflate(R.layout.nav_drawer_item, parent, false); } else { container = (CustomRelativeLayout) convertView; } ((TextView)container.findViewById(R.id.nav_item_text)).setText(getItem(position)); return container; } } 一起使用,以防他们提供帮助:

<?xml version="1.0" encoding="utf-8"?>
<com.mypackage.views.CustomRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/nav_item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/nav_drawer_grey"
        android:textSize="@dimen/text_large"
        android:layout_margin="@dimen/navigation_drawer_item_margin"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

</com.mypackage.views.CustomRelativeLayout>

nav_drawer_item.xml

{{1}}

2 个答案:

答案 0 :(得分:11)

您是否尝试在自定义布局中调用setWillNotDraw方法清除WILL_NOT_DRAW标记?

  

如果此视图本身不进行任何绘制,请将此标志设置为allow   进一步优化。默认情况下,此标志未在View上设置,但是   可以在某些View子类(如ViewGroup)上设置。通常,如果   你重写onDraw(android.graphics.Canvas)你应该清除它   标志。

答案 1 :(得分:-1)

每次遍历列表时都自己调用

   if(convertView == null){
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                container = (CustomRelativeLayout) inflater.inflate(R.layout.nav_drawer_item,
parent, false);


if(container!=null){
container.draw()
}


            } else {
                container = (CustomRelativeLayout) convertView;

if(container!=null){
container.draw()
}
        }