RecyclerView 不滚动 [英] RecyclerView doesn't scroll

查看:50
本文介绍了RecyclerView 不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 开发的新手,我试图使用 RecyclerViewCardView 制作日志.但我面临的问题是 RecyclerView 不会滚动.我对这个问题做了一些研究,但还是找不到解决问题的方法.

I am new to android development and I was trying to make a log using RecyclerView and CardView. But the problem that I am facing is that the RecyclerView won't scroll. I did a little research on this issue, but yet couldn't find a way to solve the problem.

这是我的 RecyclerView 的代码,

This is the code for my RecyclerView,

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.vishistvarugeese.ongc_app.AdminActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

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

    </LinearLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/white"
            app:headerLayout="@layout/admin_nav_header"
            app:itemIconTint="@color/black"
            app:itemTextColor="@color/colorAccent"
            app:menu="@menu/admin_menu">

        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

我的 CardView 的代码是,

And the code for my CardView is,

<?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="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/recentLoginTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/recentLoginCpf"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="40dp"
                android:text="Time"
                android:textColor="@color/black"
                android:textSize="23sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/recentLoginName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="Name"
                android:textColor="@color/colorAccent"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/recentLoginCpf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/recentLoginName"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="16dp"
                android:text="Reg Number"
                android:textColor="@color/pink"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLoginType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/recentLoginCpf"
                android:layout_alignStart="@+id/recentLoginCpf"
                android:layout_below="@+id/recentLoginTime"
                android:layout_marginBottom="10dp"
                android:text="Type"
                android:textColor="@color/pink"
                android:textSize="18sp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>


</LinearLayout>

RecyclerView的java代码,

The java code for RecyclerView,

    private RecyclerView recyclerView;
    private RecyclerView.Adapter recyclerViewAdapter;
    private List<ListItem_RecyclerView_Admin> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        //Recycler View
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        listItems = new ArrayList<>();

        for(int i=0;i<10;i++){
            ListItem_RecyclerView_Admin listItem = new ListItem_RecyclerView_Admin(
                    "9:30",
                    "Name",
                    "808821",
                    "Admin"
            );
            listItems.add(listItem);
       }
        recyclerViewAdapter = new RecyclerViewAdapter(listItems,this);
        recyclerView.setAdapter(recyclerViewAdapter);

适配器代码,

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

    private List<ListItem_RecyclerView_Admin> listItems;
    private Context context;

    public RecyclerViewAdapter(List<ListItem_RecyclerView_Admin> listitems, Context context) {
        this.listItems = listitems;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListItem_RecyclerView_Admin listItem = listItems.get(position);

        holder.recentLoginName.setText(listItem.getRecentLoginName());
        holder.recentLoginTime.setText(listItem.getRecentLoginTime());
        holder.recentLoginCpf.setText(listItem.getRecentLoginCpf());
        holder.recentLoginType.setText(listItem.getRecentLoginType());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView recentLoginName;
        private TextView recentLoginTime;
        private TextView recentLoginCpf;
        private TextView recentLoginType;

        public ViewHolder(View itemView) {
            super(itemView);
            recentLoginName = (TextView) itemView.findViewById(R.id.recentLoginName);
            recentLoginTime = (TextView) itemView.findViewById(R.id.recentLoginTime);
            recentLoginCpf = (TextView) itemView.findViewById(R.id.recentLoginCpf);
            recentLoginType = (TextView) itemView.findViewById(R.id.recentLoginType);
        }
    }
}

另外,我希望日期和最近登录标题是静态的而不是滚动的.我试图通过将它们放在线性布局中来做到这一点.但我认为这不是正确的做法.

Also, I wanted the Date and Recent Logins header to be static and not scroll. I tried to do this by putting them inside a Linear Layout. But I don't think this is the right way to do it.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

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

    </LinearLayout>

有人可以帮我解决这个问题吗?

Can someone please help me with this issue?

推荐答案

将布局的内容放在 DrawerLayout

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@drawable/headernav"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Date"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/recentLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="Recent Logins"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />
        </LinearLayout>

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

    </LinearLayout>

        </ FrameLayout>

        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/white"
            app:headerLayout="@layout/admin_nav_header"
            app:itemIconTint="@color/black"
            app:itemTextColor="@color/colorAccent"
            app:menu="@menu/admin_menu">

        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>

这篇关于RecyclerView 不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆