我已经开始Android开发,直到现在我没有问题,但我试图将RecyclerView插入我的布局,一切都很好没有错误,但我的RecyclerView本身没有显示。代码似乎对我来说。
这是我的代码:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private RVAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), (Toolbar) findViewById(R.id.app_bar));
recyclerView = (RecyclerView) findViewById(R.id.goalList);
recyclerView.setHasFixedSize(true);
adapter = new RVAdapter(getApplication(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
}
public static List<RVData> getData() {
List<RVData> data = new ArrayList<>();
String[] titles = {"Dummy1", "Dummy2", "Dummy3"};
for (int i = 0; i < titles.length; i++) {
RVData current = new RVData();
current.goalTitle = titles[i];
data.add(current);
}
return data;
}
public void newGoal(View view) {
Intent intent = new Intent(MainActivity.this, NewGoal.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml:
<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"
style="@style/DefaultLayoutStyle"
tools:context=".MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
style="@style/FABStyle"
android:onClick="newGoal"
android:src="@drawable/ic_add"
app:borderWidth="0dp" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/goalList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.kellarapps.zeal.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
goal_row.xml
<?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="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/goalRowCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/goalRowTitle"
android:textSize="17sp"
android:clickable="true"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:textColor="@color/TextColor"
android:text="@string/dummyTxt"/>
</LinearLayout>
RVData.java:
public class RVData {
CheckBox checkBox;
String goalTitle;
}
RVAdapter.java
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.myViewHolder> {
List<RVData> data = Collections.emptyList();
private LayoutInflater inflater;
public RVAdapter(Context context, List<RVData> cData) {
inflater = LayoutInflater.from(context);
this.data = cData;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.goal_row, viewGroup, false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(myViewHolder viewHolder, int position) {
RVData current = data.get(position);
viewHolder.title.setText(current.goalTitle);
}
@Override
public int getItemCount() {
return 0;
}
class myViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.goalRowTitle)
TextView title;
@Bind(R.id.goalRowCB)
CheckBox checkBox;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView;
checkBox = (CheckBox) itemView;
}
}
}
没有错误,但也没有RecyclerView。
答案 0 :(得分:2)
有一点很突出,您的项目计数被硬编码为0。您应该返回该函数中的数据大小:
@Override
public int getItemCount() {
return data.size();
}
答案 1 :(得分:1)
你可能会在这里省去一些麻烦:
class myViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.goalRowTitle)
TextView title;
@Bind(R.id.goalRowCB)
CheckBox checkBox;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView;
checkBox = (CheckBox) itemView;
}
}
更直接的是:
class myViewHolder extends RecyclerView.ViewHolder {
//@Bind(R.id.goalRowTitle)
TextView title;
//@Bind(R.id.goalRowCB)
CheckBox checkBox;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.goalRowTitle);
checkBox = (CheckBox) itemView.findViewById(R.id.goalRowCB);
}
}
将此与blackcj注意到的getCount问题相结合,您应该解决您的问题。更有可能的是,为什么你没有得到类型不匹配的原因,试图将不是TextView的视图或Checkboxes设置为那些类型......