我正在遵循Android开发人员的代码:RecyclerView
,并且我希望RecyclerView中的单个项目具有更复杂的布局(我希望它在RelativeLayout中具有一个图像和2个文本视图,而不是仅一个没有任何Layout父级的TextView。
当我的my_text_view.xml
文件仅包含一个TextView时,代码将起作用:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="28dp"
android:textSize="24sp"
tools:text="Hello John Doe" />
但是当我尝试将其更改为更复杂的布局时,它在RelativeLayout下包含一个ImageView和2个TextView,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:maxLines="1"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
然后应用程序将崩溃。 我真的不知道应该在哪里应用更多更改,我对它们对保持器,适配器和布局管理器的解释颇为迷惑。他们的代码中到底应该更改什么?如果您需要参考,我基本上会复制他们的代码(而且有效,这就是为什么我不在这里复制他们的代码)
编辑:我相信所做的更改应该在这段代码中的某个位置,但是我不知道在哪里以及具体如何
package com.example.lastlocation.recyclerviewer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
答案 0 :(得分:0)
如果您修改项目视图XML文件的内容,则还必须修改ViewHolder
类以及适配器的onCreateViewHolder()
和onBindViewHolder()
方法。
在新版本中,您有一个RelativeLayout,一个ImageView和两个TextView。您可能不需要对RelativeLayout做任何事情,因此您的ViewHolder
仅具有对ImageView和TextView的引用:
public static class MyViewHolder extends RecyclerView.ViewHolder {
public final ImageView icon;
public final TextView firstLine;
public final TextView secondLine;
public MyViewHolder(View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.icon);
firstLine = itemView.findViewById(R.id.firstLine);
secondLine = itemView.findViewById(R.id.secondLine);
}
}
您的onCreateViewHolder()方法不需要太多更改。通常,您只需要删除演员表,因为现在您不再需要膨胀TextView对象:
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
return new MyViewHolder(itemView);
}
您的onBindViewHolder()方法需要更改为使用您指定的新视图:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.icon.setImageResource(R.drawable.whatever);
holder.firstLine.setText("Hello");
holder.secondLine.setText("World");
}