【基于Java语言的Androd个人开发笔记,个人工具类笔记】常见的首页搜索框和搜索结果

基于上一篇的五栏引导碎片的基础,主要编写FragmnrtA部分
碎片中首页部分
当内容部分过长的时候,我们需要下面这个东西使页面支持滚动

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingConstraints">
        
    </androidx.core.widget.NestedScrollView>

xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/backgrongd"
    android:focusableInTouchMode="true"
    tools:context=".freament.AFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingConstraints">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/edit_home"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="9"
                android:hint="搜索一下!"
                android:imeOptions="actionSearch"
                android:singleLine="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.youth.banner.Banner
                android:id="@+id/banner_home"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/edit_home"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/txt1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFCDD2"
                android:textColor="#E57373"
                android:text="A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/banner_home"
                tools:ignore="MissingConstraints" />

            <GridView
                android:id="@+id/gv_serve_home"
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:numColumns="5"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/txt1"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/txt2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFCDD2"
                android:textColor="#E57373"
                android:text="B"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/gv_serve_home"
                tools:ignore="MissingConstraints" />

            <GridView
                android:id="@+id/gv_hot_home"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:numColumns="2"
                android:padding="2dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/txt2"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/txt3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFCDD2"
                android:textColor="#E57373"
                android:text="C"
                app:layout_constraintEnd_toEndOf="@+id/gv_new_home"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/gv_hot_home"
                tools:ignore="MissingConstraints" />

            <RadioGroup
                android:id="@+id/radio_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/txt3">

                <RadioButton
                    android:checked="true"
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_22"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="D" />

                <RadioButton
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_9"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="E" />

                <RadioButton
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_17"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="F" />

                <RadioButton
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_19"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="G" />

                <RadioButton
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_20"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="H" />

                <RadioButton
                    android:textColor="@drawable/text_selector"
                    android:id="@+id/type_21"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/kuang_selector"
                    android:button="@null"
                    android:text="I" />
            </RadioGroup>

            <GridView
                android:id="@+id/gv_new_home"
                android:layout_width="match_parent"
                android:layout_height="1200dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/radio_group" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</FrameLayout>

搜索功能

    /**
     * 搜索
     */
    private void initSearch() {
        edit_home.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_SEARCH) && event != null) {
                    String getin = edit_home.getText().toString().trim();
                    Intent intent = new Intent(getContext(), SearchActivity.class);
                    intent.putExtra("输入框内容", getin);
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });
    }

搜索功能需要一个Activity展示搜索列表
搜索列表的XML文件

    <GridView
        android:id="@+id/gv_show_new"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

自定义类别列表内容部分,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="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/iv_item_new"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/ic_launcher_background"
            android:scaleType="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvTitle_item_new"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="2dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="未搜索到相关内容"
                android:textSize="22sp" />

            <TextView
                android:id="@+id/tvTime_item_new"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:singleLine="true"
                android:ellipsize="end"
                android:text="发布时间"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/tvCon_itme_new"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:ellipsize="end"
                android:text="暂无搜索内容"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/tvLike_item_new"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="点赞人数"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="12sp" />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

搜索列表的Java文件


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("搜索结果");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        String string = intent.getStringExtra("输入框内容");
        initVolley(string);
    }

    /**
     * 展示搜索内容
     */
    private void initVolley(String string) {
        JSONObject jsonObject = new JSONObject();
        RequestQueue requestQueue = Volley.newRequestQueue(SearchActivity.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(final JSONObject response) {
                try {
                    final int num = Integer.parseInt(response.getString("total"));
                    List<Integer> list = new ArrayList<>();
                    for (int i = 0; i < num; i++) {
                        list.add(response.getJSONArray("rows").getJSONObject(i).getInt("id"));
                    }
                    initBtn(list);
                    gvShowNew.setAdapter(new BaseAdapter() {
                        private void initView(View news) {
                            ivItemNew = (ImageView) news.findViewById(R.id.iv_item_new);
                            tvTitleItemNew = (TextView) news.findViewById(R.id.tvTitle_item_new);
                            tvTimeItemNew = (TextView) news.findViewById(R.id.tvTime_item_new);
                            tvConItmeNew = (TextView) news.findViewById(R.id.tvCon_itme_new);
                            tvLikeItemNew = (TextView) news.findViewById(R.id.tvLike_item_new);
                        }

                        private TextView tvLikeItemNew;
                        private TextView tvConItmeNew;
                        private TextView tvTimeItemNew;
                        private TextView tvTitleItemNew;
                        private ImageView ivItemNew;

                        @Override
                        public int getCount() {
                            if (num == 0) {
                                return 1;
                            } else {
                                return num;
                            }
                        }

                        @Override
                        public Object getItem(int position) {
                            return null;
                        }

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

                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            convertView = View.inflate(SearchActivity.this, R.layout.item_new, null);
                            initView(convertView);
                            try {
                                if (response.getString("total").equals("0")) {
                                    tvTitleItemNew.setText("未搜索到相关数据");
                                    tvConItmeNew.setText("未搜索到相关数据");
                                    tvTimeItemNew.setText("发布时间:");
                                    tvLikeItemNew.setText("点赞数:");
                                } else {
                                    Glide.with(SearchActivity.this).load(ip + response.getJSONArray("rows").getJSONObject(position).getString("cover")).into(ivItemNew);
                                    tvTitleItemNew.setText(response.getJSONArray("rows").getJSONObject(position).getString("title"));
                                    tvConItmeNew.setText(response.getJSONArray("rows").getJSONObject(position).getString("content"));
                                    tvTimeItemNew.setText("发布时间:" + response.getJSONArray("rows").getJSONObject(position).getString("publishDate"));
                                    tvLikeItemNew.setText("点赞数:" + response.getJSONArray("rows").getJSONObject(position).getString("likeNum"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            return convertView;
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(SearchActivity.this, "服务错误", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SearchActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    /**
     * 展示详细页面
     * @param list
     */
    private void initBtn(final List<Integer> list) {
        intent = new Intent(SearchActivity.this, ShowActivity.class);
        gvShowNew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent.putExtra("idNews",list.get(position));
                startActivity(intent);
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值