为GridView中的分隔符提供不同的颜色? [英] Provide Different Color to the Seperator in the GridView?

查看:101
本文介绍了为GridView中的分隔符提供不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有对其主要布局透明的GridView背景。列表项目应该有背景透明,但Seperator应该有不同的颜色。如何实现?

主容器(图片drawable是通过progamatically设置的)

 < *************。AutoGridView 
android:id =@ + id / dashMenu
android:layout_width =match_parent
android:layout_height =210dp
android:layout_alignParentBottom =true
android :background =@ android:color / transparent
android:horizo​​ntalSpacing =1dp
android:numColumns =@ integer / grid_columns
android:scrollbars =none
android:stretchMode =columnWidth
android:verticalSpacing =1dp
android:visibility =visible/>

商品

 <?xml version =1.0encoding =utf-8?> 

< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com / tools
android:layout_width =match_parent
android:layout_height =match_parent
android:orientation =vertical>

$ b< LinearLayout
android:id =@ + id / menuWrapper
android:layout_width =match_parent
android:layout_height = match_parent
android:gravity =center
android:orientation =vertical
android:padding =12dp
tools:ignore =UseCompoundDrawables>

< ImageView
android:id =@ + id / menuImg
android:layout_width =58dp
android:layout_height =58dp
android:padding =8dp
android:src =@ mipmap / ic_launcher/>

TextView
android:id =@ + id / menuLabel
android:layout_width =match_parent
android:layout_height =wrap_content
android:fontFamily =@ font / montserrat_regular
android:gravity =center_horizo​​ntal
android:paddingBottom =6dp
android:text =Menu Item
android:textColor =@ color / white
android:textSize =12sp/>


< / LinearLayout>


< / RelativeLayout>

适配器

  public class HomeMenuAdapter extends BaseAdapter {

private Context mContext;
private ArrayList< Menu>菜单;

public HomeMenuAdapter(Context context,ArrayList< Menu> menus){
mContext = context;
this.menus =菜单;
}

@Override
public int getCount(){
return menus.size();
}

@Override
public Menu getItem(int position){
return menus.get(position);
}

@Override
public long getItemId(int position){
return 0;
}

@Override
public View getView(int position,View convertView,ViewGroup parent){
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Menu menu = menus.get(position);

ImageView menuImage;
TextView menuLabel;

查看v;

if(convertView == null){
v = layoutInflater.inflate(R.layout.dash_menu_item,null);
} else {
v = convertView;
}

/ * int columns = 3;
int total = menus.size();
int rows = total / columns;

LinearLayout menuWrapper = v.findViewById(R.id.menuWrapper); (((位置+ 1)%列)== 0){
if((total - position)> columns){
CommonUtils.setDrawableBackground(mContext,menuWrapper, drawable.menu_border_bottom);
} else {
CommonUtils.setDrawableBackground(mContext,menuWrapper,R.drawable.menu_border_neutral);
}
} else if((total - position)> columns){
CommonUtils.setDrawableBackground(mContext,menuWrapper,R.drawable.menu_border_right_bottom);
} else {
CommonUtils.setDrawableBackground(mContext,menuWrapper,R.drawable.menu_border_right);
}
* /


menuImage = v.findViewById(R.id.menuImg);
menuLabel = v.findViewById(R.id.menuLabel); (menu.getIcon()!= null&&!TextUtils.isEmpty(menu.getIcon())&& $ amp;
URLUtil.isValidUrl(menu.getIcon() )){
RequestOptions requestOptions = new RequestOptions();
requestOptions.error(menu.getIconId());
Glide.with(menuImage).applyDefaultRequestOptions(requestOptions)
.load(menu.getIcon())。into(menuImage);

} else if(menu.getIconId()!= 0){
setImage(menuImage,menu.getIconId());
}
menuLabel.setText(menu.getName());

v.setOnClickListener(new OnOneClickListener(){
@Override
public void onOneClick(View v){
new Router(mContext).route(menu);
}
});

return v;


private void setImage(ImageView menuImage,int iconId){
menuImage.setImageDrawable(mContext.getResources()。getDrawable(
iconId));
}
}

AutoGridView

  public class AutoGridView extends GridView {

private static final String TAG =AutoGridView;
private int numColumnsID;
private int previousFirstVisible;
private int numColumns = 1;

public AutoGridView(Context context,AttributeSet attrs,int defStyle){
super(context,attrs,defStyle);
init(attrs);


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

public AutoGridView(Context context){
super(context);

$ b $ ** b $ b *根据属性集
* /
设置numColumns private void init(AttributeSet attrs){
//从AttributeSet中读取numColumns
int count = attrs.getAttributeCount();
if(count> 0){
for(int i = 0; i< count; i ++){
String name = attrs.getAttributeName(i);
if(name!= null&& name.equals(numColumns)){
//更新列
this.numColumnsID = attrs.getAttributeResourceValue(i,1);
updateColumns();
休息;
}
}
}
Log.d(TAG,numColumns设置为:+ numColumns);


$ b / **
*从资源文件中读取列的数量,
*更新numColumns变量
* /
private void updateColumns(){
this.numColumns = getContext()。getResources()。getInteger(numColumnsID);
}

@Override
public void setNumColumns(int numColumns){
this.numColumns = numColumns;
super.setNumColumns(numColumns);

Log.d(TAG,setSelection - >+ previousFirstVisible);
setSelection(previousFirstVisible);

$ b $覆盖
protected void onLayout(boolean changed,int leftPos,int topPos,int rightPos,int bottomPos){
super.onLayout(changed,leftPos ,topPos,rightPos,bottomPos);
setHeights();


@Override
protected void onConfigurationChanged(Configuration newConfig){
updateColumns();
setNumColumns(this.numColumns);

$ b @Override
保护void onScrollChanged(int newHorizo​​ntal,int newVertical,int oldHorizo​​ntal,int oldVertical){
//检查第一个可见位置是否已经改变由于这个滚动
int firstVisible = getFirstVisiblePosition();
if(previousFirstVisible!= firstVisible){
//更新位置并更新高度
previousFirstVisible = firstVisible;
setHeights();
}

super.onScrollChanged(newHorizo​​ntal,newVertical,oldHorizo​​ntal,oldVertical);
}

/ **
*设置一行中每个视图的高度等于此行中最高视图的高度。
* /
private void setHeights(){
ListAdapter adapter = getAdapter();

if(adapter!= null){
for(int i = 0; i< getChildCount(); i + = numColumns){
//确定最大高度这一行
int maxHeight = 0;
for(int j = i; j< i + numColumns; j ++){
View view = getChildAt(j);
if(view!= null&& view; getHeight()> maxHeight){
maxHeight = view.getHeight();


//Log.d(TAG,最大行高度#+ i / numColumns +:+ maxHeight);

//设置该行中每个元素的最大高度
if(maxHeight> 0){
for(int j = i; j< i + numColumns; j ++ ){
View view = getChildAt(j);
if(view!= null&&& view.getHeight()!= maxHeight){
view.setMinimumHeight(maxHeight);
}
}
}
}
}
}


}




不建议添加额外的视图(请)。



解决方案

请参阅



你必须根据你的愿望改变项目装饰颜色


I have the background of the GridView which is Transparent to its main layout. The List Item Should also have the background transparent , but the Seperator should have different Color .How can this be Achieved?

Main Container(image drawable is setted through progamatically)

    <*************.AutoGridView
        android:id="@+id/dashMenu"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/transparent"
        android:horizontalSpacing="1dp"
        android:numColumns="@integer/grid_columns"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"
        android:visibility="visible" />

items

  <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/menuWrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="12dp"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:id="@+id/menuImg"
            android:layout_width="58dp"
            android:layout_height="58dp"
            android:padding="8dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/menuLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat_regular"
            android:gravity="center_horizontal"
            android:paddingBottom="6dp"
            android:text="Menu Item"
            android:textColor="@color/white"
            android:textSize="12sp" />


    </LinearLayout>


</RelativeLayout>

Adapter

public class HomeMenuAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Menu> menus;

    public HomeMenuAdapter(Context context, ArrayList<Menu> menus) {
        mContext = context;
        this.menus = menus;
    }

    @Override
    public int getCount() {
        return menus.size();
    }

    @Override
    public Menu getItem(int position) {
        return menus.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        Menu menu = menus.get(position);

        ImageView menuImage;
        TextView menuLabel;

        View v;

        if (convertView == null) {
            v = layoutInflater.inflate(R.layout.dash_menu_item, null);
        } else {
            v = convertView;
        }

        /*int columns = 3;
        int total = menus.size();
        int rows = total / columns;

        LinearLayout menuWrapper = v.findViewById(R.id.menuWrapper);
        if (((position + 1) % columns) == 0) {
            if((total - position) > columns ) {
                CommonUtils.setDrawableBackground(mContext, menuWrapper, R.drawable.menu_border_bottom);
            } else {
                CommonUtils.setDrawableBackground(mContext, menuWrapper, R.drawable.menu_border_neutral);
            }
        } else if((total - position) > columns ) {
                CommonUtils.setDrawableBackground(mContext, menuWrapper, R.drawable.menu_border_right_bottom);
        } else {
            CommonUtils.setDrawableBackground(mContext, menuWrapper, R.drawable.menu_border_right);
        }
*/


        menuImage = v.findViewById(R.id.menuImg);
        menuLabel = v.findViewById(R.id.menuLabel);

        if(menu.getIcon() != null && !TextUtils.isEmpty(menu.getIcon()) &&
                URLUtil.isValidUrl(menu.getIcon())) {
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.error(menu.getIconId());
            Glide.with(menuImage).applyDefaultRequestOptions(requestOptions)
                    .load(menu.getIcon()).into(menuImage);

        } else if(menu.getIconId() != 0) {
            setImage(menuImage, menu.getIconId());
        }
        menuLabel.setText(menu.getName());

        v.setOnClickListener(new OnOneClickListener() {
            @Override
            public void onOneClick(View v) {
                new Router(mContext).route(menu);
            }
        });

        return v;
    }

    private void setImage(ImageView menuImage, int iconId) {
        menuImage.setImageDrawable(mContext.getResources().getDrawable(
                iconId));
    }
}

AutoGridView

public class AutoGridView extends GridView {

    private static final String TAG = "AutoGridView";
    private int numColumnsID;
    private int previousFirstVisible;
    private int numColumns = 1;

    public AutoGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

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

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

    /**
     * Sets the numColumns based on the attributeset
     */
    private void init(AttributeSet attrs) {
        // Read numColumns out of the AttributeSet
        int count = attrs.getAttributeCount();
        if(count > 0) {
            for(int i = 0; i < count; i++) {
                String name = attrs.getAttributeName(i);
                if(name != null && name.equals("numColumns")) {
                    // Update columns
                    this.numColumnsID = attrs.getAttributeResourceValue(i, 1);
                    updateColumns();
                    break;
                }
            }
        }
        Log.d(TAG, "numColumns set to: " + numColumns);
    }


    /**
     * Reads the amount of columns from the resource file and
     * updates the "numColumns" variable
     */
    private void updateColumns() {
        this.numColumns = getContext().getResources().getInteger(numColumnsID);
    }

    @Override
    public void setNumColumns(int numColumns) {
        this.numColumns = numColumns;
        super.setNumColumns(numColumns);

        Log.d(TAG, "setSelection --> " + previousFirstVisible);
        setSelection(previousFirstVisible);
    }

    @Override
    protected void onLayout(boolean changed, int leftPos, int topPos, int rightPos, int bottomPos) {
        super.onLayout(changed, leftPos, topPos, rightPos, bottomPos);
        setHeights();
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        updateColumns();
        setNumColumns(this.numColumns);
    }

    @Override
    protected void onScrollChanged(int newHorizontal, int newVertical, int oldHorizontal, int oldVertical) {
        // Check if the first visible position has changed due to this scroll
        int firstVisible = getFirstVisiblePosition();
        if(previousFirstVisible != firstVisible) {
            // Update position, and update heights
            previousFirstVisible = firstVisible;
            setHeights();
        }

        super.onScrollChanged(newHorizontal, newVertical, oldHorizontal, oldVertical);
    }

    /**
     * Sets the height of each view in a row equal to the height of the tallest view in this row.
     */
    private void setHeights() {
        ListAdapter adapter = getAdapter();

        if(adapter != null) {
            for(int i = 0; i < getChildCount(); i+=numColumns) {
                // Determine the maximum height for this row
                int maxHeight = 0;
                for(int j = i; j < i+numColumns; j++) {
                    View view = getChildAt(j);
                    if(view != null && view.getHeight() > maxHeight) {
                        maxHeight = view.getHeight();
                    }
                }
                //Log.d(TAG, "Max height for row #" + i/numColumns + ": " + maxHeight);

                // Set max height for each element in this row
                if(maxHeight > 0) {
                    for(int j = i; j < i+numColumns; j++) {
                        View view = getChildAt(j);
                        if(view != null && view.getHeight() != maxHeight) {
                            view.setMinimumHeight(maxHeight);
                        }
                    }
                }
            }
        }
    }


}

Do not Suggest adding extra view(please).

解决方案

Please refer to

How can a divider line be added in an Android RecyclerView?

You have to change the item decorator color as u desire

这篇关于为GridView中的分隔符提供不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆