android搜索欄 截圖 流式佈局FlowLayout

截圖

流式佈局FlowLayout

package com.yds.jianshulib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yds
 * on 2020/3/30.
 */
public class FlowLayout extends ViewGroup {
    private List<List<View>> mAllViews = new ArrayList<>();
    private List<Integer> mLineHeightList = new ArrayList<>();

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

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.d("onLayout-Time:","onLayout");
        mAllViews.clear();
        mLineHeightList.clear();
        int width = getWidth();
        int lineWidth = 0;
        int lineHeight = 0;
        List<View> lineViews = new ArrayList<>();
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams mlp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (childWidth + mlp.leftMargin + mlp.rightMargin + lineWidth > width) {
                mLineHeightList.add(lineHeight);
                mAllViews.add(lineViews);
                lineWidth = 0;
                lineViews = new ArrayList<>();
            }
            lineWidth+=childWidth+mlp.leftMargin+mlp.rightMargin;
            lineHeight=Math.max(lineHeight,childHeight+mlp.topMargin+mlp.bottomMargin);
            lineViews.add(child);
        }
        mLineHeightList.add(lineHeight);
        mAllViews.add(lineViews);

        int left = 0;
        int top = 0;
        int lineNums = mAllViews.size();
        for (int i=0;i<lineNums;i++){
            lineViews = mAllViews.get(i);
            lineHeight = mLineHeightList.get(i);
            for (int j=0;j<lineViews.size();j++){
                View child = lineViews.get(j);
                if (child.getVisibility()==View.GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int lc = left+lp.leftMargin;
                int tc = top+lp.topMargin;
                int rc = lc+child.getMeasuredWidth();
                int bc = tc+child.getMeasuredHeight();

                child.layout(lc,tc,rc,bc);

                left+=child.getMeasuredWidth()+lp.rightMargin+lp.leftMargin;
            }
            left = 0;
            top+=lineHeight;
        }

    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d("onMeasure-Time:","onMeasure");
        //獲取總寬度(父容器爲它設置的測量值)
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        //獲取總高度(父容器爲它設置的測量值)
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        //獲取父容器爲它設置的測量模式
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //獲取父容器爲它設置的測量模式
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //如果是wrap_content情況,記錄寬和高
        int width = 0;
        int height = 0;

        //記錄每一行的寬度,width不斷獲取最大寬度
        int lineWidth = 0;
        //每一行的高度,累加至height
        int lineHeight = 0;
        int count = getChildCount();
        Log.d("onMeasure-Time:","count="+count);
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            //測量每一個child的寬和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到child的lp
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            //當前子控件實際佔據的寬度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //當前子控件實際佔據的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //如果加入當前child超出了最大寬度,則將得到的目前最大寬度給width,累加height,然後開啓新行
            if (lineWidth + childWidth > sizeWidth) {
                width = Math.max(lineWidth, childWidth);//取最大
                lineWidth = childWidth;//重新開啓新行,開始記錄
                height += lineHeight;
                lineHeight = childHeight;
            } else {//否則累加到lineWidth,lineHeigth取最大高度
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //如果是最後一個,則將當前記錄的最大寬度和當前的lineWidth做比較
            if (i == count - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width,
                (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
    }
}

  • main_module_search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/search_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/search_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <ImageView
            android:id="@+id/back"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/f_back"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/searc_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/shape_search"
            android:hint="搜索文章、專題、用戶、文集"
            android:inputType="text"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingRight="50dp"
            android:paddingBottom="10dp"
            android:singleLine="true"
            android:textColor="#A2A2A2"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/back"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:layout_width="1dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@color/f_line_gray"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/search_img"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/search_img"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginRight="20dp"
            android:src="@drawable/f_search"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/f_line_gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingStart="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/search_history" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:text="歷史記錄"
            android:textColor="@color/f_black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="10dp"
            android:text="清除歷史"
            android:textAlignment="textEnd"
            android:textColor="#A2A2A2" />
    </LinearLayout>

    <com.yds.jianshulib.widget.FlowLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="5dp">

        <TextView
            style="@style/search_flow_layout_text"
            android:text="welcome" />

        <TextView
            style="@style/search_flow_layout_text"
            android:text="測試" />

        <TextView
            style="@style/search_flow_layout_text"
            android:text="查找ViewHolder" />

        <TextView
            style="@style/search_flow_layout_text"
            android:text="快速查找" />

        <TextView
            style="@style/search_flow_layout_text"
            android:text="線程" />

        <TextView
            style="@style/search_flow_layout_text"
            android:text="多線程" />
    </com.yds.jianshulib.widget.FlowLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#EBE8E8" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/search_populer"
            android:layout_marginLeft="10dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/f_black"
            android:layout_marginLeft="10dp"
            android:text="搜索趨勢"/>
    </LinearLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/search_rank_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp">

    </androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
  • recycler_adapter_item_search_rank.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/rank_index"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            tools:text="1"
            android:layout_gravity="center"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/search_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="@color/f_black"
            android:textSize="16sp"
            tools:text="超級計算機與快速治療試驗,能否阻遏新冠病毒的傳播速度?"
            android:layout_gravity="center"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.8dp"
        android:background="@color/f_line_gray"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />
</LinearLayout>
  • SearchRankAdapter
package com.yds.mainmodule.adapter;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yds.mainmodule.R;
import com.yds.mainmodule.bo.SearchRankBO;


import java.util.List;

/**
 * Created by yds
 * on 2020/4/1.
 */
public class SearchRankAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<SearchRankBO> mSearchDataList;

    public SearchRankAdapter(Context mContext, List<SearchRankBO> searchList) {
        this.mContext = mContext;
        this.mSearchDataList = searchList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_adapter_item_search_rank, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.rankIndex.setText(mSearchDataList.get(position).getRankIndex());
        String seachText = mSearchDataList.get(position).getSearchText();
        String readTip = "閱讀 " + mSearchDataList.get(position).getReadNum();
        SpannableStringBuilder searchContent = new SpannableStringBuilder(seachText + readTip);
        ForegroundColorSpan span = new ForegroundColorSpan(mContext.getResources().getColor(R.color.f_text_gray));
        searchContent.setSpan(span,seachText.length(),seachText.length()+readTip.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(14,true);
        searchContent.setSpan(sizeSpan,seachText.length(),seachText.length()+readTip.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.searchText.setText(searchContent);
    }

    @Override
    public int getItemCount() {
        return mSearchDataList.size();
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView rankIndex;
        private TextView searchText;

        private MyViewHolder(@NonNull View itemView) {
            super(itemView);
            rankIndex = itemView.findViewById(R.id.rank_index);
            searchText = itemView.findViewById(R.id.search_text);
        }
    }
}

源碼地址:https://github.com/ydslib/Jianshu/tree/develop

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章