[英]RecyclerView doesn't scroll
I am new to android development and I was trying to make a log using RecyclerView and CardView . 我是Android开发新手,我试图使用RecyclerView和CardView创建日志。 But the problem that I am facing is that the RecyclerView won't scroll.
但我面临的问题是RecyclerView不会滚动。 I did a little research on this issue, but yet couldn't find a way to solve the problem.
我对这个问题进行了一些研究,但却找不到解决问题的方法。
This is the code for my RecyclerView, 这是我的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>
And the code for my CardView is, 我的CardView的代码是,
<?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>
The java code for RecyclerView, RecyclerView的java代码,
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);
The code for the adapter, 适配器的代码,
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. 此外,我希望Date和Recent Logins标题是静态的而不是滚动。 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? 有人可以帮我解决这个问题吗?
Put the content of your layout within DrawerLayout
将布局的内容放在
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>
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(getApplication(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
set linearlayoutManager Vertical 设置linearlayoutManager垂直
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.