【问题标题】:recyclerview items not scrolling?recyclerview 项目不滚动?
【发布时间】:2018-11-01 19:11:46
【问题描述】:

我正在制作 android cv 应用程序,并且我已经实现了 RecyclerView,但即使项目没有滚动,我也使用了 RecyclerViewLayout Manager。

below my SkillsAdapter.class

公共类 SkillsAdapter 扩展 RecyclerView.Adapter { 公共列表技能列表; 公共上下文上下文;

public SkillsAdapter(List<Skill> skillList, Context context) {
    this.skillList = skillList;
    this.context = context;

}

@NonNull
@Override
public SkillsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context)
            .inflate(R.layout.skills_item, parent, false);  // change

    return new SkillsAdapter.ViewHolder(itemView);
}

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

    Skill skill = skillList.get(position);
    holder.programming.setText(skill.getProgramming());
    holder.framework.setText(skill.getFrameworkLibraries());
    holder.architecture.setText(skill.getAndroidArchitectureComponents());
    holder.software.setText(skill.getSoftwareMethodologies());
    holder.ide.setText(skill.getIDES());




}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView programming, framework, architecture, software, ide;

    public ViewHolder(View view) {
        super(view);

        programming= (TextView) view.findViewById(R.id.programming);
        framework = (TextView) view.findViewById(R.id.framework);
        architecture = (TextView) view.findViewById(R.id.architecture);
        software = (TextView) view.findViewById(R.id.software);
        ide    = (TextView)view.findViewById(R.id.ide);
    }
}

}

在我的 SkillsItem.java 类下面

公共类 SkillItem 扩展 AppCompatActivity {

private SkillsAdapter skillsAdapter;
public List<Skill> skillList;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.skills);

    KitabInterface kitabInterface = ApiClient.getApiService();
    Call<KitabSawti> call = kitabInterface.getSkills();

    call.enqueue(new Callback<KitabSawti>() {
        @Override
        public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
            skillList=  response.body().getSkills();
            RecyclerView recyclerView  =  findViewById(R.id.recyclerView);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(linearLayoutManager);
            skillsAdapter = new SkillsAdapter( skillList,  SkillItem.this); // changes
            recyclerView.setAdapter(skillsAdapter);
        }

        @Override
        public void onFailure(Call<KitabSawti> call, Throwable t) {

        }
    });
}

}

在我的技能_items.xml 下,我已经实现了项目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBlust"
    android:orientation="vertical"
    android:padding="10dp">




    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/educationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/it_skills"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/education_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:text="@string/text_skills"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="horizontal"
        android:padding="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/subjectImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/programming_skills"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/programming"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/programming_skills"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <View style="@style/dividerHorizontal" />
        </RelativeLayout>

    </LinearLayout>


    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="horizontal"
        android:padding="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/framework_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/it_frameworks"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/framework"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/text_framework"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <View style="@style/dividerHorizontal" />
        </RelativeLayout>

    </LinearLayout>


    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="horizontal"
        android:padding="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/android_component"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/android_components"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/architecture"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/architecture"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <View style="@style/dividerHorizontal" />
        </RelativeLayout>

    </LinearLayout>


    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="horizontal"
        android:padding="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/software_method"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/software_methodologies"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/software"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/software"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <View style="@style/dividerHorizontal" />
        </RelativeLayout>

    </LinearLayout>


    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlust"
        android:orientation="horizontal"
        android:padding="20dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/software_ide"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/software_ide"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/ide"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/ides"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <View style="@style/dividerHorizontal" />
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>

在我托管 RecyclerView 的 Skills.xml 下方

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />


</RelativeLayout>

以下截图 current ui

【问题讨论】:

  • 尝试在布局中将 wrap_content 替换为 match_parent。将 wrap_content 放在 RecyclerView、ListView 或根视图上是一种不好的做法。
  • @AnhaytAnanun,我已经按照你说的做了,但它没有滚动
  • 好的,另一个快速建议是将技能_items.xml 中的 android:layout_height="match_parent" 替换为 wrap_content。对不起,我现在没有电脑,所以试着用头脑分析代码。
  • Anhayt Ananun 非常感谢你,你救了我的命
  • 好的,添加这个作为答案,以便人们看到。

标签: android scroll android-recyclerview


【解决方案1】:

问题是,项目的根布局(skills_items.xml 中的根 LinearLayout)的高度为 match_parent。这弄乱了 RecyclerView 进行滚动事件计算的方式。 此外,通常建议不要将 wrap_content 用于 Activity 的根布局的高度或宽度,将 wrap_content 用于 RecyclerView 或 ListView 的高度(如果滚动是垂直的)或宽度(如果滚动是水平的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多