无法让ImageView显示在RecyclerView上

时间:2017-04-03 01:11:50

标签: android android-layout android-recyclerview

我刚刚学习了RecyclerView,无法弄清楚为什么imageView与android:id =" @ + id / im_item_icon"不会出现在屏幕上。我甚至有另一个可以工作的imageView但是当我交换ID时,原来不工作的视图工作,而工作的视图不起作用。所以基本上问题必须是ID,但我无法弄清楚原因。顺便说一下,我从博客#34; http://wiseassblog.com/tutorials/2016/03/04/how-to-build-a-recyclerview/"获得了代码。

DerpAdapter.java

public class DerpAdapter extends RecyclerView.Adapter<DerpAdapter.DerpHolder> {

private List<ListItem> listData;
private LayoutInflater inflater;

public DerpAdapter(List<ListItem> listData, Context c){
    inflater = LayoutInflater.from(c);
    this.listData = listData;
}

@Override
public DerpAdapter.DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new DerpHolder(view);
}

@Override
public void onBindViewHolder(DerpHolder holder, int position) {
    ListItem item = listData.get(position);
    holder.title.setText(item.getTitle());
    holder.icon.setImageResource(item.getImageResId());
}

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

class DerpHolder extends RecyclerView.ViewHolder {

    private TextView title;
    private ImageView icon;
    private View container;

    public DerpHolder(View itemView) {
        super(itemView);

        title = (TextView)itemView.findViewById(R.id.lbl_item_text);
        icon = (ImageView)itemView.findViewById(R.id.im_item_icon);
        //We'll need the container later on, when we add an View.OnClickListener
        container = itemView.findViewById(R.id.cont_item_root);
    }
}

DerpData.java

public class DerpData {
private static final String[] titles = {"Nothingness cannot be defined",
        "Time is like a river made up of the events which happen, and a violent stream; " +
                "for as soon as a thing has been seen, it is carried away, and another comes" +
                " in its place, and this will be carried away too,",
        "But when I know that the glass is already broken, every minute with it is precious.",
        "For me, it is far better to grasp the Universe as it really is than to persist in" +
                " delusion, however satisfying and reassuring.",
        "The seeker after the truth is not one who studies the writings of the ancients and," +
                " following his natural disposition, puts his trust in them, but rather the" +
                " one who suspects his faith in them and questions what he gathers from them," +
                " the one who submits to argument and demonstration, and not to the " +
                "sayings of a human being whose nature is fraught with all kinds " +
                "of imperfection and deficiency.",
        "You must take personal responsibility. You cannot change the circumstances, the" +
                " seasons, or the wind, but you can change yourself. That is something you" +
                " have charge of."
};
private static final String[] subTitles = {"Bruce Lee",
        "Marcus Aurelius",
        "Meng Tzu",
        "Ajahn Chah",
        "Carl Sagan",
        "Alhazen",
        "Jim Rohn"

};
private static final int icon = R.drawable.ic_tonality_black_36dp;

public static List <ListItem> getListData() {
    List <ListItem> data = new ArrayList <>();

    //Repeat process 4 times, so that we have enough data to demonstrate a scrollable
    //RecyclerView
    for (int x = 0; x  < 4; x++) {
        //create ListItem with dummy data, then add them to our List
        for (int i = 0; i  < titles.length; i++) {
            ListItem item = new ListItem();
            item.setTitle(titles[i]);
            item.setSubTitle(subTitles[i]);
            data.add(item);
        }
    }
    return data;
}

ListItem.java

public class ListItem {
private int imageResId;
private String subTitle;
private String title;
private boolean favourite = false;

public String getSubTitle() {
    return subTitle;
}

public void setSubTitle(String subTitle) {
    this.subTitle = subTitle;
}

public boolean isFavourite() {
    return favourite;
}

public void setFavourite(boolean favourite) {
    this.favourite = favourite;
}

public int getImageResId() {
    return imageResId;
}

public void setImageResId(int imageResId) {
    this.imageResId = imageResId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

DetailActivity.java

public class DetailActivity extends AppCompatActivity {

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

ListActivity.java

public class ListActivity extends AppCompatActivity {

private RecyclerView recView;
private DerpAdapter adapter;

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

    recView = (RecyclerView)findViewById(R.id.rec_list);
    //Check out GridLayoutManager and StaggeredGridLayoutManager for more options
    recView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new DerpAdapter(DerpData.getListData(), this);
    recView.setAdapter(adapter);
}

activity_detail.xml

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="application.binarysoup.jsonpractice.ui.DetailActivity">

<TextView
    android:id="@+id/lbl_quote_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-medium" />

<TextView
    android:id="@+id/lbl_quote_attribution"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/lbl_quote_text"
    android:fontFamily="sans-serif-light"
    android:textStyle="italic" />

activity_list.xml

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="application.binarysoup.jsonpractice.ui.ListActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rec_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cont_item_root"
android:layout_width="match_parent"
android:layout_height="72dp"
android:background="@drawable/background_state_drawable"
android:clickable="true"
>

<ImageView
    android:id="@+id/im_item_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:paddingLeft="16dp"
    android:src="@drawable/ic_tonality_black_36dp" />


<TextView
    android:id="@+id/lbl_item_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/im_item_icon"
    android:layout_marginLeft="72dp"
    android:layout_marginRight="48dp"
    android:ellipsize="end"
    android:fontFamily="sans-serif"
    android:singleLine="true"
    android:text="Sois comme l'eau mon ami"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<TextView
    android:id="@+id/lbl_item_sub_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lbl_item_text"
    android:layout_marginLeft="72dp"
    android:layout_marginRight="48dp"
    android:ellipsize="end"
    android:fontFamily="sans-serif"
    android:singleLine="true"
    android:text="Mononc' J"
    android:textSize="14sp" />

<ImageView
    android:id="@+id/im_item_icon_secondary"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:clickable="true"
    android:padding="16dp"
    android:src="@drawable/ic_star_border_black_24dp"
    android:background="@drawable/background_state_drawable"
    />

1 个答案:

答案 0 :(得分:0)

  

无法让ImageView显示在RecyclerView上

您必须初始化imageResId才能将其显示在recyclerView

 for (int i = 0; i  < titles.length; i++) {
            ListItem item = new ListItem();
            item.setTitle(titles[i]);
            item.setSubTitle(subTitles[i]);
//put the code to initialise the imageResId here
            data.add(item);
        }