我正在使用卡片视图和列表视图作为大学项目。
RecyclerView小部件是ListView的更高级和灵活的版本。此窗口小部件是一个容器,用于显示可以通过维护有限数量的视图而非常有效地滚动的大型数据集。如果数据集合的元素在运行时根据用户操作或网络事件而更改,请使用RecyclerView小部件。
RecyclerView类通过提供以下内容简化了大型数据集的显示和处理:
用于定位项目的布局管理器 常用项目操作的默认动画,例如删除或添加项目 您还可以灵活地为RecyclerView小部件定义自定义布局管理器和动画。
我的main_activity.xml
<?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="sonu.myapplication.MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:id="@+id/ToolbarMain">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:dividerHeight="10dp"
android:id="@+id/lvMain"
android:layout_below="@id/ToolbarMain"
android:divider="@null">
</ListView>
</RelativeLayout>
<android.support.v7.widget.CardView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:elevation="4dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="105dp"
android:id="@+id/ivMain"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="11dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMain"
android:text="Timetable"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:layout_toRightOf="@id/ivMain"
android:textSize="24sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvDescription"
android:text="Description"
android:layout_marginLeft="6dp"
android:layout_below="@id/tvMain"
android:layout_toRightOf="@id/ivMain"
android:textSize="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvClick"
android:textSize="12dp"
android:text="Click here"
android:layout_alignParentRight="true"
android:layout_marginTop="7dp"
android:layout_below="@id/tvDescription"
android:layout_alignBottom="@id/ivMain"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
package sonu.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUiViews();
initToolbar();
}
private void setupUiViews(){
toolbar=(Toolbar)findViewById(R.id.ToolbarMain);
listView=(ListView)findViewById(R.id.lvMain);
}
private void initToolbar(){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("TimeTable App");
}
private void setUpListView(){
String[] title = getResources().getStringArray(R.array.Main);
String[] description = getResources().getStringArray(R.array.Description);
SimpleAdapter simpleAdapter=new SimpleAdapter(this, title, description);
listView.setAdapter(simpleAdapter);
}
public class SimpleAdapter extends BaseAdapter{
private Context myContext;
private LayoutInflater layoutInflater;
private TextView title, description;
private String[] titleArray;
private String[] descriptionArray;
private ImageView imageView;
public SimpleAdapter(Context myContext, String[] titleArray, String[] descriptionArray){
this.myContext=myContext;
this.titleArray=titleArray;
this.descriptionArray=descriptionArray;
layoutInflater=LayoutInflater.from(myContext);
}
@Override
public int getCount() {
return titleArray.length;
}
@Override
public Object getItem(int position) {
return titleArray[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=layoutInflater.inflate(R.layout.main_activity_single_item, null);
}
title=(TextView)convertView.findViewById(R.id.tvMain);
description=(TextView)convertView.findViewById(R.id.tvDescription);
imageView=(ImageView)convertView.findViewById(R.id.ivMain);
title.setText(titleArray[position]);
description.setText(descriptionArray[position]);
if (titleArray[position].equalsIgnoreCase("Timetable")){
imageView.setImageResource(R.drawable.timetable);
}else if (titleArray[position].equalsIgnoreCase("Subjects")){
imageView.setImageResource(R.drawable.subjects);
}else if (titleArray[position].equalsIgnoreCase("Faculty")){
imageView.setImageResource(R.drawable.teachers);
}else if (titleArray[position].equalsIgnoreCase("resource")){
imageView.setImageResource(R.drawable.resource);
}
return convertView;
}
}
}
现在我的strings.xml
<resources>
<string name="app_name">My Application</string>
<string-array name="Main">
<item> Timetable </item>
<item> Subjects </item>
<item> Faculty </item>
<item> Resources </item>
</string-array>
<string-array name="Description">
<item> Description 1</item>
<item> Description 2</item>
<item> Description 3</item>
<item> Description 4</item>
</string-array>
</resources>
答案 0 :(得分:0)
你的oncreate方法应该是这样的。首先初始化视图然后工具栏然后访问视图和设置适配器。看到你的代码我认为你没有调用setUpListView()。尝试添加并再次检查。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUiViews();
initToolbar();
setUpListView();
}
答案 1 :(得分:0)
在你的gradle中添加com.android.support:cardview-v7:26.1.0 cardview依赖项。如果默认情况下是toobar那么你不需要调用initToolbar()方法。