java - Android Studio : Items in RecycleView "jumps" to the left

标签 java android

我正在研究 RecycleView,但遇到了一些奇怪的事情。我正在编写一些简单的应用程序,这是 RecycleView 中自定义行的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">
        <TextView
            android:id="@+id/TextViewGameName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FirstTextView"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:id="@+id/TextViewPrices"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SecondTextView"
            android:textColor="@android:color/holo_blue_dark"/>
    </LinearLayout>


    <Button
        android:id="@+id/ButtonRemove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="usuń"
        android:layout_weight="0">

    </Button>

在显示屏上,看起来还不错:enter image description here

但是每当我启动这个应用程序时,就会出现问题,如下所示: enter image description here

由于某种原因,按钮跳到了左侧,我不知道为什么。除了布局之外,我不会在这里发布任何代码,因为我认为它与问题本身没有任何关系,但如果有必要,我可以附加它。预先感谢您的帮助。

编辑: 我的适配器代码:

package com.ja.myboosterproject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

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

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView gameNameTextView;
    public TextView pricesTextView;
    public Button removeButton;

    public ViewHolder(View itemView) {
        super(itemView);
        gameNameTextView = (TextView) itemView.findViewById(R.id.TextViewGameName);
        pricesTextView = (TextView) itemView.findViewById(R.id.TextViewPrices);
        removeButton = (Button) itemView.findViewById(R.id.ButtonRemove);
    }
}

private List<com.ja.myboosterproject.Booster> mBoosters;
private Context mContext;

public BoosterAdapter(Context context, List<Booster> boosters){
    mContext = context;
    mBoosters = boosters;
}

private Context getContext(){
    return mContext;
}

@Override
public BoosterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    View contentView = inflater.inflate(R.layout.booster_list_row, parent, false);
    ViewHolder viewHolder = new ViewHolder(contentView);
    return viewHolder;
}

@Override
public void onBindViewHolder(BoosterAdapter.ViewHolder viewHolder, int position){
    Booster booster = mBoosters.get(position);
    viewHolder.gameNameTextView.setText(booster.getGameName());
    viewHolder.pricesTextView.setText(booster.getPriceBought() + " - " + booster.getPriceSold());
}

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

}

最佳答案

好吧,我终于明白了。感谢@Eugen Pechanec(我希望我知道如何正确提及你),因为布局检查器向我表明存在问题。这就是我的回收 View 本身的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/layout_boosters"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

我使用了ContraintLayout,一切看起来都不错。但是当我检查布局检查器时,出现了问题: enter image description here 即使所有 LinearLayout 都设置为“match_parent”,仍有大片区域未使用空间。所以我决定将 ConstraintLayout 更改为 LinearLayout ,宾果游戏,它成功了。我不知道为什么RecycleView不想与ConstraintLayout合作,我希望有人解释一下,但我的问题已经解决了。

编辑:找到解决方案: 实际上,很多人都有同样的问题,在这里我找到了允许 ConstraintLayout 和 RecyclerView 一起工作的解决方案:https://stackoverflow.com/a/47673523/8786284

关于java - Android Studio : Items in RecycleView "jumps" to the left,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48774273/

相关文章:

java - AES 在 android 中使用 Base64 key 加密

android - 如何使用 OpenGLES 2.0 在 libgdx 的背景上实时渲染 Android 的 YUV-YV12 相机图像?

java - 如何从命令行安装 pom 文件中提到的插件,而不编译或构建整个项目?

java - 按下主页按钮时如何取消计时器?

java - Storm redis spout元组无一异常(exception)地丢失

java - JOptionPane 出现问题并使其正常工作

android - 有没有办法自动化 Android SDK 安装?

android - Android 中的 TextToSpeech API 是否需要互联网连接才能工作?

android - Android 的 Sqlite 语法错误

java - Android - loopJ AsyncHttpClient 返回响应 onFinish 或 onSuccess