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

android recyclerview缺少的项目存在较大差距

  •  1
  • Pushkar  · 技术社区  · 6 年前

    我有从sqlite数据库加载数据的Recycler视图。 打开“活动”时,在“回收器”视图中没有加载任何内容,但在滚动时,半个数据是可见的。再次进入顶部项目是可见的,下面有很大的间隙。缺口出现在缺少项目的地方。请参见 video.>

    <

    正如您所看到的,数据库中的tupperware一词不在recyclerview中。在这一点上,可以看到一个很大的缺口。

    来自适配器

    非空 @覆盖 public historyrecycleholder onCreateViewholder(@nonnull viewgroup parent,int viewtype){ 视图视图=layoutInflator.From(parent.getContext()) .充气(r.layout.history_recycler_item,parent,false); 返回新的历史记录回收文件夹(视图); } @覆盖 public void onbindviewholder(@nonnull historyrecycleholder holder,int position){ final string txt=wordlist.get(位置); holder.historyword.settext(txt); holder.wordConstrain.setOnclickListener(new view.onclickListener()){ @覆盖 公共void onclick(视图){ 意向意向=新意向(context,wordactivity.class); intent.putextra(intent key.history_key,txt); context.startactivity(意图); } (}); }

    recycleView.viewholder

    public class historyRecycleholder extends recyclerview.viewholder{
    
    公共文本查看历史记录单词;
    公共约束布局词约束;
    
    公共历史记录回收文件夹(查看项视图){
    super(项目视图);
    history word=itemview.findviewbyid(r.id.history\u word);
    wordconstran=itemview.findviewbyid(r.id.history_recycler_constran);
    }
    }
    

    历史回收商\u item.xml

    <?xml version=“1.0”encoding=“utf-8”?gt;
    <android.support.constraint.constraintlayout(限制布局)
    xmlns:android=“http://schemas.android.com/apk/res/android”
    xmlns:app=“http://schemas.android.com/apk/res-auto”
    xmlns:tools=“http://schemas.android.com/tools”
    android:id=“@+id/历史记录\u回收器\u约束”
    android:layout_width=“匹配父级”
    android:layout_height=“wrap_content”>
    
    <文本视图
    android:id=“@+id/历史\u字”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:background=“@color/cardview_light_background”
    安卓:paddingbottom=“5dp”
    安卓:paddingend=“10dp”
    android:paddingstart=“10dp”
    安卓:paddingtop=“5dp”
    安卓:textsize=“24sp”
    安卓:textstyle=“bold”
    app:layout_constraintBottom_tobottomof=“父级”
    app:layout_constrainted_toendoff=“父级”
    app:layout_constraintStart_toStartof=“父级”
    app:layout_constraintop_totopof=“父级”
    工具:text=“@string/word”/>
    </android.support.constraint.constraintlayout>
    

    来自活动

    historyList=new arrayList<gt;();
    HistoryAdapter=新的HistoryAdapter(HistoryList,this);
    
    RecyclerView.LayoutManager布局管理器=
    新建LinearLayoutManager(getApplicationContext());
    
    recyclerview.setlayoutmanager(布局管理器);
    recyclerView.setItemAnimator(new defaultItemAnimator());
    RecyclerView.AdditemDecoration(新的DivideritemDecoration(this,
    linearlayoutmanager.vertical));
    recyclerview.setadapter(历史适配器);
    recyclerview.sethasFixedSize(真);
    
    viewGroup.layoutParams params=recyclerview.getlayoutParams();
    params.height=(最大高度/2);
    recyclerview.setlayoutParams(参数);
    

    数据库数据通过AsyncLoader加载

    @nullable
    @覆盖
    public arraylist<string>loadinbackground()。{
    
    返回database.getwordsinSearchHistory();
    }
    

    数据库方法

    public arraylist<string>getwordsinSearchHistory()。{
    arraylist<string>list=new arraylist<>();
    
    sqliteDatabase database=this.getreadabledatabase();
    
    字符串查询=
    “select*from”+搜索\历史记录\表
    +“order by”+搜索\u历史记录\u表格\u列\u id+“desc”;
    
    cursor cursor=database.rawquery(查询,空);
    cursor.moveToFirst();
    而(!)cursor.isafterlast()){
    list.add(cursor.getString(cursor.getColumnIndex(search_history_table_column_word));
    cursor.movetonext();
    }
    cursor.close();
    退货清单;
    }
    video.

    Device ScreenshotData in Database

    正如您所看到的,数据库中的tupperware一词不在recyclerview中。取而代之的是一个巨大的缺口。

    从适配器

    @NonNull
    @Override
    public HistoryRecycleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.history_recycler_item, parent, false);
    
        return new HistoryRecycleHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull HistoryRecycleHolder holder, int position) {
    
        final String txt = wordList.get(position);
    
        holder.historyWord.setText(txt);
    
        holder.wordConstrain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, WordActivity.class);
                intent.putExtra(IntentKeys.HISTORY_KEY, txt);
                context.startActivity(intent);
            }
        });
    
    }
    

    recycleView.viewholder

    public class HistoryRecycleHolder extends RecyclerView.ViewHolder {
    
        public TextView historyWord;
        public ConstraintLayout wordConstrain;
    
        public HistoryRecycleHolder(View itemView) {
            super(itemView);
            historyWord = itemView.findViewById(R.id.history_word);
            wordConstrain = itemView.findViewById(R.id.history_recycler_constrain);
        }
    }
    

    历史记录回收器\u item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/history_recycler_constrain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/history_word"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/cardview_light_background"
            android:paddingBottom="5dp"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:paddingTop="5dp"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/word" />
    </android.support.constraint.ConstraintLayout>
    

    从活动中

            historyList = new ArrayList<>();
            historyAdapter = new HistoryAdapter(historyList, this);
    
            RecyclerView.LayoutManager layoutManager =
                    new LinearLayoutManager(getApplicationContext());
    
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(this,
                    LinearLayoutManager.VERTICAL));
            recyclerView.setAdapter(historyAdapter);
            recyclerView.setHasFixedSize(true);
    
            ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
            params.height = (MaxHeight / 2);
            recyclerView.setLayoutParams(params);
    

    数据库数据通过AsyncLoader加载。

    @Nullable
    @Override
    public ArrayList<String> loadInBackground() {
    
            return database.getWordsInSearchHistory();
    }
    

    数据库方法

    public ArrayList<String> getWordsInSearchHistory() {
            ArrayList<String> list = new ArrayList<>();
    
            SQLiteDatabase database = this.getReadableDatabase();
    
            String query =
                    "SELECT * FROM " + SEARCH_HISTORY_TABLE
                            + " ORDER BY " + SEARCH_HISTORY_TABLE_COLUMN_ID + " DESC";
    
            Cursor cursor = database.rawQuery(query, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                list.add(cursor.getString(cursor.getColumnIndex(SEARCH_HISTORY_TABLE_COLUMN_WORD)));
                cursor.moveToNext();
            }
            cursor.close();
            return list;
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Ridcully    6 年前

    唯一让我觉得奇怪的是,你以编程方式更改了recylwinew的布局参数。你能试着把那个部件拆下来看看它是否有什么变化吗?