依赖
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.fynn.fluidlayout:fluidlayout:1.0'
父gradle中添加(对比自己的添加)
// Top-level build file where you can add configuration options common to all sub -projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle中添加(对比添加)
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1
daoPackage 'com.rookie.testsimulation.gen'
targetGenDir 'src/main/java'
}
android {
compileSdkVersion 26
buildToolsVersion "27.0.0"
defaultConfig {
applicationId "com.rookie.testsimulation"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt' ), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs' , include : ['*.jar' ])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2' , {
exclude group: 'com.android.support' , module : 'support-annotations'
})
greenDao工具类,自己生成bean包,例如:然后点击工具生成类
@Entity
public class Record {
@Id (autoincrement = true )
private Long id;
private String name;
}
ISearchView
public interface ISearchView {
void showSearchData(List<SearchBean.DataBean> data);
}
MainActivity
public class MainActivity extends AppCompatActivity implements ISearchView {
String[] arrs = {"手机" , "电脑" , "月饼" , "三只松鼠" , "单机斗地主" , "天堂战记" , "妖精的尾巴" , "极限挑战" , "我们相爱吧" , "倚天屠龙记" , "明星大侦探"
};
@BindView (R.id.search_edit)
MySearchView mSearchEdit;
@BindView (R.id.search_btn)
TextView mSearchBtn;
@BindView (R.id.search_clear)
Button mSearchClear;
@BindView (R.id.search_list)
RecyclerView mSearchList;
private Button button;
private FluidLayout fluid_layout;
private int pscid;
private List<SearchBean.DataBean> list = new ArrayList<>();
private SearchPresenter searchPresenter;
private EditText search_content;
private int page = 1 ;
private RecordDao recordDao;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this );
fluid_layout = (FluidLayout) findViewById(R.id.fluid_layout);
search_content = (EditText) findViewById(R.id.search_content);
search_content.setFocusable(true );
search_content.setFocusableInTouchMode(true );
searchPresenter = new SearchPresenter(this );
initData();
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this , "search.db" , null );
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
recordDao = daoSession.getRecordDao();
}
private void initData () {
for (int i = 0 ; i < arrs.length; i++) {
button = new Button(this );
button.setText(arrs[i]);
button.setTextSize(13 );
final String s = button.getText().toString();
FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(6 , 6 , 6 , 6 );
fluid_layout.addView(button, params);
}
}
@Override
public void showSearchData (List<SearchBean.DataBean> data) {
if (data != null ) {
list.addAll(data);
for (int i = 0 ; i < data.size(); i++) {
pscid = data.get(i).getPscid();
}
Intent intent = new Intent(MainActivity.this , GoodsListActivity.class);
intent.putExtra("pscid" , pscid + "" );
startActivity(intent);
} else {
Toast.makeText(this , "没有这个商品" , Toast.LENGTH_SHORT).show();
}
}
@OnClick ({ R.id.search_btn, R.id.search_clear})
public void onClick (View v) {
switch (v.getId()) {
default :
break ;
case R.id.search_btn:
String context = search_content.getText().toString();
if (TextUtils.isEmpty(context.trim())){
Toast.makeText(this , "请输入要搜索的内容" , Toast.LENGTH_SHORT).show();
}else {
searchPresenter.searchParams(context);
Record record = new Record(null , context);
recordDao.insert(record);
select();
}
break ;
case R.id.search_clear:
recordDao.deleteAll();
select();
break ;
}
}
@Override
protected void onResume () {
super .onResume();
select();
}
private void select () {
List<Record> records = recordDao.loadAll();
SearchRecoedAdapter adapter = new SearchRecoedAdapter(records, this );
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this );
mSearchList.setLayoutManager(linearLayoutManager);
DividerItemDecoration dec = new DividerItemDecoration(MainActivity.this ,DividerItemDecoration.VERTICAL);
mSearchList.addItemDecoration(dec);
mSearchList.setAdapter(adapter);
}
}
SearchRecoedAdapter
public class SearchRecoedAdapter extends RecyclerView .Adapter <SearchRecoedAdapter .MyViewHolder >{
private List <Record> list ;
private Context context;
public SearchRecoedAdapter(List <Record> list , Context context) {
this.list = list ;
this.context = context;
}
@Override
public SearchRecoedAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
View view = View.inflate(context, R.layout.search_history_item,null );
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(SearchRecoedAdapter.MyViewHolder holder, int position) {
holder.tv_content.setText(list .get(position).getName());
}
@Override
public int getItemCount() {
return list .size();
}
public class MyViewHolder extends RecyclerView .ViewHolder {
private final TextView tv_content;
public MyViewHolder(View itemView) {
super(itemView);
tv_content = itemView.findViewById(R.id.tv_content);
}
}
}
GoodsAdapter
public class GoodsAdapter extends RecyclerView .Adapter <GoodsAdapter .GoodsViewHolder > {
private Context context;
private List <GoodsBean.DataBean> list ;
private int i ;
public GoodsAdapter(Context context, List <GoodsBean.DataBean> list , int i) {
this.context = context;
this.list = list ;
this.i=i;
}
@Override
public GoodsViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
View v = View.inflate(context, R.layout.layout_goods_list_item, null );
GoodsViewHolder holder = new GoodsViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(GoodsViewHolder holder, final int position) {
holder.tvTitle.setText(list .get(position).getTitle());
holder.price.setText("¥" +list .get(position).getPrice());
holder.num.setText("销量:" +list .get(position).getSalenum());
String images = list .get(position).getImages();
String[] split = images.split("\\|" );
Glide.with(context).load(split[0 ]).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, InfoActivity.class);
intent.putExtra("pid" ,list .get(position).getPid()+"" );
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list .size();
}
class GoodsViewHolder extends XRecyclerView .ViewHolder {
private TextView tvTitle;
private TextView price;
private ImageView img;
private TextView num;
public GoodsViewHolder(View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.gd_title);
price = itemView.findViewById(R.id.gd_price);
num = itemView.findViewById(R.id.gd_count);
img = itemView.findViewById(R.id.gd_img);
}
}
}
search_bgcolor
<shape xmlns:android ="http://schemas.android.com/apk/res/android"
android:shape ="rectangle" >
<size
android:height ="20dp"
android:width ="50dp"
/>
<solid android:color ="#A5ffffff" />
<corners android:radius ="18dp" />
</shape >
splash_time_shape
<shape xmlns:android ="http://schemas.android.com/apk/res/android" >
<size
android:width ="30dp"
android:height ="20dp" />
<corners android:radius ="20dp" />
<solid android:color ="#FF5B09" />
</shape >
activity_main
<LinearLayout 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"
tools:context="com.rookie.testsimulation.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#45999999"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/search_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/back"
/>
<com .rookie .testsimulation .MySearchView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/search_edit"
android:layout_height="@dimen/mysearchview_height" >
</com .rookie .testsimulation .MySearchView >
<Button
android:background="@drawable/splash_time_shape"
android:id="@+id/search_btn"
android:layout_width="40dp"
android:layout_height="25sp"
android:textSize="12sp"
android:textStyle="bold"
android:text="搜索"
/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索发现"
android:textStyle="bold"
android:textSize="16dp"
android:textColor="#000"
android:layout_margin="10dp"
/>
<com .fynn .fluidlayout .FluidLayout
android:id="@+id/fluid_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
</com .fynn .fluidlayout .FluidLayout >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:textColor="#000"
android:layout_margin="10dp"
android:text="历史搜索" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="-5dp"
android:background="#e1e1ef" />
<android.support .v 7.widget .RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_list" >
</android.support .v 7.widget .RecyclerView >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清 空 历 史 记 录"
android:textSize="18sp"
android:id="@+id/search_clear" />
</LinearLayout>
</ScrollView>
</LinearLayout>
layout_custom_search
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable /search_bgcolor"
android:padding="8dp" >
<EditText
android:id="@+ id/search_content"
android:layout_width="0dp"
android:layout_height="@dimen /search_height"
android:layout_weight="1"
android:background="@null "
android:hint="京东超级品牌日"
android:textColorHint="#999999"
android:paddingLeft="10dp"
/>
<ImageView
android:layout_width="@dimen /search_height"
android:layout_height="@dimen /search_height"
android:src="@drawable /camera" />
</LinearLayout>
search_history_item
<LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textSize ="20sp"
android:paddingLeft ="10dp"
android:padding ="10dp"
android:textStyle ="bold"
android:id ="@+id/tv_content" />
</LinearLayout >
MySearchView
public class MySearchView extends LinearLayout {
private EditText search_content;
public MySearchView (Context context) {
this (context,null );
}
public MySearchView (Context context, @Nullable AttributeSet attrs) {
this (context, attrs,0 );
}
public MySearchView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super (context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.layout_custom_search,this );
search_content = view.findViewById(R.id.search_content);
}
}