2019July17_列表选中效果

DailyWorkSummary

一 需求

选中列表项,当前项有选中效果;选择其他项,已选择项恢复原先效果

二 效果图

2019July17_列表选中效果_第1张图片

三 原理

View的LayoutParam使用
注意获取View大小的时机

四 注意

  • Activity加载:onCreat进入 - setContentView- View的构造-onContentChange-onCreate出去-onResume-View的onSizeChanged
  • 在Activity的onCreate或onContentChange中请求或获取数据,最好放到View.post中去,这样代码执行在onResume之后,保证View已经加载了

五 主要代码

SelectedView

public class SelectedView extends LinearLayout {
    //保留可以滑动的拓展
    public int ITEM_WRAP = 0;
    private int mHeight, mWidth;
    private int mSelectedExpand = 10;
    private int mSelectedShader = 5;

    private int mHorizontalPadding = 10;
    private int mItemWidth;
    private List mList;
    public int mSeleted = -1;

    public SelectedView(Context context) {
        this(context, null);
    }

    public SelectedView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setBackgroundColor(Color.GREEN);
        mSelectedExpand = UtilsDensity.dip2px(3);
        mSelectedShader = UtilsDensity.dip2px(3);
        mItemWidth = UtilsDensity.dip2px(60);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight();
        mWidth = getWidth();
    }

    public void setItems(final List list) {

        if (list == null || list.isEmpty()) {
            removeAllViews();
            return;
        }
        mList = list;
        removeAllViews();
        getView(list);
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.setTag(i);
            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    int itemW;
                    View child;
                    if (switchSelected(pos)) {
                        for (int j = 0; j < mList.size(); j++) {
                            itemW = list.get(j).isSlected ? (mItemWidth + 2 * mSelectedExpand + 2 * mSelectedShader) : mItemWidth;
                            child = getChildAt(j);
                            LinearLayout.LayoutParams params = (LayoutParams) child.getLayoutParams();
                            params.width = itemW;
                            params.height = itemW;
                            params.rightMargin = 0;
                            params.leftMargin = 0;
                            int padding = mSelectedExpand + mSelectedShader;
                            if (j != mSeleted) {
                                if (j == 0) {
                                    params.rightMargin = padding;
                                } else if (j == list.size() - 1) {
                                    params.leftMargin = padding;
                                } else {
                                    params.rightMargin = padding;
                                    params.leftMargin = padding;
                                }
                            }
                            child.setLayoutParams(params);
                        }
                    }

                }
            });
        }
    }


    private boolean switchSelected(int pos) {
        if (mList == null || mList.isEmpty() || pos >= mList.size()) {
            return false;
        }
        boolean reusult = false;
        for (int i = 0; i < mList.size(); i++) {
            if (i == pos) {
                if (!mList.get(i).isSlected) {
                    mSeleted = i;
                    mList.get(i).isSlected = true;
                    reusult = true;
                }
            } else {
                mList.get(i).isSlected = false;
            }
        }

        return reusult;
    }


    private void getView(List list) {

        int itemW;
        for (int i = 0; i < list.size(); i++) {
            View itemView = getItemView();
            if (list.get(i).isSlected) {
                mSeleted = i;
            }
            itemW = list.get(i).isSlected ? (mItemWidth + 2 * mSelectedExpand + 2 * mSelectedShader) : mItemWidth;
            LayoutParams params = new LayoutParams(itemW, itemW);
            int padding = mSelectedExpand + mSelectedShader;
            if (i != mSeleted) {
                if (i == 0) {
                    params.rightMargin = padding;
                } else if (i == list.size() - 1) {
                    params.leftMargin = padding;
                } else {
                    params.rightMargin = padding;
                    params.leftMargin = padding;
                }
            }
            addView(itemView, params);
        }


    }

    public View getItemView() {
        View view = View.inflate(getContext(), R.layout.layout_select_view_item, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            view.setId(View.generateViewId());
        }
        return view;
    }


    public static class Item {
        public boolean isSlected = false;

        @Override
        public boolean equals(Object obj) {
            return super.equals(obj);
        }
    }
}

使用

public class SelectedViewActivity extends FragmentActivity {


    SelectedView sv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_view_activity);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        sv =findViewById(R.id.sv);
        sv.post(new Runnable() {
            @Override
            public void run() {
                List list = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    SelectedView.Item item = new SelectedView.Item();
                    if (i == 2) {
                        item.isSlected = true;
                    }
                    list.add(item);
                }
                sv.setItems(list);
            }
        });
    }


}

六 Demo

SelectedViewActivity

你可能感兴趣的:(2019July17_列表选中效果)