From 8d82df08d1504174770eca53322273720b427759 Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Mon, 20 Apr 2020 16:59:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=91=8A=E4=BF=A1=E6=81=AF=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BD=AE=E6=92=AD=EF=BC=8C=E7=AE=AD=E5=A4=B4=E6=8C=87?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 12 ++- .../pocket/data/adapter/NoticeAdpter.java | 24 ++--- .../ui/main/ui/forum/main/ForumFragment.java | 94 ++++++++++++++----- .../ui/main/ui/forum/main/MyBanner.java | 32 +++++++ .../java/com/community/pocket/util/Param.java | 10 +- app/src/main/res/drawable/ic_arrow_left.xml | 9 ++ app/src/main/res/drawable/ic_arrow_right.xml | 9 ++ .../layout/main/layout/forum/layout/forum.xml | 37 +++++++- .../main/layout/forum/layout/forum_notice.xml | 2 + app/src/main/res/values-en-rUS/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 12 files changed, 191 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/forum/main/MyBanner.java create mode 100644 app/src/main/res/drawable/ic_arrow_left.xml create mode 100644 app/src/main/res/drawable/ic_arrow_right.xml diff --git a/app/build.gradle b/app/build.gradle index 08f48dc..89dca1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { defaultConfig { applicationId "com.community.pocket" - minSdkVersion 17 + minSdkVersion 19 targetSdkVersion 29 versionCode 1 versionName "1.0" @@ -28,6 +28,14 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true + + compileOptions { + + sourceCompatibility JavaVersion.VERSION_1_8 + + targetCompatibility JavaVersion.VERSION_1_8 + + } } buildTypes { @@ -109,4 +117,6 @@ dependencies { // Test helpers testImplementation "androidx.room:room-testing:$room_version" + + implementation 'com.youth.banner:banner:2.0.7' } diff --git a/app/src/main/java/com/community/pocket/data/adapter/NoticeAdpter.java b/app/src/main/java/com/community/pocket/data/adapter/NoticeAdpter.java index ab92bb2..d5f22b5 100644 --- a/app/src/main/java/com/community/pocket/data/adapter/NoticeAdpter.java +++ b/app/src/main/java/com/community/pocket/data/adapter/NoticeAdpter.java @@ -12,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.community.pocket.R; import com.community.pocket.data.model.Notice; +import com.youth.banner.adapter.BannerAdapter; import java.util.Date; import java.util.List; @@ -20,25 +21,20 @@ import java.util.List; * 公告内容 * TODO 实现无限循环自动滚动 */ -public class NoticeAdpter extends RecyclerView.Adapter { +public class NoticeAdpter extends BannerAdapter { - private List noticeList; - - public NoticeAdpter(List noticeList) { - this.noticeList = noticeList; + public NoticeAdpter(List datas) { + super(datas); } - @NonNull @Override - public PagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - + public PagerViewHolder onCreateHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.forum_notice, parent, false); return new PagerViewHolder(itemView); } @Override - public void onBindViewHolder(@NonNull PagerViewHolder holder, int position) { - Notice notice = noticeList.get(position); + public void onBindView(PagerViewHolder holder, Notice notice, int position, int size) { //设置数据源 holder.title.setText(notice.getTitle()); holder.content.setText(notice.getContent()); @@ -49,13 +45,8 @@ public class NoticeAdpter extends RecyclerView.Adapter { + if (forumNoticeResponse == null) { + return; + } + + forumNoticeResponse.toast(getContext()); + + if (forumNoticeResponse.getResult() == Response.Result.OK) { + initNotice(forumNoticeResponse.getNoticeList()); + } + }); + } + + //初始化公告箭头事件 + private void initArrow() { + initFocusListener(noticePrev); + initFocusListener(noticeNext); + initClickListener(noticePrev); + initClickListener(noticeNext); + } + + //箭头随焦点改变透明度 + @SuppressLint("ClickableViewAccessibility") + private void initFocusListener(ImageView imageView) { + imageView.setOnTouchListener((v, event) -> { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + v.setAlpha(1); + break; + case MotionEvent.ACTION_UP: + v.setAlpha(0.1f); + break; } + return false; }); + } - //监控公告信息请求状态 - viewModel.getNoticeDataResponse().observe(getViewLifecycleOwner(), new Observer() { - @Override - public void onChanged(ForumNoticeResponse forumNoticeResponse) { - if (forumNoticeResponse == null) { - return; - } - - forumNoticeResponse.toast(getContext()); - - if (forumNoticeResponse.getResult() == Response.Result.OK) { - initNotice(forumNoticeResponse.getNoticeList()); - } + //箭头点击事件 + private void initClickListener(ImageView imageView) { + imageView.setOnClickListener(v -> { + if (imageView == noticePrev) { + banner.setCurrentItem(banner.getCurrentItem() > 0 ? banner.getCurrentItem() - 1 : banner.getItemCount() - 1); + } else { + banner.setCurrentItem(banner.getCurrentItem() < banner.getItemCount() - 1 ? banner.getCurrentItem() + 1 : 0); } }); } //加载公告信息 - private void initNotice(List notices) { - viewPager2.setAdapter(new NoticeAdpter(notices)); + private void initNotice(final List notices) { + + banner.setAdapter(new NoticeAdpter(notices)); + banner.setIndicator(new RectangleIndicator(getContext())); + banner.setIndicatorWidth(100, 200); + banner.setIndicatorNormalColor(getResources().getColor(R.color.colorAccent)); + banner.setIndicatorSelectedColor(getResources().getColor(R.color.colorBlack)); + + if (notices.size() >= 2) { + noticePrev.setVisibility(View.VISIBLE); + noticeNext.setVisibility(View.VISIBLE); + } else { + noticePrev.setVisibility(View.GONE); + noticeNext.setVisibility(View.GONE); + } + + initArrow(); } private void scrollTop() { diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/MyBanner.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/MyBanner.java new file mode 100644 index 0000000..f14b4e0 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/MyBanner.java @@ -0,0 +1,32 @@ +package com.community.pocket.ui.main.ui.forum.main; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; + +import androidx.annotation.NonNull; + +import com.community.pocket.data.adapter.NoticeAdpter; +import com.community.pocket.data.model.Notice; +import com.youth.banner.Banner; + +public class MyBanner extends Banner { + + public MyBanner(@NonNull Context context) { + super(context); + } + + public MyBanner(@NonNull Context context, @NonNull AttributeSet attrs) { + super(context, attrs); + } + + public MyBanner(@NonNull Context context, @NonNull AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent event) { + super.onInterceptTouchEvent(event); + return event.getAction() == MotionEvent.ACTION_DOWN; + } +} diff --git a/app/src/main/java/com/community/pocket/util/Param.java b/app/src/main/java/com/community/pocket/util/Param.java index e75fa26..6ad94ea 100644 --- a/app/src/main/java/com/community/pocket/util/Param.java +++ b/app/src/main/java/com/community/pocket/util/Param.java @@ -4,9 +4,17 @@ package com.community.pocket.util; * 参数传递Key */ public enum Param { + //重置密码用户 username, + //重置密码邮箱 email, + //论坛帖子id forumId, + //访客预约是否外来人口 isOutPeople, - enable + //我的信息修改密码按钮是否启用 + enable, + //公告滚动页数 + notice_prev, + notice_next } diff --git a/app/src/main/res/drawable/ic_arrow_left.xml b/app/src/main/res/drawable/ic_arrow_left.xml new file mode 100644 index 0000000..b1cdf47 --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_left.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_arrow_right.xml b/app/src/main/res/drawable/ic_arrow_right.xml new file mode 100644 index 0000000..527fd2b --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_right.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/main/layout/forum/layout/forum.xml b/app/src/main/res/layout/main/layout/forum/layout/forum.xml index 0f1a8b7..2d75da7 100644 --- a/app/src/main/res/layout/main/layout/forum/layout/forum.xml +++ b/app/src/main/res/layout/main/layout/forum/layout/forum.xml @@ -26,10 +26,41 @@ android:layout_height="wrap_content" android:orientation="vertical"> - + android:layout_height="wrap_content"> + + + + + + + diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index f931654..cd72dde 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -220,4 +220,6 @@ no data score%1d notes:\n%1s + notice prev + notice next \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index a0e3c43..7a8f746 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -220,4 +220,6 @@ 暂无历史信用分记录 信用分%1d 备注信息:\n%1s + 上一条公告 + 下一条公告 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ff0ccab..d139342 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -221,6 +221,8 @@ no data score%1d notes:\n%1s + notice prev + notice next