Android历史搜索和热门标签

前言

搜索界面一直是一个APP至关重要的部分,也是用户用的最多的界面,那么历史搜索和热门标签的话,也是这个界面所需要的重要的组成部分。
本篇文章旨在帮助大家如何写好两个重要的部分。话不多说,先上图

界面

这里写图片描述

用到的控件和框架

1.Android ORM框架 GreenDao3.0

2.SearchView在ToolBar中的使用

3.FlowLayoutTag 标签控件(也是本人写的控件,具体在引用如下)

compile’com.daidingkang:flowlayouttag:1.0.0’

FlowLayoutTag主要代码

FlowLayout.class

/**
 * 自定义流式布局
 */
public class FlowLayout extends ViewGroup {
   

    private LayoutInflater mInflater;
    private boolean isColorful;

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

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

    public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mInflater = LayoutInflater.from(getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        // wrapContent
        int width = 0;
        int height = 0;

        // 记录每一行的宽和高
        int lineWidth = 0;
        int lineHeight = 0;

        // 得到内部元素的个数
        int count = getChildCount();

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            // 测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
            // 子view的占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            // 子view占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;
            // 换行
            if (lineWidth + childWidth > sizeWidth - getPaddingLeft()
                    - getPaddingRight()) {

                // 对比得到最大的宽度
                width = Math.max(width, lineWidth);
                // 重置lineWidth
                lineWidth = childWidth;
                // 记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else {
                // 未换行
                // 叠加行宽
                lineWidth += childWidth;
                // 得到当前最大高度
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 最后一个控件
            if (i == count - 1) {
                width = Math.max(lineWidth, width);
                height += lineHeight;
            }
        }
//        Log.i("test", "sizeWidth" + sizeWidth);
//        Log.i("test", "sizeHeight" + sizeHeight);

        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth
                        : width + getPaddingLeft() + getPaddingRight(),
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height
                        + getPaddingTop() + getPaddingBottom());
        setPadding(dp2px(20), dp2px(10), dp2px(20), dp2px(10));
    }

    // 储存所有的View
    private ArrayList<ArrayList<View>> mAllViews = new ArrayList<>();
    // 储存每一行的高度
    private ArrayList<Integer> mLineHeight = new ArrayList<>();

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

        // 清除一下list集合
        mAllViews.clear();
        mLineHeight.clear();

        // 得到viewGroup当前宽度
        int width = getWidth();

        
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在Android应用中实现搜索功能并显示历史记录,你可以按照以下步骤进行: 1. 创建一个搜索界面,包括一个搜索框和一个搜索按钮。 2. 使用Shared Preferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。 3. 在搜索界面中添加一个“搜索历史记录”按钮,当用户点击该按钮时,从SharedPreferences中读取搜索历史记录并显示在一个列表中。 4. 当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。 以下是一个简单的示例代码的实现: ```java public class SearchActivity extends AppCompatActivity { private EditText mSearchEditText; private Button mSearchButton; private Button mHistoryButton; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); mSharedPreferences = getPreferences(MODE_PRIVATE); mSearchEditText = findViewById(R.id.search_edit_text); mSearchButton = findViewById(R.id.search_button); mHistoryButton = findViewById(R.id.history_button); mSearchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String keyword = mSearchEditText.getText().toString(); addKeywordToHistory(keyword); // 执行搜索操作 } }); mHistoryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showHistoryList(); } }); } private void addKeywordToHistory(String keyword) { SharedPreferences.Editor editor = mSharedPreferences.edit(); Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); history.add(keyword); editor.putStringSet("history", history); editor.apply(); } private void showHistoryList() { Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); final String[] historyArray = history.toArray(new String[history.size()]); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("历史记录"); builder.setItems(historyArray, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { mSearchEditText.setText(historyArray[i]); // 执行搜索操作 } }); builder.show(); } } ``` 在这个示例代码中,我们使用SharedPreferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。当用户点击历史记录按钮时,从SharedPreferences中读取搜索历史记录并显示在一个列表中。当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值