//MainActivity 页面 public class MainActivity extends AppCompatActivity { private BottomBar bottomBar; private ViewPager viewPager; private HomeFragment homeFragment; private DiscoverFragment discoverFragment; private MyFrangment myFragment; private List<Fragment> list = new ArrayList<>(); private MyFragmentPagerAdapter myFragmentPagerAdapter; private CustomTitle customTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomBar = findViewById(R.id.bottomBar); viewPager = findViewById(R.id.viewpager); customTitle = findViewById(R.id.cu); initdata(); myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),list); viewPager.setAdapter(myFragmentPagerAdapter); viewPager.setCurrentItem(1); intnews(); viewPager.setOnPageChangeListener(new MyPagerChangeListener()); customTitle.setOnClickCustTitleListener(new CustomTitle.OnClickCustTitleListener() { @Override public void OnLeftClick() { } @Override public void OnrightClick() { } }); } private void initdata() { homeFragment = new HomeFragment(); list.add(homeFragment); discoverFragment = new DiscoverFragment(); list.add(discoverFragment); myFragment = new MyFrangment(); list.add(myFragment); } private void intnews() { bottomBar.setOnTabSelectListener(new OnTabSelectListener() { @Override public void onTabSelected(@IdRes int tabId) { switch (tabId) { case R.id.tab_shouye: viewPager.setCurrentItem(0,false); break; case R.id.tab_all: viewPager.setCurrentItem(1,false); break; case R.id.tab_cart: viewPager.setCurrentItem(2,false); break; case R.id.tab_account: viewPager.setCurrentItem(3,false); break; } } }); } class MyFragmentPagerAdapter extends FragmentPagerAdapter { private FragmentManager mfragmentManager; private List<Fragment> mlist; public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.mlist = list; } public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } } class MyPagerChangeListener implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: //bottomBar.selectTabWithId(R.id.tab_shouye); Toast.makeText(MainActivity.this,"0",Toast.LENGTH_LONG).show(); break; case 1: // bottomBar.selectTabWithId(R.id.tab_all); Toast.makeText(MainActivity.this,"1",Toast.LENGTH_LONG).show(); break; case 2: //bottomBar.selectTabWithId(R.id.tab_cart); Toast.makeText(MainActivity.this,"2",Toast.LENGTH_LONG).show(); break; case 3: //bottomBar.selectTabWithId(R.id.tab_account); Toast.makeText(MainActivity.this,"2",Toast.LENGTH_LONG).show(); break; } } @Override public void onPageScrollStateChanged(int state) { } } } //MianActivity的副本
<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=".MainActivity"> <com.example.zhoukaomomi1_7_7.custmview.CustomTitle android:id="@+id/cu" android:layout_width="match_parent" android:layout_height="50dp" app:title_text="段子" android:background="#3333FF" > </com.example.zhoukaomomi1_7_7.custmview.CustomTitle> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </android.support.v4.view.ViewPager> <com.roughike.bottombar.BottomBar android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentBottom="true" app:bb_tabXmlResource="@xml/bottombar_tabs"/> </LinearLayout>//自建xml文件包 并创建xml布局文件 底部导航
<tabs> <tab id="@+id/tab_shouye" title="推荐" icon="@drawable/shouye" /> <tab id="@+id/tab_all" title="段子" icon="@drawable/all" /> <tab id="@+id/tab_cart" title="发现" icon="@drawable/cart" /> <tab id="@+id/tab_account" title="视频" icon="@drawable/account" /> </tabs>//创建JavaAdapter 适配器
public class JavaAdapter extends XRecyclerView.Adapter<JavaAdapter.javaViewHolder>{ private List<JavaBean.DataBean>data; private JavaAdapter.javaViewHolder javaViewHolder; public JavaAdapter(List<JavaBean.DataBean> data){ this.data=data; } @Override public javaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.java_adapter,parent,false); javaViewHolder=new javaViewHolder(view); return javaViewHolder; } @Override public void onBindViewHolder(javaViewHolder holder, final int position) { holder.textView.setText(data.get(position).getContent()); Uri uri=Uri.parse(data.get(position).getUser().getIcon()+""); holder.simpleDraweeView1.setImageURI(uri); holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bean bean=new Bean(null,data.get(position).getContent(),data.get(position).getUser().getIcon()+""); onClickJavaAdaper.OnJavaAdaperClick(bean); } }); } @Override public int getItemCount() { return data == null ? 0 : data.size(); } class javaViewHolder extends RecyclerView.ViewHolder{ private final TextView textView; private final SimpleDraweeView simpleDraweeView1; private final ImageView imageView; public javaViewHolder(View itemView) { super(itemView); textView=itemView.findViewById(R.id.java_adaper_tv); simpleDraweeView1=itemView.findViewById(R.id.java_adaper_image); imageView=itemView.findViewById(R.id.java_adaper_image1); } } OnClickJavaAdaper onClickJavaAdaper; public interface OnClickJavaAdaper{ void OnJavaAdaperClick(Bean bean); } public void setOnClickJavaAdaper(OnClickJavaAdaper onClickJavaAdaper) { this.onClickJavaAdaper = onClickJavaAdaper; } } //bean类
@Entity(nameInDb = "Bean") public class Bean { @Id(autoincrement = true) private Long id; private String name; private String imageurl; @Generated(hash =1622626014) public Bean(Long id,String name,String imageurl){ this.id=id; this.name=name; this.imageurl=imageurl; } @Generated(hash = 80546095) public Bean(){ } public Long getId(){ return this.id; } public void setId(Long id) { this.id = id; } public String getName(){ return this.name; } public void setName(String name){ this.name=name; } public String getImageurl(){ return this.imageurl; } public void setImageurl(String imageurl){ this.imageurl=imageurl; } @Override public String toString() { return "Bean{"+ "id"+id+ ",name='"+name+ '\'' + ",imageurl='"+imageurl+'\'' + '}'; } } //工具包
public class OkUtils { private final OkHttpClient okHttpClient; private static OkUtils mokUtils; private final Handler mhandler; private OkUtils(){ mhandler = new Handler(Looper.getMainLooper()); okHttpClient=new OkHttpClient.Builder() .connectTimeout(5000,TimeUnit.MILLISECONDS) .readTimeout(5000,TimeUnit.MILLISECONDS) .writeTimeout(5000,TimeUnit.MILLISECONDS) .build(); } public static OkUtils getokClient(){ if (mokUtils == null) { synchronized (OkUtils.class) { if (mokUtils == null) { return mokUtils = new OkUtils(); } } } return mokUtils; } public void doGet(String url, final IOkutils iOkutils){ final Request request = new Request.Builder() .get() .url(url) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mhandler.post(new Runnable() { @Override public void run() { iOkutils.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response!=null && response.isSuccessful()){ final String json = response.body().string(); mhandler.post(new Runnable() { @Override public void run() { iOkutils.onResponse(json); } }); } } }); } public void doPost(String url, Map<String,String> map, final IOkutils iOkutils){ FormBody.Builder builder = new FormBody.Builder(); for (String key :map.keySet()){ builder.add(key,map.get(key)); } FormBody build = builder.build(); Request request = new Request.Builder() .post(build) .url(url) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mhandler.post(new Runnable() { @Override public void run() { iOkutils.onFailure(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response !=null && response.isSuccessful()){ final String json = response.body().string(); mhandler.post(new Runnable() { @Override public void run() { iOkutils.onResponse(json); } }); } } }); } public interface IOkutils{ void onFailure(IOException e); void onResponse(String json); } }//JavaBean类
public class JavaBean { /** * msg : 获取段子列表成功 * code : 0 * data : [{"commentNum":null,"content":"我就想问谁发的一帮傻子?我想在你的腿上写个 秀字!","createTime":"2018-07-07T08:28:06","imgUrls":null,"jid":3286,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"一帮傻子","createTime":"2018-07-06T20:33:35","imgUrls":null,"jid":3285,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":",","createTime":"2018-07-06T19:44:50","imgUrls":null,"jid":3284,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"发个段子,震震楼好吧。","createTime":"2018-07-06T17:21:32","imgUrls":null,"jid":3283,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"能不能考到","createTime":"2018-07-06T09:56:05","imgUrls":null,"jid":3282,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"水电费","createTime":"2018-07-06T08:39:33","imgUrls":null,"jid":3281,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"发表不了","createTime":"2018-07-05T20:17:18","imgUrls":null,"jid":3280,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"ojbk","createTime":"2018-07-05T20:17:02","imgUrls":null,"jid":3279,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"略略略","createTime":"2018-07-05T20:16:55","imgUrls":null,"jid":3278,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}},{"commentNum":null,"content":"垃圾","createTime":"2018-07-05T20:15:33","imgUrls":null,"jid":3277,"praiseNum":null,"shareNum":null,"uid":10234,"user":{"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"}}] */ private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * commentNum : null * content : 我就想问谁发的一帮傻子?我想在你的腿上写个 秀字! * createTime : 2018-07-07T08:28:06 * imgUrls : null * jid : 3286 * praiseNum : null * shareNum : null * uid : 10234 * user : {"age":null,"fans":"null","follow":false,"icon":null,"nickname":null,"praiseNum":"null"} */ private Object commentNum; private String content; private String createTime; private Object imgUrls; private int jid; private Object praiseNum; private Object shareNum; private int uid; private UserBean user; public Object getCommentNum() { return commentNum; } public void setCommentNum(Object commentNum) { this.commentNum = commentNum; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public Object getImgUrls() { return imgUrls; } public void setImgUrls(Object imgUrls) { this.imgUrls = imgUrls; } public int getJid() { return jid; } public void setJid(int jid) { this.jid = jid; } public Object getPraiseNum() { return praiseNum; } public void setPraiseNum(Object praiseNum) { this.praiseNum = praiseNum; } public Object getShareNum() { return shareNum; } public void setShareNum(Object shareNum) { this.shareNum = shareNum; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public UserBean getUser() { return user; } public void setUser(UserBean user) { this.user = user; } public static class UserBean { /** * age : null * fans : null * follow : false * icon : null * nickname : null * praiseNum : null */ private Object age; private String fans; private boolean follow; private Object icon; private Object nickname; private String praiseNum; public Object getAge() { return age; } public void setAge(Object age) { this.age = age; } public String getFans() { return fans; } public void setFans(String fans) { this.fans = fans; } public boolean isFollow() { return follow; } public void setFollow(boolean follow) { this.follow = follow; } public Object getIcon() { return icon; } public void setIcon(Object icon) { this.icon = icon; } public Object getNickname() { return nickname; } public void setNickname(Object nickname) { this.nickname = nickname; } public String getPraiseNum() { return praiseNum; } public void setPraiseNum(String praiseNum) { this.praiseNum = praiseNum; } } } } //相当于创建数据库的SQLite
public class DaoManger { private final DaoSession daoSession; private static DaoManger daoManger; private DaoManger(Context context){ daoSession = DaoMaster.newDevSession(context, "wo.db"); } public static DaoManger instance(Context context){ if (daoManger==null){ synchronized (DaoManger.class){ if (daoManger==null){ return daoManger=new DaoManger(context); } } } return daoManger; } public DaoSession getDaoSession() { return daoSession; } } //展示数据可以上拉下刷
public class HomeFragment extends Fragment { private static final String TAG="HomeFragment"; private BeanDao beanDao; private OkUtils okUtils; private XRecyclerView xRecyclerView; private JavaAdapter javaAdaper; private int cc = 1; private int page = 3; private List<JavaBean.DataBean> datas = new ArrayList<>(); private DaoSession daoSession; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.home_fragment,container,false); xRecyclerView = v.findViewById(R.id.home_xrecycler); return v; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); daoSession = DaoManger.instance(getContext()).getDaoSession(); beanDao = daoSession.getBeanDao(); int page; initnews(); } private void initnews() { okUtils= OkUtils.getokClient(); String urls="https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=100&page="+page; okUtils.doGet(urls, new OkUtils.IOkutils() { @Override public void onFailure(IOException e) { Log.d(TAG,e+""); } @Override public void onResponse(String json) { Gson gson = new Gson(); JavaBean javaBean = gson.fromJson(json, JavaBean.class); List<JavaBean.DataBean> data = javaBean.getData(); final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); xRecyclerView.setLayoutManager(linearLayoutManager); javaAdaper = new JavaAdapter(datas); xRecyclerView.setAdapter(javaAdaper); javaAdaper.setOnClickJavaAdaper(new JavaAdapter.OnClickJavaAdaper() { @Override public void OnJavaAdaperClick(Bean bean) { List<Bean> list = beanDao.queryBuilder().list(); for (int i = 0; i < list.size(); i++) { Log.d(TAG,list.get(i).toString()); } } }); if (cc==1){ datas.clear(); xRecyclerView.refreshComplete(); }else { xRecyclerView.loadMoreComplete(); } datas.addAll(data); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { cc=1; page = 3; initnews(); } @Override public void onLoadMore() { cc = 2; ++page; Log.d(TAG,page+""); initnews(); } }); } }); } } // HomeFragment碎片的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/home_xrecycler" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>//创建数据库Dao类 和DaoManger结合使用
public class BeanDao extends AbstractDao<Bean, Long> { public static final String TABLENAME = "Bean"; /** * Properties of entity Bean.<br/> * Can be used for QueryBuilder and for referencing column names. */ public static class Properties { public final static Property Id = new Property(0, Long.class, "id", true, "_id"); public final static Property Name = new Property(1, String.class, "name", false, "NAME"); public final static Property Imageurl = new Property(2, String.class, "imageurl", false, "IMAGEURL"); } public BeanDao(DaoConfig config) { super(config); } public BeanDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } /** Creates the underlying database table. */ public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "\"Bean\" (" + // "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id "\"NAME\" TEXT," + // 1: name "\"IMAGEURL\" TEXT);"); // 2: imageurl } /** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"Bean\""; db.execSQL(sql); } @Override protected final void bindValues(DatabaseStatement stmt, Bean entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } String name = entity.getName(); if (name != null) { stmt.bindString(2, name); } String imageurl = entity.getImageurl(); if (imageurl != null) { stmt.bindString(3, imageurl); } } @Override protected final void bindValues(SQLiteStatement stmt, Bean entity) { stmt.clearBindings(); Long id = entity.getId(); if (id != null) { stmt.bindLong(1, id); } String name = entity.getName(); if (name != null) { stmt.bindString(2, name); } String imageurl = entity.getImageurl(); if (imageurl != null) { stmt.bindString(3, imageurl); } } @Override public Long readKey(Cursor cursor, int offset) { return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); } @Override public Bean readEntity(Cursor cursor, int offset) { Bean entity = new Bean( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // imageurl ); return entity; } @Override public void readEntity(Cursor cursor, Bean entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); entity.setImageurl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); } @Override protected final Long updateKeyAfterInsert(Bean entity, long rowId) { entity.setId(rowId); return rowId; } @Override public Long getKey(Bean entity) { if(entity != null) { return entity.getId(); } else { return null; } } @Override public boolean hasKey(Bean entity) { return entity.getId() != null; } @Override protected final boolean isEntityUpdateable() { return true; } } //和上一个一样 结合使用
public class DaoMaster extends AbstractDaoMaster { public static final int SCHEMA_VERSION = 1; /** Creates underlying database table using DAOs. */ public static void createAllTables(Database db, boolean ifNotExists) { BeanDao.createTable(db, ifNotExists); } /** Drops underlying database table using DAOs. */ public static void dropAllTables(Database db, boolean ifExists) { BeanDao.dropTable(db, ifExists); } /** * WARNING: Drops all table on Upgrade! Use only during development. * Convenience method using a {@link DevOpenHelper}. */ public static DaoSession newDevSession(Context context, String name) { Database db = new DevOpenHelper(context, name).getWritableDb(); DaoMaster daoMaster = new DaoMaster(db); return daoMaster.newSession(); } public DaoMaster(SQLiteDatabase db) { this(new StandardDatabase(db)); } public DaoMaster(Database db) { super(db, SCHEMA_VERSION); registerDaoClass(BeanDao.class); } public DaoSession newSession() { return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); } public DaoSession newSession(IdentityScopeType type) { return new DaoSession(db, type, daoConfigMap); } /** * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - */ public static abstract class OpenHelper extends DatabaseOpenHelper { public OpenHelper(Context context, String name) { super(context, name, SCHEMA_VERSION); } public OpenHelper(Context context, String name, CursorFactory factory) { super(context, name, factory, SCHEMA_VERSION); } @Override public void onCreate(Database db) { Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); createAllTables(db, false); } } /** WARNING: Drops all table on Upgrade! Use only during development. */ public static class DevOpenHelper extends OpenHelper { public DevOpenHelper(Context context, String name) { super(context, name); } public DevOpenHelper(Context context, String name, CursorFactory factory) { super(context, name, factory); } @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); dropAllTables(db, true); onCreate(db); } } } //这个也是 一样的
public class DaoSession extends AbstractDaoSession { private final DaoConfig beanDaoConfig; private final BeanDao beanDao; public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig> daoConfigMap) { super(db); beanDaoConfig = daoConfigMap.get(BeanDao.class).clone(); beanDaoConfig.initIdentityScope(type); beanDao = new BeanDao(beanDaoConfig, this); registerDao(Bean.class, beanDao); } public void clear() { beanDaoConfig.clearIdentityScope(); } public BeanDao getBeanDao() { return beanDao; } } //CustomTitle类
public class CustomTitle extends LinearLayout implements View.OnClickListener { private ImageView imageView1,imageView2; private TextView textView; public CustomTitle(Context context) { super(context); } public CustomTitle(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public CustomTitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray=context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomTitle,0,0); String CustomTitle_textView=typedArray.getString(R.styleable.CustomTitle_title_text); View view =inflate(context,R.layout.custom_title,this); imageView1= view.findViewById(R.id.custom_title_imag1); imageView2= view.findViewById(R.id.custom_title_imag2); textView=view.findViewById(R.id.custom_title_tv); textView.setText(CustomTitle_textView); imageView1.setOnClickListener(this); imageView2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.custom_title_imag1: onClickCustTitleListener.OnLeftClick(); break; case R.id.custom_title_imag2: onClickCustTitleListener.OnrightClick(); break; } } OnClickCustTitleListener onClickCustTitleListener; public interface OnClickCustTitleListener{ void OnLeftClick(); void OnrightClick(); } public void setOnClickCustTitleListener(OnClickCustTitleListener onClickCustTitleListener){ this.onClickCustTitleListener =onClickCustTitleListener; } } //CustomTitle的副本
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/custom_title_imag1" android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@drawable/user_icon" /> <TextView android:id="@+id/custom_title_tv" android:layout_weight="6" android:layout_width="0dp" android:layout_height="match_parent" android:text="2123" android:gravity="center" /> <ImageView android:id="@+id/custom_title_imag2" android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@drawable/edit_icon" /> </LinearLayout>
//MyApp 记得注册
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }//JavaAdapter的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:fresco="http://schemas.android.com/apk/res-auto"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/java_adaper_image" android:layout_width="80dp" android:layout_height="80dp" fresco:roundAsCircle="true" /> <TextView android:id="@+id/java_adaper_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@id/java_adaper_image" android:text="1" android:layout_toRightOf="@id/java_adaper_image" /> <ImageView android:id="@+id/java_adaper_image1" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentEnd="true" android:layout_centerVertical="true" fresco:srcCompat="@drawable/add_icon" android:layout_alignParentRight="true" /> </RelativeLayout> //自建Attrs文件
<resources> <declare-styleable name="CustomTitle"> <attr name="title_text" format="string"></attr> </declare-styleable> </resources>//记得添加网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>由于我的Studio版本升级过了 所以要多加几个权限
如果不加会包V4包冲突
implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:design:27.1.0' //所有依赖文件 (必填)
greendao{ schemaVersion 1//数据库版本号 daoPackage'com.example.zhoukaomomi1_7_7.database'//设置时生成代码的目录 targetGenDir 'src/main/java' //设置DaoMaster、DaoSession、Dao目录 }
configurations { all*.exclude group:'com.android.support',module:'support-v13' }
//底部导航依赖 implementation 'com.roughike:bottom-bar:2.0.2' //butterknife插件 implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' implementation 'com.squareup.okhttp3:okhttp:3.9.0' implementation 'com.google.code.gson:gson:2.6.2' //xRecyclerView的依赖 implementation 'com.jcodecraeer:xrecyclerview:1.5.9' implementation 'com.facebook.fresco:fresco:1.9.0' // 支持 GIF 动图,需要添加 implementation 'com.facebook.fresco:animated-gif:1.9.0' implementation'org.greenrobot:greendao:3.2.2' 因用到数据库 所以需要添加 greendao 布局最上边添加以下这条
apply plugin: 'org.greenrobot.greendao' 在大项目的build.gradle里边添加以下这条 数据库专用
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'