用gridlayoutmanager无法正确显示回收者视图数据

时间:2020-07-07 15:52:01

标签: android

Blockquote

“您好,我想用GridLayoutManager这样在回收器视图中显示数据,当单击下一个和上一个按钮时,项目应水平滚动,并且项目显示如下图所示。感谢andvance” enter image description here

Blockquote

我的视图显示如下 enter image description here

enter code here
package com.triviamaker.Adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.triviamaker.Activities.GameFeaturesActivity;
import com.triviamaker.Others.Constants;
import com.triviamaker.R;

import java.util.List;

public class WofAllRoundTeamScoreAdapter extends RecyclerView.Adapter<WofAllRoundTeamScoreAdapter.AllRoundView> {
    Context context;
    int count = 0;
    List<List<String>> scores;
    List<String> teamNames;
    int teamCount = 0;


    public WofAllRoundTeamScoreAdapter(Context context, List<List<String>> scores, List<String> teamNames) {
        this.context = context;
        this.teamNames = teamNames;
        this.scores = scores;
        teamCount = scores.get(0).size() + 1;
        count = ((scores.size()) + 1) * (scores.get(0).size() + 1);
    }

    @NonNull
    @Override
    public AllRoundView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.all_round_team_score_item, null);
//        int height = parent.getHeight() / 6;
//        int width = parent.getWidth() / 4;
//        v.setMinimumHeight(height);
//        v.setMinimumWidth(width);
        v.setLayoutParams(new ViewGroup.LayoutParams((parent.getWidth() / 4), parent.getWidth() / 5)); /// THIS LINE WILL DIVIDE OUR VIEW INTO NUMBERS OF PARTS
        return new AllRoundView(v);
    }

    @Override
    public void onBindViewHolder(@NonNull AllRoundView holder, int position) {
        holder.setIsRecyclable(false);
        int pos = holder.getAdapterPosition();
        int quotient = (pos / teamCount);
        if (pos == 0) {
            holder.scoreText.setVisibility(View.GONE);
        } else if (pos < teamCount) {
            float[] corRadious = new float[]{10, 10, 10, 10, 0, 0, 0, 0};
            Constants.setTextBackgroundDrawable(holder.scoreText, corRadious, ContextCompat.getColor(context, R.color.transparent_background));
            holder.scoreText.setText(teamNames.get(pos - 1));
        } else if (pos >= teamCount && (pos % teamCount) == 0) {
            float[] corRadii = new float[]{10, 10, 0, 0, 0, 0, 10, 10};
            Constants.setTextBackgroundDrawable(holder.scoreText, corRadii, ContextCompat.getColor(context, R.color.transparent_background));
            holder.scoreText.setText("Round " + quotient);
        } else
            holder.scoreText.setText("$0");

    }

    @Override
    public int getItemCount() {
        return count;
    }

    class AllRoundView extends RecyclerView.ViewHolder {
        TextView scoreText;

        public AllRoundView(@NonNull View itemView) {
            super(itemView);
            scoreText = itemView.findViewById(R.id.allTeamScoreTxt);
        }
    }
}

Blockquote

并这样设置回收站视图

enter code here

AllRoundItemSpaceDecoration decoration = new AllRoundItemSpaceDecoration(5, totalTeam);
        decoration.setIsGridLayout(true);
        allRoundTeamScoreRecyclerView.setHasFixedSize(true);
        allRoundTeamScoreRecyclerView.addItemDecoration(decoration);
        CustomGridLayoutManager customGridLayoutManager = new 
      CustomGridLayoutManager(WheelStyleActivity.this, totalTeam + 1, LinearLayoutManager.VERTICAL, false);
        customGridLayoutManager.setScrollEnabled(true);
        customGridLayoutManager.setScrollEnabledHorizontal(true);

        allRoundTeamScoreRecyclerView.setLayoutManager(customGridLayoutManager);

0 个答案:

没有答案
相关问题