我有5列从零到无穷大的GridLayout。 如果数字是质数,我只想在GridLayoutManager / RecycleView中绘制一个正方形,如果此加载工作正常,但是如果我又绘制非质数,那为什么呢?我该如何解决?
下面的代码段很长,因此我事先表示歉意。
public class MainActivity extends AppCompatActivity {
int numberOfColumns = 5;
AdapterMyRecyclerView adapterMyRecyclerView;
int currentItems, totalItems, scrollOutItems;
Boolean isScrolling = false;
GridLayoutManager manager;
ProgressBar progressBar;
ArrayList list;
final String data[] = {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
"48",
"49"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList(Arrays.asList(data));
manager = new GridLayoutManager(this, numberOfColumns);
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
progressBar = (ProgressBar) findViewById(R.id.progress);
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapterMyRecyclerView = new AdapterMyRecyclerView(this, list);
recyclerView.setAdapter(adapterMyRecyclerView);
recyclerView.setLayoutManager(manager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItems = manager.getChildCount();
totalItems = manager.getItemCount();
scrollOutItems = manager.findFirstCompletelyVisibleItemPosition();
if ((isScrolling) && (currentItems + scrollOutItems == totalItems)) {
isScrolling = false;
fitchData(list.size());
}
}
});
//Toast.makeText(MainActivity.this, "size = " + list.size(), Toast.LENGTH_SHORT).show();
}
private void fitchData(final int size) {
progressBar.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for (int i = size; i < size + 25; i++) {
list.add(i + "");
adapterMyRecyclerView.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
}
}, 5000);
}
}
public class AdapterMyRecyclerView extends RecyclerView.Adapter < AdapterMyRecyclerView.ViewHolder > {
public static Integer longNumber;
public static Boolean isLongNum = false;
//private String[] mData;
private LayoutInflater mInflater;
//progress
ArrayList < String > dataProgressBar;
Context context;
AdapterMyRecyclerView(Context context, ArrayList < String > dataProgressBar) {
this.mInflater = LayoutInflater.from(context);
this.dataProgressBar = dataProgressBar;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.myTextView.setText(dataProgressBar.get(holder.getAdapterPosition()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context = v.getContext();
//Toast.makeText(context, "clicked at " + dataProgressBar.get(position), Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "sizeData = " + dataProgressBar.size(), Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "position = " + position, Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "holder.getAdapterPosition() = " + dataProgressBar.get(holder.getLayoutPosition()), Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "LongNumber = " + longNumber, Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
context = v.getContext();
longNumber = Integer.parseInt(dataProgressBar.get(position));
isLongNum = true;
getLongNumber(longNumber);
Toast.makeText(context, "Long clicked at " + dataProgressBar.get(position), Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "LongNumber = " + longNumber, Toast.LENGTH_SHORT).show();
holder.myTextView.setBackgroundColor(Color.BLUE);
/*for (int i=0 ; i<dataProgressBar.size(); i++){
Toast.makeText(context, "i = " + i, Toast.LENGTH_SHORT).show();
if(i % longNumber == 0){
//longNumber = Integer.parseInt(dataProgressBar.get(position));
//isLongNum=true;
holder.myTextView.setBackgroundColor(Color.YELLOW);
}
}*/
return false;
}
});
if (isPrime(position)) {
holder.myTextView.setBackgroundColor(Color.RED);
if (position == 0 || position == 1) {
holder.myTextView.setBackgroundColor(Color.WHITE);
}
}
//End onBindViewHolder()
}
//checks whether an int is prime or not.
boolean isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}
@Override
public int getItemCount() {
return dataProgressBar.size();
}
public void getLongNumber(int x) {
longNumber = x;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView myTextView;
public ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.info_text);
}
}
}