java - 将 RecyclerView 显示到片段中
问题描述
再会,
我正在尝试在应用程序的片段中显示 RecyclerView。但是每次我运行该应用程序时它都会崩溃。据我了解,teamList 在 MainActivity 上存在问题,我需要将与 teamList 相关的所有内容都带到 KomandosFragment 的 onCreate 方法中。但是每次我尝试这样做时,它都是行不通的。
任何帮助,将不胜感激。
编辑:GitHub https://github.com/Kirkoglu/Meginimas中的完整代码
主要活动:
package com.anzelmasfile.meginimas;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class MainActivity extends AppCompatActivity {
RecyclerView teamList;
String s1[], s2[];
int images[] = {R.drawable.gargzdai, R.drawable.jonava, R.drawable.joniskis, R.drawable.kaunas,
R.drawable.klaipeda, R.drawable.marijampole, R.drawable.mazeikiai, R.drawable.moletai,
R.drawable.palanga, R.drawable.sakiai, R.drawable.silale, R.drawable.silute,
R.drawable.taurage, R.drawable.telsiai, R.drawable.vilnius};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
// teamList = findViewById(R.id.teamList);
// s1 = getResources().getStringArray(R.array.komandos);
// s2 = getResources().getStringArray(R.array.treneriai);
//
// KomandosAdapter komandosAdapter = new KomandosAdapter(this, s1, s2, images);
//
// teamList.setAdapter(komandosAdapter);
// teamList.setLayoutManager(new LinearLayoutManager(this));
ViewPager2 viewPager2 = findViewById(R.id.viewPager);
viewPager2.setAdapter(new OrdersPagerAdapter(this));
final TabLayout tabLayout = findViewById(R.id.tabLayout);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0: {
tab.setText("Pradinis");
tab.setIcon(R.drawable.pradinis);
break;
}
case 1: {
tab.setText("Komandos");
tab.setIcon(R.drawable.komandos);
break;
}
case 2: {
tab.setText("Tvarkaraštis");
tab.setIcon(R.drawable.tvarkarastis);
break;
}
case 3: {
tab.setText("Rezultatai");
tab.setIcon(R.drawable.rezultatai);
break;
}
case 4: {
tab.setText("Lentelė");
tab.setIcon(R.drawable.lentele);
break;
}
}
}
});
tabLayoutMediator.attach();
}
}
Komandos适配器:
package com.anzelmasfile.meginimas;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class KomandosAdapter extends RecyclerView.Adapter<KomandosAdapter.KomandosViewHolder> {
String data1[], data2[];
int images[];
Context context;
public KomandosAdapter(Context ct, String s1[], String s2[], int img[]) {
context = ct;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public KomandosViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_komandos, parent, false);
return new KomandosViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull KomandosViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return 15;
}
public static class KomandosViewHolder extends RecyclerView.ViewHolder {
TextView myText1, myText2;
ImageView myImage;
KomandosViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.teamName);
myText2 = itemView.findViewById(R.id.teamCoach);
myImage = itemView.findViewById(R.id.teamLogo);
}
}
}
KomandosFragment:
package com.anzelmasfile.meginimas;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class KomandosFragment extends Fragment {
public KomandosFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_komandos, container, false);
}
}
解决方案
推荐阅读
- mysql - 为什么设置新的 MySQL 数据库时会显示错误?
- spring-security - 如何从 Spring Security 中删除 ROLE_ 前缀?
- css - 背景的 CSS 层 - 使用颜色不透明度的上层
- python - OpenCV:找不到大矩形轮廓
- node.js - @google-cloud/logging-winston Express 中间件请求捆绑问题
- ios - 放大 UI 元素会导致它在 iOS 上变得不清晰/像素化
- python - 从分类类别中绘制多个图像
- python - Matplotlib 没有在水平条形图上绘制几个点?
- java - 在 Java 中拒绝 HTTP 服务器上的 SSL 连接
- google-cloud-platform - GCP 日志记录中的 APIGEE