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.
97 lines
3.4 KiB
97 lines
3.4 KiB
package com.community.pocket.ui.main.ui.forum.hot;
|
|
|
|
import android.os.Bundle;
|
|
import android.util.TypedValue;
|
|
import android.view.Gravity;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.IdRes;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
import androidx.lifecycle.Observer;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import com.community.pocket.R;
|
|
import com.community.pocket.data.model.AbstractForumHot;
|
|
import com.community.pocket.data.model.Hot;
|
|
import com.community.pocket.ui.BaseFragment;
|
|
|
|
import org.xutils.view.annotation.ContentView;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 热门帖子
|
|
*/
|
|
@ContentView(R.layout.forum_hot_fragment)
|
|
public class ForumHotFragment extends BaseFragment {
|
|
|
|
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
ForumHotViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumHotViewModel.class);
|
|
|
|
viewModel.loadHot();
|
|
|
|
//加载热门信息
|
|
viewModel.getForumHotResponse().observe(getViewLifecycleOwner(), new Observer<ForumHotResponse>() {
|
|
@Override
|
|
public void onChanged(ForumHotResponse forumHotResponse) {
|
|
if (forumHotResponse == null) {
|
|
return;
|
|
}
|
|
|
|
if (forumHotResponse.getSuccess() != null) {
|
|
loadRank(forumHotResponse.getBody());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 加载热门信息
|
|
*
|
|
* @param hot 热门信息
|
|
*/
|
|
private void loadRank(Hot hot) {
|
|
addRank(R.id.active_user, R.id.active_user_text, hot.getUserHots());
|
|
addRank(R.id.hot_topic, R.id.hot_topic_text, hot.getTopicHots());
|
|
addRank(R.id.hot_events, R.id.hot_events_text, hot.getActiveHots());
|
|
}
|
|
|
|
/**
|
|
* @param constraintId 约束布局id
|
|
* @param textId 标题布局id
|
|
* @param ranks 排名数组
|
|
*/
|
|
private <T extends AbstractForumHot> void addRank(@IdRes int constraintId, @IdRes int textId, List<T> ranks) {
|
|
|
|
View view = getView();
|
|
if (view != null) {
|
|
ConstraintLayout layout = view.findViewById(constraintId);
|
|
int id = 0;
|
|
for (int i = 1; i <= ranks.size(); i++) {
|
|
|
|
TextView textView = new TextView(view.getContext());
|
|
textView.setText(view.getContext().getString(R.string.hot_rank, i, ranks.get(i - 1)));
|
|
//文本居中
|
|
textView.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
|
|
//字体大小
|
|
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
|
|
textView.setId(View.generateViewId());
|
|
//设置约束
|
|
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
layoutParams.topToBottom = i == 1 ? textId : id;
|
|
layoutParams.setMargins(0, 10, 0, 0);
|
|
textView.setLayoutParams(layoutParams);
|
|
layout.addView(textView);
|
|
id = textView.getId();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|