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.

113 lines
4.3 KiB

package com.community.pocket.ui.main.ui.forum;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SearchView;
import androidx.core.widget.NestedScrollView;
import com.community.pocket.R;
import com.community.pocket.data.model.Forum;
import com.community.pocket.data.model.Page;
import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.main.ui.forum.data.ForumDataActivity;
import com.community.pocket.ui.main.ui.forum.main.ForumFragment;
import com.community.pocket.util.Param;
import org.xutils.view.annotation.ViewInject;
public abstract class ForumPost extends BaseFragment {
//显示发帖人
protected abstract int showAuthor();
//显示审核状态
protected abstract int showStatus();
@ViewInject(R.id.post_layout)
private LinearLayout layout;
/**
* 加载帖子数据
*/
protected void loadPost(Page<Forum> forumList) {
if (forumList.getCurrentPage() == 1) {
layout.removeAllViews();
}
for (int i = 0; i < forumList.getList().size(); i++) {
final Forum forum = forumList.getList().get(i);
View childView = View.inflate(getContext(), R.layout.post, null);
TextView status = childView.findViewById(R.id.check_status);
status.setVisibility(showStatus());
switch (forum.getStatus()) {
case ok:
status.setText(R.string.forum_status_ok);
break;
case fail:
status.setText(R.string.forum_status_fail);
break;
case uncheck:
status.setText(R.string.forum_status_uncheck);
break;
}
TextView title = childView.findViewById(R.id.post_title);
title.setText(forum.getTitle());
TextView content = childView.findViewById(R.id.post_content);
content.setText(forum.getContent());
TextView author = childView.findViewById(R.id.poster);
author.setVisibility(showAuthor());
author.setText(getString(R.string.poster, forum.getUsername()));
TextView postReply = childView.findViewById(R.id.post_reply);
postReply.setText(getString(R.string.post_reply, forum.getReply()));
TextView time = childView.findViewById(R.id.post_time);
time.setText(getString(R.string.post_time, formatUnix(forum.getTime())));
ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layoutParams.setMargins(0, 0, 0, 50);
childView.setLayoutParams(layoutParams);
//打开帖子
Button button = childView.findViewById(R.id.showAll);
button.setOnClickListener(v -> {
Intent intent = new Intent(getContext(), ForumDataActivity.class);
intent.putExtra(Param.forumId.name(), forum.getId());
startActivityForResult(intent, 1);
});
layout.addView(childView);
}
}
protected abstract FormLoad getViewModel();
protected abstract Long getCurrentPage();
protected abstract SearchView searchView();
protected void load(Long page) {
getViewModel().loadForumNew(page, searchView().getQuery().toString());
}
//滚动加载更多帖子
@RequiresApi(api = Build.VERSION_CODES.M)
protected void loadMore() {
//滚动加载更多帖子
ForumFragment forumFragment = getParentFragment(ForumFragment.class);
forumFragment.getScrollView().setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
load(getCurrentPage() + 1);
}
});
}
}