代码之家  ›  专栏  ›  技术社区  ›  user8412632

缺少RecyclerView的第一项

  •  2
  • user8412632  · 技术社区  · 6 年前

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

    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中。我的日志中没有错误。任何帮助都将不胜感激

    6 回复  |  直到 6 年前
        1
  •  5
  •   Lalit Jadav    6 年前

    你做得不对。。。。

    您正在中添加五项 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");
            }
        }
    

    但您需要以这种方式验证空列表

        2
  •  3
  •   iamgopal    6 年前

    您的回收器视图可能隐藏在工具栏下。给出56dp的上边距。

    android:layout_marginTop="56dp"
    
        3
  •  2
  •   Ratilal Chopda    6 年前

    试试这个,你的第一个职位就是设定你的头衔。

    使用此

    @Override
    public int getItemViewType(int position) {
        return TYPE_LIST;
    }
    

    而不是这个

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return TYPE_HEAD;
        }
        return TYPE_LIST;
    }
    
        4
  •  1
  •   Brakja Manaki    4 年前

    对我起作用的是,首先我从数组中删除任何空值(因为第一个元素可以是空的),然后再次添加空值。这样我就避免了重复null值。我不使用“if”或任何适配器实现的方法,所以我让我的应用程序更高效。 我的代码如下:

    myArray.remove(null); // here I remove any preview added null value as first (0 position) element
    myArray.add(0, null); //now I add null as first element
    //this way I am avoiding showing content with a null values
    

    我希望这对某人有帮助

        5
  •  0
  •   Rosário Pereira Fernandes Aabid Ansar    6 年前

    吐司会显示,因为您的卡片在那里,但它隐藏在工具栏中。您可以在RecyclerView中添加一些边距:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
        xmlns:app="schemas.android.com/apk/res-auto"
        xmlns:tools="schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
        <android.support.v7.widget.RecyclerView
            android:layout_marginTop:"?android:attr/actionBarSize" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/recyclerView" />
    </RelativeLayout>
    
        6
  •  0
  •   Hitesh Modhwadia    6 年前

    如果您想添加标题而不从活动中传递第一个空白数据,则需要在适配器内的第一个位置添加空白条目,如下所示。

    在适配器中替换此代码:

              public RecordingsAdapter(ArrayList<Recording>  recordingArrayList) {
    
               this.recordingArrayList = recordingArrayList;
               this.recordingArrayList.add(0,null);
    
              }