公告信息自动轮播,箭头指示

0515
panqihua 5 years ago
parent f4fe963f85
commit 8d82df08d1
  1. 12
      app/build.gradle
  2. 24
      app/src/main/java/com/community/pocket/data/adapter/NoticeAdpter.java
  3. 94
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java
  4. 32
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/MyBanner.java
  5. 10
      app/src/main/java/com/community/pocket/util/Param.java
  6. 9
      app/src/main/res/drawable/ic_arrow_left.xml
  7. 9
      app/src/main/res/drawable/ic_arrow_right.xml
  8. 37
      app/src/main/res/layout/main/layout/forum/layout/forum.xml
  9. 2
      app/src/main/res/layout/main/layout/forum/layout/forum_notice.xml
  10. 2
      app/src/main/res/values-en-rUS/strings.xml
  11. 2
      app/src/main/res/values-zh-rCN/strings.xml
  12. 2
      app/src/main/res/values/strings.xml

@ -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'
}

@ -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<NoticeAdpter.PagerViewHolder> {
public class NoticeAdpter extends BannerAdapter<Notice, NoticeAdpter.PagerViewHolder> {
private List<Notice> noticeList;
public NoticeAdpter(List<Notice> noticeList) {
this.noticeList = noticeList;
public NoticeAdpter(List<Notice> 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<NoticeAdpter.PagerViewHol
holder.time.setText(context.getString(R.string.notice_time, DateFormat.format(context.getString(R.string.dateformat), date)));
}
@Override
public int getItemCount() {
return noticeList.size();
}
// ViewHolder需要继承RecycleView.ViewHolder
static class PagerViewHolder extends RecyclerView.ViewHolder {
class PagerViewHolder extends RecyclerView.ViewHolder {
//公告标题
private TextView title;
@ -74,6 +65,7 @@ public class NoticeAdpter extends RecyclerView.Adapter<NoticeAdpter.PagerViewHol
author = itemView.findViewById(R.id.notice_author);
time = itemView.findViewById(R.id.notice_time);
}
}
}

@ -1,23 +1,25 @@
package com.community.pocket.ui.main.ui.forum.main;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;
import com.community.pocket.R;
import com.community.pocket.data.adapter.NoticeAdpter;
import com.community.pocket.data.model.Notice;
import com.community.pocket.ui.main.MainFragment;
import com.community.pocket.ui.main.ui.share.Response;
import com.youth.banner.indicator.RectangleIndicator;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
@ -32,7 +34,7 @@ import java.util.List;
public class ForumFragment extends MainFragment {
@ViewInject(R.id.paper)
private ViewPager2 viewPager2;
private MyBanner banner;
@ViewInject(R.id.scroll_body)
private NestedScrollView nestedScrollView;
@ -40,6 +42,11 @@ public class ForumFragment extends MainFragment {
@ViewInject(R.id.forum_new)
private Button newButton;
@ViewInject(R.id.notice_prev)
private ImageView noticePrev;
@ViewInject(R.id.notice_next)
private ImageView noticeNext;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
@ -48,34 +55,75 @@ public class ForumFragment extends MainFragment {
viewModel.loadNotice();
newButton.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
_new(v);
newButton.setOnClickListener(this::_new);
//监控公告信息请求状态
viewModel.getNoticeDataResponse().observe(getViewLifecycleOwner(), forumNoticeResponse -> {
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<ForumNoticeResponse>() {
@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<Notice> notices) {
viewPager2.setAdapter(new NoticeAdpter(notices));
private void initNotice(final List<Notice> 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() {

@ -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<Notice, NoticeAdpter> {
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;
}
}

@ -4,9 +4,17 @@ package com.community.pocket.util;
* 参数传递Key
*/
public enum Param {
//重置密码用户
username,
//重置密码邮箱
email,
//论坛帖子id
forumId,
//访客预约是否外来人口
isOutPeople,
enable
//我的信息修改密码按钮是否启用
enable,
//公告滚动页数
notice_prev,
notice_next
}

@ -0,0 +1,9 @@
<vector android:height="100dp"
android:viewportHeight="1024"
android:viewportWidth="1024"
android:width="100dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="#323233"
android:pathData="M299.52,521.73c-3.28,-13.93 0.51,-29.18 11.37,-40.04l341.91,-341.91c16.69,-16.69 43.72,-16.69 60.31,0s16.69,43.72 0,60.31L401.31,512l311.91,311.91c16.69,16.69 16.69,43.72 0,60.31s-43.72,16.69 -60.31,0l-342.02,-341.91c-5.63,-5.63 -9.63,-12.8 -11.37,-20.58z" />
</vector>

@ -0,0 +1,9 @@
<vector android:height="100dp"
android:viewportHeight="1024"
android:viewportWidth="1024"
android:width="100dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="#323233"
android:pathData="M724.48,521.73c-1.84,7.78 -5.73,14.85 -11.37,20.48l-341.91,342.02c-16.69,16.69 -43.72,16.69 -60.31,0s-16.69,-43.72 0,-60.31L622.69,512 310.89,200.09c-16.69,-16.69 -16.69,-43.72 0,-60.31 16.69,-16.69 43.72,-16.69 60.31,0l341.91,341.91c10.85,10.85 14.64,26.11 11.37,40.04z" />
</vector>

@ -26,10 +26,41 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/paper"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content">
<ImageView
android:id="@+id/notice_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:alpha="0.1"
android:contentDescription="@string/notice_next"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@id/paper"
app:layout_constraintTop_toTopOf="@id/paper"
app:srcCompat="@drawable/ic_arrow_right" />
<ImageView
android:id="@+id/notice_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:alpha="0.1"
android:contentDescription="@string/notice_prev"
android:visibility="gone"
app:layout_constraintStart_toStartOf="@id/paper"
app:layout_constraintTop_toTopOf="@id/paper"
app:srcCompat="@drawable/ic_arrow_left" />
<com.community.pocket.ui.main.ui.forum.main.MyBanner
android:id="@+id/paper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"

@ -56,8 +56,10 @@
android:id="@+id/notice_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="end"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/notice_author" />

@ -220,4 +220,6 @@
<string name="no_score_history">no data</string>
<string name="change_score">score%1d</string>
<string name="score_notes">notes:\n%1s</string>
<string name="notice_prev">notice prev</string>
<string name="notice_next">notice next</string>
</resources>

@ -220,4 +220,6 @@
<string name="no_score_history">暂无历史信用分记录</string>
<string name="change_score">信用分%1d</string>
<string name="score_notes">备注信息:\n%1s</string>
<string name="notice_prev">上一条公告</string>
<string name="notice_next">下一条公告</string>
</resources>

@ -221,6 +221,8 @@
<string name="no_score_history">no data</string>
<string name="change_score">score%1d</string>
<string name="score_notes">notes:\n%1s</string>
<string name="notice_prev">notice prev</string>
<string name="notice_next">notice next</string>
<!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation -->

Loading…
Cancel
Save