为什么我的回收者观点在项目之间有如此大的距离?

时间:2019-12-11 15:08:24

标签: java android android-recyclerview

我以前使用过回收站视图,但从来没有这个问题。我所做的其他回收者视图之间的主要区别是,我为此使用了SQLite。
我希望所有项目在彼此之间出现并且彼此之间没有空格。

我初始化回收站的排行榜活动

public class LeaderBoard extends AppCompatActivity {


    private RecyclerView recyclerView;
    private RecyclerController mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leader_board);
        DaoSQLite manager=new DaoSQLite(this);
        SQLiteDatabase db=manager.getReadableDatabase();
        layoutManager=new LinearLayoutManager(this);
        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter=new RecyclerController(manager.getSortScores());
        recyclerView.setAdapter(mAdapter);
    }
}

查看回收站

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/txtNameOption"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/lbName"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="21dp"
        android:fontFamily="serif"
        android:text="Name"
        android:textAlignment="textStart"
        android:textAppearance="@style/TextAppearance.AppCompat.Display2"
        android:textIsSelectable="false"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/lbLevel2"
        android:layout_width="109dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="154dp"
        android:layout_marginTop="21dp"
        android:fontFamily="serif"
        android:text="Level"
        android:textAlignment="textStart"
        android:textAppearance="@style/TextAppearance.AppCompat.Display2"
        android:textIsSelectable="false"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/lbScore"
        android:layout_width="132dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="266dp"
        android:layout_marginTop="21dp"
        android:fontFamily="serif"
        android:text="Score"
        android:textAlignment="textStart"
        android:textAppearance="@style/TextAppearance.AppCompat.Display2"
        android:textIsSelectable="false"
        android:textSize="15sp"
        android:textStyle="bold" />

    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="79dp"
        android:background="#604697"
        android:backgroundTint="#34BE3838"
        android:backgroundTintMode="src_in"
        android:hapticFeedbackEnabled="false" />

</RelativeLayout>

这是SQLite DAO

package com.example.quizappjava.DataBase;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.quizappjava.Beans.User;


public class DaoSQLite extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Game.db";
    private static final String TABLE_NAME_SCORES = "puntuaciones";
    private SQLiteDatabase sqLiteDatabase = getWritableDatabase();

    public DaoSQLite(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+ TABLE_NAME_SCORES +" (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT (15) NOT NULL,score INTEGER NOT NULL, level INTEGER NOT NULL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public Cursor getSortScores(){
        return getReadableDatabase().query(TABLE_NAME_SCORES,null,null,null,null,null,"score");
    }
    public void insertNewScore(User user){
        sqLiteDatabase.insert(TABLE_NAME_SCORES,null,user.toContentValues());
    }

}

回收控制器

package com.example.quizappjava.JavaClasses;

import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.quizappjava.Beans.User;
import com.example.quizappjava.R;

import java.util.ArrayList;

public class RecyclerController extends RecyclerView.Adapter<RecyclerController.ViewHolder> {

    private ArrayList<User> userArraList = new ArrayList<User>();



    public RecyclerController(Cursor queryRequest) {
        ParseData(queryRequest);
    }

    private void ParseData(Cursor queryRequest) {
        for (queryRequest.moveToFirst(); !queryRequest.isAfterLast(); queryRequest.moveToNext()) {
            userArraList.add(
                    new User(
                            queryRequest.getString(queryRequest.getColumnIndex("name")),
                            queryRequest.getInt(queryRequest.getColumnIndex("score")),
                            queryRequest.getInt(queryRequest.getColumnIndex("level"))
                    ));
        }

    }

    @NonNull
    @Override
    public RecyclerController.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerController.ViewHolder holder, int position) {

        holder.user=userArraList.get(position);

        holder.lbName.setText(userArraList.get(position).getName());
        holder.lbScore.setText(userArraList.get(position).getScore()+"");
        holder.lbLevel.setText(userArraList.get(position).getLevel()+"");
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView lbName;
        private TextView lbScore;
        private TextView lbLevel;
        private User user;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            lbName=itemView.findViewById(R.id.lbName);
            lbLevel=itemView.findViewById(R.id.lbLevel2);
            lbScore=itemView.findViewById(R.id.lbScore);

        }


    }
}

此图像显示了我在执行应用程序时如何看到排行榜,第一个图像是我看到一个项目的方式,第二个图像是两个项目之间的空白

When i see one item i cant see the rest

Separation between 2 items on the recycler

3 个答案:

答案 0 :(得分:1)

尝试 编辑此内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/txtNameOption"
android:layout_width="match_parent"
android:layout_height="match_parent">

收件人:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/txtNameOption"
android:layout_width="match_parent"
android:layout_height="wrap_content">

答案 1 :(得分:1)

您应该说明并解决here的问题。

传递到回收站的相对布局具有android:layout_height="match_parent,但应具有android:layout_height="wrap_content,否则每个项目的高度都将与RecylerView相同。

答案 2 :(得分:1)

您必须调整recyclerview的单元格高度以包装内容,而不是匹配父项:

查看回收机

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/txtNameOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

match_parent高度会将卖出比例缩放到父视图的高度