gpt4 book ai didi

android - RecyclerView 的第一项丢失

转载 作者:太空狗 更新时间:2023-10-29 15:58:44 26 4
gpt4 key购买 nike

我有一个 Activity 。我正在创建一个文件,该文件在创建 Activity 时包含一些数据。正确创建所有文件并正确写入数据。以下是我的代码

public class MyRecordingsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecordingsAdapter recordingsAdapter;
private ArrayList<Recording> recordingArrayList;
private File userRecordingFile;
private static final String USER_MIX_DIR = "UserMix";
private String lines[]=new String[]{};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_recordings);


for(int i=0;i<5;i++){
try{
userRecordingFile = new File(createRecordingFiles(), "Recording"+i+".txt");
FileWriter writer = new FileWriter(userRecordingFile);
writer.append("DEF"+i+"\nHIJ "+i);
writer.flush();
writer.close();
}
catch (Exception e){
e.printStackTrace();
}

}

getSupportActionBar().hide();

recordingArrayList=new ArrayList<>();
recyclerView=findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);

readFiles();


Toast.makeText(getApplicationContext(),lines[0],Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),lines[1],Toast.LENGTH_SHORT).show();
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[0],lines[1]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[2],lines[3]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[4],lines[5]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[6],lines[7]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[8],lines[9]));

recordingsAdapter=new RecordingsAdapter(recordingArrayList);
recyclerView.setAdapter(recordingsAdapter);
}


public File createRecordingFiles() {

File dirRoot = getExternalCacheDir();

File workDir = new File(dirRoot, USER_MIX_DIR);
//Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_SHORT).show();
if (!workDir.exists()) {
workDir.mkdirs();

File recordingFile = new File(workDir, "Recording File ");
try {
recordingFile.createNewFile();
} catch (IOException e) {

}
}
return workDir;
}


public void readFiles(){
StringBuilder text = new StringBuilder();
BufferedReader br=null;
try {
File dirRoot = getExternalCacheDir();
File workDir = new File(dirRoot, USER_MIX_DIR);

for(int i=0;i<5;i++){
File file = new File(workDir,"Recording"+i+".txt");

br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
lines=text.toString().split("\n");

}
}


br.close() ;
}catch (IOException e) {
e.printStackTrace();
}


}

}

我写的 Toast 正确显示了 DEF0 和 HIJ 0,但它们没有显示在 recyclerview 中。以下是屏幕截图

enter image description here

以下是我的适配器类

public class RecordingsAdapter extends RecyclerView.Adapter<RecordingsAdapter.RecyclerViewHolder> {
public static final int TYPE_HEAD=0;
public static final int TYPE_LIST=1;
private ArrayList<Recording> recordingArrayList;

public RecordingsAdapter(ArrayList<Recording> recordingArrayList) {
this.recordingArrayList = recordingArrayList;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
RecyclerViewHolder recyclerViewHolder;

if(viewType == TYPE_LIST){
view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_my_recordings,parent,
false);
recyclerViewHolder=new RecyclerViewHolder(view,viewType);
return recyclerViewHolder;

}else if(viewType == TYPE_HEAD){
view= LayoutInflater.from(parent.getContext()).inflate(R.layout.head_layout,parent,
false);
recyclerViewHolder=new RecyclerViewHolder(view,viewType);
return recyclerViewHolder;
}
return null;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Recording recording;
if(holder.view_type==TYPE_LIST){
recording=recordingArrayList.get(position);
holder.imageView.setImageResource(recording.getImage_id());
holder.typeTextView.setText(recording.getTitle());
holder.dateTimeTextView.setText(recording.getDatetime());

}else if (holder.view_type == TYPE_HEAD){
holder.typeHeaderTextView.setText("TYPE");
holder.titleHeaderTextView.setText("TITLE");
holder.dateTimeHeaderTextView.setText("DATE/TIME");
}
}

@Override
public int getItemCount() {
return recordingArrayList.size();
}

@Override
public int getItemViewType(int position) {
if(position==0){
return TYPE_HEAD;
}
return TYPE_LIST;
}

public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
int view_type;
ImageView imageView;
TextView typeTextView,dateTimeTextView;
TextView typeHeaderTextView,titleHeaderTextView,dateTimeHeaderTextView;

public RecyclerViewHolder(View itemView, int viewType) {
super(itemView);
if(viewType==TYPE_LIST){
typeTextView=itemView.findViewById(R.id.tv_cell_recording_recording_name);
imageView=itemView.findViewById(R.id.iv_cell_recordings);
dateTimeTextView=itemView.findViewById(R.id.tv_cell_recording_date_time);
view_type=1;
}else if(viewType==TYPE_HEAD){
typeHeaderTextView=itemView.findViewById(R.id.tv_type_head_layout);
titleHeaderTextView=itemView.findViewById(R.id.tv_title_head_layout);
dateTimeHeaderTextView=itemView.findViewById(R.id.tv_date_time_head_layout);
view_type=0;
}

}
}
}

DEf0 和 HIJ 0 没有显示在 recyclerview 中。我不明白为什么它们没有显示在我的回收站 View 中。我的日志中没有错误。任何帮助将不胜感激

最佳答案

你做错了....

您要在 recyclerview 中添加五个项目,但首先要考虑的是标题。

所以这会将您的第一个项目作为标题,您会看到其他四个项目。

您需要做的是,将虚拟项目作为第一个项目传递,然后添加您的 5 个项目。

这样你就可以解决你的问题。

你可以这样试试:

recordingArrayList.add(new Recording(R.drawable.ic_launcher,"", ""));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[0],lines[1]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[2],lines[3]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[4],lines[5]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[6],lines[7]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[8],lines[9]));

或者你也可以按照下面的方式进行:

更改适配器代码

@Override
public int getItemCount() {
return recordingArrayList.size() + 1;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Recording recording;
if(holder.view_type==TYPE_LIST){
recording=recordingArrayList.get(position - 1);
...

}else if (holder.view_type == TYPE_HEAD){
holder.typeHeaderTextView.setText("TYPE");
holder.titleHeaderTextView.setText("TITLE");
holder.dateTimeHeaderTextView.setText("DATE/TIME");
}
}

但是你需要用这种方式验证空列表

关于android - RecyclerView 的第一项丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com