You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.3 KiB
179 lines
5.3 KiB
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.ViewModelProvider;
|
|
|
|
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;
|
|
import org.xutils.view.annotation.ViewInject;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 论坛框架
|
|
*/
|
|
@ContentView(R.layout.forum)
|
|
public class ForumFragment extends MainFragment {
|
|
|
|
@ViewInject(R.id.paper)
|
|
private MyBanner banner;
|
|
|
|
@ViewInject(R.id.scroll_body)
|
|
private NestedScrollView nestedScrollView;
|
|
|
|
@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);
|
|
|
|
ForumViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumViewModel.class);
|
|
|
|
viewModel.loadNotice();
|
|
|
|
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;
|
|
});
|
|
}
|
|
|
|
//箭头点击事件
|
|
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(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() {
|
|
nestedScrollView.fling(0);
|
|
nestedScrollView.smoothScrollTo(0, 0);
|
|
}
|
|
|
|
//跳转热榜界面
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
@Event(value = R.id.forum_hot)
|
|
private void hot(View view) {
|
|
scrollTop();
|
|
ctrl().navigate(R.id.forumHotFragment);
|
|
changeColor(view);
|
|
}
|
|
|
|
//跳转最新界面
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
public void _new(View view) {
|
|
scrollTop();
|
|
ctrl().navigate(R.id.forumNewFragment);
|
|
changeColor(view);
|
|
}
|
|
|
|
|
|
//跳转发帖界面
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
@Event(value = R.id.forum_post)
|
|
private void post(View view) {
|
|
scrollTop();
|
|
ctrl().navigate(R.id.forumPostFragment);
|
|
changeColor(view);
|
|
}
|
|
|
|
//跳转我的帖子界面
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
@Event(value = R.id.forum_my)
|
|
private void my(View view) {
|
|
scrollTop();
|
|
ctrl().navigate(R.id.forumMyFragment);
|
|
changeColor(view);
|
|
}
|
|
|
|
@Override
|
|
protected int fragmentId() {
|
|
return R.id.fragment;
|
|
}
|
|
|
|
@Override
|
|
protected int[] button_ids() {
|
|
return new int[]{R.id.forum_hot, R.id.forum_new, R.id.forum_post, R.id.forum_my};
|
|
}
|
|
}
|
|
|