简体   繁体   中英

recyclerview items not scrolling?

I am making android cv app and I have implemented RecyclerView but items not scrolling even though, I have used RecyclerViewLayout Manager.

below my SkillsAdapter.class

public class SkillsAdapter extends RecyclerView.Adapter { public List skillList; public Context context;

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);
    }
}

}

below my SkillsItem.java class

public class SkillItem extends 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) {

        }
    });
}

}

below my skills_items.xml where I have implemented items

<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>

below my skills.xml where I have hosted RecyclerView

<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>

below screenshot current ui

The problem is, that the root layout of your item (the root LinearLayout at skills_items.xml) has height of match_parent. This messes up the way RecyclerView does the scrolling event calculations. Also, it is generally advised against using wrap_content for height or width of the root layout for an activity and for wrap_content for height (if scroll is vertical) or width (if scroll is horizontal) of RecyclerView or ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM