package com.json.search;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.json.search.adapter.SearchHistoryAdapter;
import com.json.search.adapter.SearchResultAdapter;
import com.json.search.bean.SearchHistoryBean;
import com.json.search.bean.SearchResultBean;
import com.json.search.greendao.SearchHistoryBeanDao;
import com.json.search.util.TimeDifferentUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.BaseRequest;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,BaseQuickAdapter.RequestLoadMoreListener{
public static String SEARCHURL = "http://gank.io/api/search/query/listview/category/";
private RelativeLayout rlProgress;//提示正在加载中
private ImageView ivBack;//返回按钮
private EditText etSearch;
private ImageView searchClear;//清除按钮
private TextView tvSearch;//标题栏上面的搜索按钮
private LinearLayout llHistory;//历史搜索的整体布局
private RecyclerView historySearchRv;//历史搜索记录
private SearchHistoryAdapter historyAdapter;//搜索历史的adapter
private RecyclerView searchResultRv;//搜索结果
private SearchResultAdapter resultAdapter;//搜索结果的adapter
private SwipeRefreshLayout swipeRefreshLayout;
private List<SearchResultBean.ResultsBean> lists;//数据源
private Gson gson;
private int currentPage = 1;//当前页
private int pageSize = 10;
private String format= "yyyyMMddHHmmss"; //初始化一些时间设置格式
private SearchHistoryBeanDao historyDao;//DAO
private SearchHistoryBean bean;
private List<SearchResultBean.ResultsBean> mDatas;
private View historyFooter;//添加尾部
private TextView tvClear;//清空历史记录
private static final int SEARCH_MESSAGE = 1;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case SEARCH_MESSAGE:
//网络请求数据
requestNetWorkDatas();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iniViews();
//初始化搜索结果的adapter
resultAdapter = new SearchResultAdapter(null);
resultAdapter.isFirstOnly(false);
resultAdapter.setOnLoadMoreListener(this);
searchResultRv.setAdapter(resultAdapter);
//先判断数据库中是否有数据,有,则显示历史记录
historyDao = MyApplication.getDaoSession(MainActivity.this).getSearchHistoryBeanDao();
List<SearchHistoryBean> historyBeenLists = historyDao.queryBuilder().orderDesc(SearchHistoryBeanDao.Properties.PublishedAt).build().list();
if(historyBeenLists != null && historyBeenLists.size() > 0){
//说明数据库中含有数据
llHistory.setVisibility(View.VISIBLE);
mDatas = new ArrayList<>();
for(SearchHistoryBean bean : historyBeenLists){
String title = bean.getDesc();
SearchResultBean.ResultsBean resultBean = new SearchResultBean.ResultsBean();
resultBean.setDesc(title);
resultBean.setUrl(bean.getUrl());
mDatas.add(resultBean);
}
historyAdapter = new SearchHistoryAdapter(mDatas);
historySearchRv.setAdapter(historyAdapter);
if(mDatas != null && mDatas.size() > 0){
historyAdapter.addFooterView(historyFooter);
}else{
historyAdapter.removeFooterView(historyFooter);
}
}
}
private void iniViews() {
rlProgress = (RelativeLayout) findViewById(R.id.rl_progress);
ivBack = (ImageView) findViewById(R.id.search_back);
etSearch = (EditText) findViewById(R.id.et_search);
searchClear = (ImageView) findViewById(R.id.search_clear);
tvSearch = (TextView) findViewById(R.id.tv_search);
llHistory = (LinearLayout) findViewById(R.id.ll_history);
//当历史搜索有数据时,则加载footer布局 清空历史记录
historyFooter = LayoutInflater.from(this).inflate(R.layout.history_search_footer, null);
tvClear = (TextView) historyFooter.findViewById(R.id.clear_tv);
//清空历史记录
tvClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//清空集合
mDatas.clear();
//清空数据库 删除所有数据
historyDao.deleteAll();
//实时更新UI界面,还需要隐藏掉尾布局,以及历史搜索等字样
historyAdapter.notifyDataSetChanged();
historyFooter.setVisibility(View.GONE);
llHistory.setVisibility(View.GONE);
}
});
ivBack.setOnClickListener(this);
etSearch.setOnClickListener(this);
searchClear.setOnClickListener(this);
tvSearch.setOnClickListener(this);
historySearchRv = (RecyclerView) findViewById(R.id.history_search_recyclerview);
searchResultRv = (RecyclerView) findViewById(R.id.search_data_recyclerview);
//设置展示样式
historySearchRv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
searchResultRv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
//EditText上有文字变动时,有未发出的搜索请求,则应取消
if(mHandler.hasMessages(SEARCH_MESSAGE)){
mHandler.removeMessages(SEARCH_MESSAGE);
}
//如果为空,直接显示搜索历史,否则,显示搜索结果
if(TextUtils.isEmpty(editable)){
searchClear.setVisibility(View.GONE);
rlProgress.setVisibility(View.GONE);//显示加载中的提示语
searchResultRv.setVisibility(View.GONE);
//需要进一步判断,因为用户如果点击的是键盘上自带的删除键的话,会导致历史搜索的布局不隐藏
if(mDatas != null && mDatas.size() > 0){
llHistory.setVisibility(View.VISIBLE);
}else{
没有合适的资源?快使用搜索试试~ 我知道了~
仿电商应用中搜索模块的实现【GreenDao数据库存储数据】
共89个文件
xml:52个
java:15个
png:8个
需积分: 10 23 下载量 152 浏览量
2017-03-10
16:51:01
上传
评论
收藏 150KB ZIP 举报
温馨提示
仿电商应用中搜索模块的实现【GreenDao数据库存储数据】 直接拿来就能用,方便
资源推荐
资源详情
资源评论
收起资源包目录
SearchContent.zip (89个子文件)
SearchContent
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
app
proguard-rules.pro 663B
src
test
java
com
json
search
ExampleUnitTest.java 409B
androidTest
java
com
json
search
ExampleInstrumentedTest.java 760B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable-hdpi
back_ic.png 339B
clear_search_ic.png 383B
search_icon.png 278B
drawable
search_bg_shape.xml 224B
search_footer_bg.xml 283B
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 79B
attrs.xml 324B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
history_search_footer.xml 850B
item_search_listview.xml 906B
activity_main.xml 5KB
activity_test.xml 844B
activity_info.xml 890B
values-w820dp
dimens.xml 364B
java
com
json
search
greendao
SearchHistoryBeanDao.java 5KB
DaoMaster.java 3KB
DaoSession.java 1KB
bean
SearchResultBean.java 2KB
SearchHistoryBean.java 1KB
InfoActivity.java 2KB
MainActivity.java 18KB
adapter
SearchHistoryAdapter.java 709B
SearchResultAdapter.java 709B
CustomProgressBar.java 5KB
MyApplication.java 1KB
util
TimeDifferentUtil.java 1KB
TestActivity.java 756B
AndroidManifest.xml 1KB
app.iml 14KB
.gitignore 8B
build.gradle 1KB
libs
SearchContent.iml 867B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 233B
.idea
gradle.xml 707B
runConfigurations.xml 564B
misc.xml 2KB
copyright
profiles_settings.xml 76B
modules.xml 363B
compiler.xml 686B
workspace.xml 203KB
libraries
javawriter_2_1_1.xml 330B
jsr305_2_0_1.xml 330B
rules_0_5.xml 722B
okgo_2_1_4.xml 561B
javax_annotation_api_1_2.xml 358B
okio_1_9_0.xml 501B
support_core_utils_24_2_1.xml 778B
support_compat_24_2_1.xml 932B
support_media_compat_24_2_1.xml 974B
runner_0_5.xml 728B
support_fragment_24_2_1.xml 946B
support_vector_drawable_24_2_1.xml 641B
support_annotations_24_2_1.xml 497B
hamcrest_library_1_3.xml 342B
support_core_ui_24_2_1.xml 939B
espresso_idling_resource_2_2_2.xml 683B
animated_vector_drawable_24_2_1.xml 646B
recyclerview_v7_24_2_1.xml 780B
BaseRecyclerViewAdapterHelper_2_8_5.xml 898B
support_v4_24_2_1.xml 420B
hamcrest_core_1_3.xml 526B
appcompat_v7_24_2_1.xml 762B
greendao_3_2_0.xml 515B
junit_4_12.xml 477B
greendao_api_3_2_0.xml 535B
hamcrest_integration_1_3.xml 354B
espresso_core_2_2_2.xml 628B
okhttp_3_4_1.xml 517B
gson_2_8_0.xml 507B
javax_inject_1.xml 324B
exposed_instrumentation_api_publish_0_5.xml 701B
.gitignore 127B
local.properties 500B
settings.gradle 16B
build.gradle 632B
共 89 条
- 1
资源评论
风一样的男人zzz
- 粉丝: 64
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功