Provide Different Color to the Seperator in the Gr

2019-09-01 18:54发布

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).

1条回答
乱世女痞
2楼-- · 2019-09-01 19:16

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

查看更多
登录 后发表回答