1.首页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
>
<include layout="@layout/include_layout"/>
</LinearLayout>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:id="@+id/smar_layout"
android:layout_weight="9"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.recker.flybanner.FlyBanner
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/fly_banner"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/rece_view"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rece_view1"
/>
</LinearLayout>
</ScrollView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
2.首页面java
public class Frag1Fragment extends BaseFragment<Frag1Presenter> implements Frag1Contract.View {
@BindView(R.id.fly_banner)
FlyBanner flyBanner;
Unbinder unbinder;
@BindView(R.id.btn_sao)
Button btnSao;
@BindView(R.id.rece_view)
RecyclerView receView;
@BindView(R.id.rece_view1)
RecyclerView receView1;
@BindView(R.id.smar_layout)
SmartRefreshLayout smarLayout;
@BindView(R.id.edit_text)
EditText editText;
@BindView(R.id.btn_xiaoxi)
Button btnXiaoxi;
public static Frag1Fragment newInstance() {
Frag1Fragment fragment = new Frag1Fragment();
return fragment;
}
@Override
public void setupFragmentComponent(@NonNull AppComponent appComponent) {
DaggerFrag1Component //如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.frag1Module(new Frag1Module(this))
.build()
.inject(this);
}
@Override
public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_frag1, container, false);
}
@Override
public void initData(@Nullable Bundle savedInstanceState) {
//数据库
DaoSession daoSession = jingd.bwie.com.jdong.app.service.App.getInstance().daoSession();
User1Dao user1Dao = daoSession.getUser1Dao();
user1Dao.insert(new User1(null,"哈哈哈","啦啦啦","呀呀呀"));
user1Dao.loadAll();
smarLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
}
});
smarLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
}
});
//设置 Header 为 贝塞尔雷达 样式
smarLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setEnableHorizontalDrag(true));
//设置 Footer 为 球脉冲 样式
smarLayout.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));
}
/**
* 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
* 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
* 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
* <p>
* 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
* 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
* 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
* <p>
* Example usage:
* <pre>
* public void setData(@Nullable Object data) {
* if (data != null && data instanceof Message) {
* switch (((Message) data).what) {
* case 0:
* loadData(((Message) data).arg1);
* break;
* case 1:
* refreshUI();
* break;
* default:
* //do something
* break;
* }
* }
* }
*
* // call setData(Object):
* Message data = new Message();
* data.what = 0;
* data.arg1 = 1;
* fragment.setData(data);
* </pre>
*
* @param data 当不需要参数时 {@code data} 可以为 {@code null}
*/
@Override
public void setData(@Nullable Object data) {
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showMessage(@NonNull String message) {
checkNotNull(message);
ArmsUtils.snackbarText(message);
}
@Override
public void launchActivity(@NonNull Intent intent) {
checkNotNull(intent);
ArmsUtils.startActivity(intent);
}
@Override
public void killMyself() {
}
@Override
public void showData(News1 news1) {
List<News1.DataBean> data = news1.getData();
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
String icon = data.get(i).getIcon();
Log.i("aaa", "showData: " + icon);
list.add(data.get(i).getIcon());
}
flyBanner.setImagesUrl(list);
}
@Override
public void showData1(News2 news2) {
List<News2.DataBean> data = news2.getData();
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
receView.setLayoutManager(gridLayoutManager);
MyJiuAdapter myJiuAdapter = new MyJiuAdapter(getActivity(), data);
receView.setAdapter(myJiuAdapter);
}
@Override
public void showData2(News3 news3) {
List<News3.TuijianBean.ListBean> list = news3.getTuijian().getList();
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
receView1.setLayoutManager(gridLayoutManager);
MyLieAdapter myLieAdapter = new MyLieAdapter(getActivity(), list);
receView1.setAdapter(myLieAdapter);
myLieAdapter.setOnItemClickLisentener(new MyLieAdapter.onItemClickLisentener() {
@Override
public void onItemClick(int layoutPosition) {
int pid = list.get(layoutPosition).getPid();
Intent intent = new Intent(getActivity(), ShopMessageActivity.class);
intent.putExtra("pid", pid + "");
startActivity(intent);
}
});
}
@Override
public void error(String s) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
unbinder = ButterKnife.bind(this, rootView);
mPresenter.getData();
mPresenter.getJiugongGe();
mPresenter.getLiebiao();
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@OnClick(R.id.edit_text)
public void onViewClicked() {
Intent intent = new Intent(getActivity(), LiushiActivity.class);
startActivity(intent);
}
}
3.九宫格适配器
public class MyJiuAdapter extends RecyclerView.Adapter<MyJiuAdapter.JiuGongGe> {
private Context context;
private List<News2.DataBean> list;
public MyJiuAdapter(Context context, List<News2.DataBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public JiuGongGe onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.jiugongge_layout, null);
JiuGongGe jiuGongGe = new JiuGongGe(inflate);
return jiuGongGe;
}
@Override
public void onBindViewHolder(@NonNull JiuGongGe holder, int position) {
holder.text_title.setText(list.get(position).getName());
Uri parse = Uri.parse(list.get(position).getIcon());
holder.simp_view.setImageURI(parse);
}
@Override
public int getItemCount() {
return list.size();
}
class JiuGongGe extends RecyclerView.ViewHolder
{
private final TextView text_title;
private final SimpleDraweeView simp_view;
public JiuGongGe(View itemView) {
super(itemView);
simp_view = (SimpleDraweeView)itemView.findViewById(R.id.simp_view);
text_title = (TextView)itemView.findViewById(R.id.text_title);
}
}
}
4.列表适配器
public class MyLieAdapter extends RecyclerView.Adapter<MyLieAdapter.LieBiao> {
private Context context;
private List<News3.TuijianBean.ListBean> list3;
onItemClickLisentener onItemClickLisentener;
public interface onItemClickLisentener{
void onItemClick(int layoutPosition);
}
public void setOnItemClickLisentener(onItemClickLisentener onItemClickLisentener){
this.onItemClickLisentener=onItemClickLisentener;
}
public MyLieAdapter(Context context, List<News3.TuijianBean.ListBean> list3) {
this.context = context;
this.list3 = list3;
}
@NonNull
@Override
public LieBiao onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.show_layout, null);
LieBiao lieBiao = new LieBiao(inflate);
return lieBiao;
}
@Override
public void onBindViewHolder(@NonNull LieBiao holder, int position) {
String images = list3.get(position).getImages();
if (images.contains("|"))
{
images=images.substring(0,images.indexOf("|"));
}
else {
images = list3.get(position).getImages();
}
holder.text_shopname.setText(list3.get(position).getSubhead());
holder.text_shopprice.setText(list3.get(position).getPrice()+"");
Uri parse = Uri.parse(images);
holder.simp_view1.setImageURI(parse);
}
@Override
public int getItemCount() {
return list3.size();
}
class LieBiao extends RecyclerView.ViewHolder implements View.OnClickListener {
private final SimpleDraweeView simp_view1;
private final TextView text_shopname;
private final TextView text_shopprice;
public LieBiao(View itemView) {
super(itemView);
simp_view1 = (SimpleDraweeView)itemView.findViewById(R.id.simp_view1);
text_shopname = (TextView)itemView.findViewById(R.id.text_shopname);
text_shopprice = (TextView)itemView.findViewById(R.id.text_shopprice);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int layoutPosition = getLayoutPosition();
onItemClickLisentener.onItemClick(layoutPosition);
}
}
private OncheckedLisenner oncheckedLisenner;;
interface OncheckedLisenner{
void onchecked(int layoutPosition, boolean checked);
}
public void setOncheckedLisenner(OncheckedLisenner oncheckedLisenner){
this.oncheckedLisenner=oncheckedLisenner;
}
}
5.列表布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="150dp"
android:layout_height="150dp"
app:placeholderImage="@mipmap/ic_launcher"
android:id="@+id/simp_view1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:layout_marginLeft="20dp"
android:id="@+id/text_shopname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="price"
android:textColor="#f00"
android:layout_marginLeft="20dp"
android:id="@+id/text_shopprice"
/>
</LinearLayout>
6.跳转详情页面java
public class ShopMessageActivity extends BaseActivity<ShopMessagePresenter> implements ShopMessageContract.View {
@BindView(R.id.simp_shopmessage)
SimpleDraweeView simpShopmessage;
@BindView(R.id.text_shopmessageprice)
TextView textShopmessageprice;
@BindView(R.id.text_shopmessageyuanprice)
TextView textShopmessageyuanprice;
@BindView(R.id.text_shopmessagetitle)
TextView textShopmessagetitle;
@BindView(R.id.btn_jiaru)
Button btnJiaru;
@BindView(R.id.btn_goumai)
Button btnGoumai;
private NewsShopmessage.DataBean data;
private String pid;
private String uid;
Map<String,String> map = new HashMap<>();
@Override
public void setupActivityComponent(@NonNull AppComponent appComponent) {
DaggerShopMessageComponent //如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.shopMessageModule(new ShopMessageModule(this))
.build()
.inject(this);
}
@Override
public int initView(@Nullable Bundle savedInstanceState) {
return R.layout.activity_shop_message; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
}
@Override
public void initData(@Nullable Bundle savedInstanceState) {
Intent intent = getIntent();
pid = intent.getStringExtra("pid");
mPresenter.getshopmessage(pid);
if(!EventBus.getDefault().isRegistered(this))
{
EventBus.getDefault().register(this);
}
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onMessageEvent(User user)
{
uid = String.valueOf(user.getW());
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showMessage(@NonNull String message) {
checkNotNull(message);
ArmsUtils.snackbarText(message);
}
@Override
public void launchActivity(@NonNull Intent intent) {
checkNotNull(intent);
ArmsUtils.startActivity(intent);
}
@Override
public void killMyself() {
finish();
}
@Override
public void showDatamessage1(NewsShopmessage newsShopmessage) {
data = newsShopmessage.getData();
String images = data.getImages();
double price = data.getPrice();
textShopmessageprice.setText(price+"");
String title = data.getTitle();
textShopmessagetitle.setText(title);
if(images.contains("|"))
{
images = images.substring(0,images.indexOf("|"));
}
else
{
images = data.getImages();
}
Uri parse = Uri.parse(images);
simpShopmessage.setImageURI(parse);
}
@Override
public void showDataShopadd(NewsShopadd newsShopadd) {
String msg = newsShopadd.getMsg();
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: add setContentView(...) invocation
ButterKnife.bind(this);
}
@OnClick({R.id.btn_jiaru, R.id.btn_goumai})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_jiaru:
if(uid==null)
{
Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
}
else
{
map.put("uid",uid);
map.put("pid",pid);
mPresenter.getshopadd(map);
}
break;
case R.id.btn_goumai:
break;
}
}
}
-----------跳转详情页面接口
//商品详情
@GET("product/getProductDetail?")
Observable<NewsShopmessage> getShopmessage(@Query("pid") String uid);
7.详情页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="@dimen/dp_10"
android:layout_gravity="center"
android:id="@+id/simp_shopmessage"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginTop="@dimen/sp_14"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥:25.9"
android:textSize="@dimen/sp_16"
android:textColor="#f00"
android:id="@+id/text_shopmessageprice"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原价格¥ 86"
android:textSize="@dimen/sp_16"
android:textColor="#bbbbbb"
android:id="@+id/text_shopmessageyuanprice"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxx"
android:textSize="@dimen/sp_16"
android:layout_marginTop="@dimen/sp_16"
android:textStyle="bold"
android:id="@+id/text_shopmessagetitle"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="@dimen/dp_72">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_jiaru"
android:background="#f00"
android:text="加入购物车"
android:textSize="20sp"
android:textColor="#ffffff"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_goumai"
android:background="#ff0"
android:text="立即购买"
android:textSize="20sp"
android:textColor="#ffffff"
/>
</LinearLayout>
</LinearLayout>
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=".mvp.ui.activity.LiushiActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入搜索内容"
android:id="@+id/edit_liushi"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:text="搜索"
/>
</LinearLayout>
<com.fyales.tagcloud.library.TagCloudLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
app:tagSpacing="15dp"
app:lineSpacing="@dimen/dp_10"
android:id="@+id/tag_layout"
/>
</LinearLayout>
9.流式页面
public class LiushiActivity extends AppCompatActivity {
private TagCloudLayout tag_layout;
private ArrayList<String> mList;
private TagBaseAdapter mAdapter;
private EditText edit_liushi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_liushi);
tag_layout = (TagCloudLayout)findViewById(R.id.tag_layout);
edit_liushi = (EditText)findViewById(R.id.edit_liushi);
mList = new ArrayList<>();
mList.add("中华人民共和国");
mList.add("大韩民国");
mList.add("日本");
mList.add("朝鲜");
mList.add("台湾");
mList.add("香港特别行政区");
mList.add("澳门特别行政区");
mList.add("越南");
mList.add("老挝");
mList.add("柬埔寨");
mList.add("泰国");
mList.add("缅甸");
mList.add("马来西亚");
mList.add("新加坡");
mList.add("印度尼西亚");
mList.add("文莱");
mList.add("菲律宾");
mAdapter = new TagBaseAdapter(this,mList);
findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = edit_liushi.getText().toString();
mList.add(s);
mAdapter.notifyDataSetChanged();
}
});
tag_layout.setAdapter(mAdapter);
tag_layout.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
@Override
public void itemClick(int position) {
Toast.makeText(LiushiActivity.this,mList.get(position),Toast.LENGTH_SHORT).show();
}
});
}
}