我试图将SlideNerd的说明纳入如何在我的CS项目中创建回收者视图。 他正在教导如何在导航抽屉内制作回收者视图,并且我试图在主要活动中制作一个。
这是我的 current output
显示了一个回收元素,但问题是它是XML代码中初始化的示例文本和图标.RelectItemRecyclerFragment中的数据未传递给ItemViewholder而不显示。有人能帮助我找到我做错的事吗?
更新 通过将测试语句放在getData(),onBindViewHolder()和getItemCount()方法中进行调试后,我了解到我的onBindViewHolder()方法没有被调用。我已经查看了与我的问题相关的其他问题,他们的解决方案是实例化我已经做过的LinearLayoutManager。有人知道onBindViewHolder()在哪里/怎么被调用或者我忘了做什么来调用它?
java文件
ChooseItem.Java:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class ChooseItem extends AppCompatActivity {
private Toolbar toolbar;
private ItemAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_item);
ChooseItemRecyclerFragment fragment = (ChooseItemRecyclerFragment) getSupportFragmentManager().findFragmentById(R.id.choose_item_list_fragment);
fragment.setUp(R.id.choose_item_list_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
}
@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_choose_item, 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);
}
}
ChooseItemRecyclerFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by michael on 11/17/2015.
*/
public class ChooseItemRecyclerFragment extends Fragment {
private View containerView;
private RecyclerView recyclerView;
private ItemAdapter adapter;
public ChooseItemRecyclerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static List<Item> getData(){
List<Item> data= new ArrayList<>();
int[] iconId = {R.id.item_icon, R.id.item_icon, R.id.item_icon, R.id.item_icon, R.id.item_icon};
String[] itemNames = {"Fajita", "Cahones", "Burger", "Bald Eagles", "Communism"};
String[] itemPrice = {"$10.43", "$1.43", "$10.43", "$10.43", "$666.66"};
for(int i = 0; i<iconId.length&&i<itemNames.length&&i<itemPrice.length;i++){
Item current= new Item(itemNames[i], "description", itemPrice[i], iconId[i]);
data.add(current);
}
return data;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout =inflater.inflate(R.layout.fragment_choose_item, container, false);
recyclerView = (RecyclerView)layout.findViewById(R.id.choose_item_list);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
adapter=new ItemAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout; }
public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
containerView=getActivity().findViewById(fragmentId);
}
}
ItemAdapter.java
package com.example.michael.othershit;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by michael on 11/9/2015.
*/
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private LayoutInflater inflater;
List<Item> data = Collections.EMPTY_LIST;
/**
* Created by michael on 11/9/2015.
*/
public ItemAdapter(Context context,List<Item> list){
inflater= LayoutInflater.from(context);
data=list;
}
/**
* Created by michael on 11/9/2015.
*/
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =inflater.inflate(R.layout.item_recycler_view_row, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);
return holder;
}
/**
* Created by michael on 11/9/2015.
*/
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
TextView title = holder.title;
TextView price = holder.price;
ImageView icon = holder.icon;
title.setText(data.get(position).getName());
price.setText(data.get(position).getItemPrice());
icon.setImageResource(data.get(position).getIconId());
}
/**
* Created by michael on 11/9/2015.
*/
@Override
public int getItemCount() {
return data.size();
}
/**
* Created by michael on 11/9/2015.
*/
class ItemViewHolder extends RecyclerView.ViewHolder{
TextView title;
TextView price;
ImageView icon;
public ItemViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.item_name);
icon = (ImageView) itemView.findViewById(R.id.item_icon);
price=(TextView) itemView.findViewById(R.id.item_price);
}
}
}
XML文件:
activity_choose_items
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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" android:fitsSystemWindows="true"
tools:context=".ChooseItemActivity"
android:id="@+id/drawer_layout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/item_recycler_view_row"/>
<include layout="@layout/content_choose_item" />
<fragment android:id="@+id/choose_item_list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.michael.othershit.ChooseItemRecyclerFragment"
tools:layout="@layout/fragment_choose_item" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
fragment_choose_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/rv_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/choose_item_list">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
item_recycler_view_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/item_recycler_view"
>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/item_icon"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_local_restaurant">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="@+id/item_name"
android:layout_marginLeft="72dp"
android:text="Example Item Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="@+id/item_price"
android:paddingLeft="32dp"
android:text="$1.89"/>
</LinearLayout>