一:首页布局
<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.example.yuekao_demo2.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <EditText android:id="@+id/edit" android:layout_width="380dp" android:layout_height="wrap_content" android:hint="在千万商品中海量搜索" android:singleLine="true" android:background="@drawable/search_shape" android:layout_marginTop="5dp" android:layout_marginLeft="8dp" /> <TextView android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:textSize="25dp" android:layout_marginLeft="17dp" android:layout_marginTop="5dp" android:textColor="#000000" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="最近搜索" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:textSize="20dp" android:textColor="#000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="雅诗兰黛" android:layout_marginTop="15dp" android:layout_marginLeft="30dp" android:textSize="18dp" android:background="@drawable/text_shape" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索发现" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:textSize="20dp" android:textColor="#000000" /> <com.zhy.view.flowlayout.TagFlowLayout android:id="@+id/tag_flow" android:layout_width="match_parent" android:layout_height="wrap_content" app:max_select="1" android:padding="20dp" > </com.zhy.view.flowlayout.TagFlowLayout> </LinearLayout>
二:首页Activity
public class MainActivity extends AppCompatActivity { private TagFlowLayout flowLayout; private TextView cancel; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取资源ID flowLayout=findViewById(R.id.tag_flow); cancel=findViewById(R.id.cancel); editText=findViewById(R.id.edit); //初始化 List<String> list=new ArrayList<>(); list.add("考拉三周年人气热销榜"); list.add("电动牙刷"); list.add("尤妮佳"); list.add("豆豆鞋"); list.add("沐浴露"); list.add("冷酸灵牙膏"); list.add("日东红茶"); list.add("榴莲"); list.add("美宝莲"); list.add("欧莱雅"); //创建适配器 final KeyAdapter adapter=new KeyAdapter(list); flowLayout.setAdapter(adapter); //吐司 flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() { @Override public boolean onTagClick(View view, int position, FlowLayout parent) { Toast.makeText(MainActivity.this,adapter.getItem(position),Toast.LENGTH_SHORT).show(); return true; } }); //跳转 cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); String s = editText.getText().toString(); intent.putExtra("name",s); startActivity(intent); } }); } }
流失布局适配器
public class KeyAdapter extends TagAdapter<String> { public KeyAdapter(List<String> datas) { super(datas); } @Override public View getView(FlowLayout parent, int position, String s) { //创建textview TextView textView=new TextView(parent.getContext()); textView.setText(s); textView.setTextSize(20); textView.setBackgroundColor(Color.LTGRAY); textView.setBackgroundResource(R.drawable.text_shape); if(position==0){ textView.setTextColor(Color.RED); }else { textView.setTextColor(Color.GRAY); } return textView; } }
三:XRecyclerView上拉加载,下拉刷新
1:布局
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yuekao_demo2.SecondActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" android:layout_marginTop="5dp" > <TextView android:id="@+id/back" android:layout_width="40dp" android:layout_height="55dp" android:text="返回" android:textSize="20dp" android:layout_marginLeft="15dp" /> <EditText android:id="@+id/search_button" android:layout_width="390dp" android:layout_height="wrap_content" android:hint="面膜" android:background="@drawable/search_shape" android:layout_marginLeft="5dp" /> </LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
2:Ancitivity
public class SecondActivity extends AppCompatActivity implements IPhoneView{
//注入P层 IPhonePresenter presenter; private XRecyclerView recyclerView; private PhoneAdapter adapter; private EditText editText1; private int page=1; String name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); presenter=new PhonePresenter(this); presenter.getList(); //初始化视图 initView(); //获取网络请求数据 getData(page); recyclerView.setPullRefreshEnabled(true); recyclerView.setLoadingMoreEnabled(true); recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page=1; getData(page); } @Override public void onLoadMore() { page++; getData(page); } }); } private void getData(int page) { OK.getOk().doGet("http://www.zhaoapi.cn/product/searchProducts?keywords="+name+"&page="+page+"&sort=0", new OkCallback() { @Override public void onUI(String json) { Gson gson=new Gson(); RootData rootData = gson.fromJson(json, RootData.class); adapter.addData(rootData.data); recyclerView.loadMoreComplete(); recyclerView.refreshComplete(); } @Override public void onFailed(String json) { } }); } private void initView() { //获取资源ID recyclerView=findViewById(R.id.recycler_view); editText1=findViewById(R.id.search_button); //布局管理器 LinearLayoutManager manager=new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); //分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL)); adapter=new PhoneAdapter(this); recyclerView.setAdapter(adapter); adapter.setListener(new PhoneAdapter.PhoneItemClickListener() { @Override public void onItemClick(int position) { Toast.makeText(SecondActivity.this,"您点击了"+position+"行",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(SecondActivity.this,ThirdActivity.class); startActivity(intent); } }); //接收传过来的值 Intent intent=getIntent(); Bundle extras = intent.getExtras(); name = extras.getString("name"); editText1.setText(name); } @Override public void showData(List<Phone> phones) { adapter.addData(phones); } }