//依赖
//okhttpClient依赖 implementation 'com.squareup.okhttp3:okhttp:3.9.0' //拦截器的依赖 implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0' //gson依赖包 implementation 'com.google.code.gson:gson:2.8.4' //reclerview的依赖 implementation 'com.android.support:recyclerview-v7:27.1.1' //xRecyclerView的依赖 implementation 'com.jcodecraeer:xrecyclerview:1.3.2' implementation 'com.android.support:design:27.1.1' //butterknife插件依赖 implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' //fresco依赖 implementation 'com.facebook.fresco:fresco:1.9.0'
//使用fresco必须初始化 public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
//Main布局
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="8" > android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:id="@+id/main_radio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="horizontal"> <RadioButton android:id="@+id/main_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:button="@null" android:text="推介" /> <RadioButton android:id="@+id/main_cart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:button="@null" android:text="段子" /> RadioGroup> LinearLayout> LinearLayout>
//MainActivity
public class MainActivity extends AppCompatActivity { private Listlist=new ArrayList<>(); private ViewPager viewPager; private RadioGroup radio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { viewPager = findViewById(R.id.viewpager); radio = findViewById(R.id.main_radio); list.add(new HomeFragment()); list.add(new CartFragment()); MyFragment myFragment = new MyFragment(getSupportFragmentManager(), list); viewPager.setAdapter(myFragment); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: radio.check(R.id.main_home); break; case 1: radio.check(R.id.main_cart); break; } } @Override public void onPageScrollStateChanged(int state) { } }); radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.main_home: viewPager.setCurrentItem(0); break; case R.id.main_cart: viewPager.setCurrentItem(1); break; } } }); } }
//首页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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#0099ff" > <Button android:id="@+id/dian" android:layout_width="50dp" android:layout_height="wrap_content" android:background="@drawable/user_icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="段子" android:gravity="center_horizontal" android:layout_marginLeft="110dp" android:textColor="#fff" /> <Button android:layout_width="80dp" android:layout_height="50dp" android:background="@drawable/edit_icon" android:layout_marginLeft="130dp" /> LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/home_xrecyler" android:layout_width="match_parent" android:layout_height="match_parent" > com.jcodecraeer.xrecyclerview.XRecyclerView> LinearLayout>
//首页适配器布局
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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/item1_face" android:layout_width="50dp" android:layout_height="50dp" app:roundAsCircle="true" android:background="@mipmap/ic_launcher" /> <TextView android:id="@+id/item1_tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="jinlin" android:textColor="#f00" android:layout_marginLeft="80dp" /> <TextView android:id="@+id/item1_tv_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日期" android:layout_marginLeft="80dp" android:layout_marginTop="30dp" /> <Button android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/add_icon" android:layout_alignParentRight="true" /> RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/item1_faso" android:layout_width="300dp" android:layout_height="100dp" android:background="@mipmap/ic_launcher" /> RelativeLayout> LinearLayout>
//recylerView适配器
public class MyAdapter extends RecyclerView.Adapter { private List//首页 fragmentlist; public MyAdapter(List list) { this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), R.layout.home_item, null); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { ((MyHolder)holder).tv_name.setText(list.get(position).getContent()); ((MyHolder)holder).tv_date.setText(list.get(position).getCreateTime()); String pic = list.get(position).getUser().getIcon(); if (!TextUtils.isEmpty(pic)){ Uri uri = Uri.parse(pic); ((MyHolder)holder).face1.setImageURI(uri); ((MyHolder)holder).face2.setImageURI(uri); } } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public class MyHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView face1; private final TextView tv_name; private final TextView tv_date; private final SimpleDraweeView face2; public MyHolder(View itemView) { super(itemView); face1 = itemView.findViewById(R.id.item1_face); tv_name = itemView.findViewById(R.id.item1_tv_name); tv_date = itemView.findViewById(R.id.item1_tv_date); face2 = itemView.findViewById(R.id.item1_faso); } } }
import java.util.List; public class HomeFragment extends BaseFragmentimplements HomeView,View.OnClickListener{ private MyAdapter myAdapter; private XRecyclerView xRecyclerView; int a=1; private DaoSession daoSession; private PersonDao personDao; private static final String TAG = "HomeFragment****"; @Override protected void initData() { presenter.getNet(); } @Override protected void initListener() { } @Override protected void initViews(View view) { xRecyclerView = view.findViewById(R.id.home_xrecyler); } @Override protected int provId() { return R.layout.homefragment; } @Override protected HomePresent provide() { return new HomePresent((HomeView) this); } @Override public void onSuccess(HomeBean homeBean) { List data = homeBean.getData(); myAdapter = new MyAdapter(data); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); xRecyclerView.setLayoutManager(linearLayoutManager); xRecyclerView.setAdapter(myAdapter); xRecyclerView.setSaveFromParentEnabled(true); xRecyclerView.setLoadingMoreEnabled(true); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { a=1; initData(); xRecyclerView.refreshComplete(); } @Override public void onLoadMore() { a++; initData(); xRecyclerView.refreshComplete(); } }); daoSession = DaoManager.instance(getContext()).getDaoSession(); personDao = daoSession.getPersonDao(); for (int i = 0; i personDao.insert(new Person(null,""+data.get(i).getContent(),data.get(i).getCreateTime())); } } @Override public void onFaild(String error) { } @Override public void onClick(View v) { switch (v.getId()){ case R.id.dian: List list=personDao.queryBuilder() .where(PersonDao.Properties.Id.gt(1)) .build() .list(); Log.d(TAG, "onClick: "+list.get(0).getName1()); break; } } }
//DaoManager
public class DaoManager { private final DaoSession daoSession; private static DaoManager daoManager; private DaoManager(Context context) { daoSession = DaoMaster.newDevSession(context, "lo.db"); } public static DaoManager instance(Context context) { if (daoManager == null) { synchronized (DaoManager.class) { if (daoManager == null) { return daoManager = new DaoManager(context); } } } return daoManager; } public DaoSession getDaoSession() { return daoSession; } }