我有一个.xml文件(download_text_item.xml),其中包含一个TextView,用于在RecyclervIew中设置Text。
现在我需要一个带有TextView的第二个.xml文件(mirror_text_item.xml),它也应该可以在RecyclerView中访问,但我不知道如何在我的适配器中正确地对它进行充气,我制作了一个包含,它通过.xml但不是以编程方式工作。
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/mirrors">
<include layout="@layout/mirror_text_item"/>
</FrameLayout>
我也尝试使用DownloadAdapter中的container.addView
但不知何故我无法正确使用。
mirror_text_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:clickable="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/mir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLines="1"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="http://www.mirror.com"
android:textColor="?attr/colorPrimary"
android:textSize="16dp" />
</RelativeLayout>
</RelativeLayout>
DownloadAdapter:
public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.ViewHolder> {
String[] items;
Fragment fragment;
public DownloadAdapter(Fragment fragment, String[] items) {
this.fragment = fragment;
this.items = items;
}
@Override
public DownloadAdapter.ViewHolder onCreateViewHolder(ViewGroup container, int viewType) {
View a, v = null;
if (viewType == 0)
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_header_item, container, false);
else
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_text_item, container, false);
//mirror_text_item.xml needs to be inflated here
return new ViewHolder(v);
}
public String getSelection(int position) {
return items[position];
}
@Override
public int getItemViewType(int position) {
return 1;
}
@Override
public void onBindViewHolder(DownloadAdapter.ViewHolder holder, final int position) {
final String item = Utils.unescapeJavaString(Html.fromHtml(getSelection(position)).toString());
holder.text.setText(item);
if (Utils.downloadVis == 1)
holder.mir.setText("Just testing"); //Nullpointer Exception as not inflated
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.openUri(fragment, item);
}
});
}
@Override
public int getItemCount() {
return items.length;
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView text, mir;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
mir = (TextView) itemView.findViewById(R.id.mir);
this.itemView = itemView;
}
}
}
编辑: 一切都适用于H Raval代码,除了Mirror Textview的文本,我想问题在于下面的代码:
@Override
public DownloadAdapter.ViewHolder onCreateViewHolder(ViewGroup container, int viewType) {
View a, v = null;
if (viewType == 0)
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_header_item, container, false);
else {
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_text_item, container, false);
return new DownloadViewHolder(v);
}
if (Utils.downloadVis == 1) {
v = LayoutInflater.from(container.getContext()).inflate(R.layout.mirror_text_item, container, false);
return new MirrorViewHolder(v);
}
return new ViewHolder(v);
}
答案 0 :(得分:0)
在这里你去.....
public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.ViewHolder> {
String[] items;
Fragment fragment;
public DownloadAdapter(Fragment fragment, String[] items) {
this.fragment = fragment;
this.items = items;
}
@Override
public DownloadAdapter.ViewHolder onCreateViewHolder(ViewGroup container, int viewType) {
View a, v = null;
if (viewType == 0)
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_header_item, container, false);
else
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_text_item, container, false);
//mirror_text_item.xml needs to be inflated here
return new ViewHolder(v);
}
public String getSelection(int position) {
return items[position];
}
@Override
public int getItemViewType(int position) {
return 1;
}
@Override
public DownloadAdapter.ViewHolder onCreateViewHolder(ViewGroup container, int viewType) {
View a, v = null;
if((Utils.downloadVis == 1)){
v = LayoutInflater.from(container.getContext()).inflate(R.layout.mirror_text_item, container, false);
return new DownloadViewHolder(v);
}else{
v = LayoutInflater.from(container.getContext()).inflate(R.layout.download_text_item, container, false);
return new MirrorViewHolder(v);
}
}
@Override
public void onBindViewHolder(DownloadAdapter.ViewHolder viewholder, final int position) {
final String item = Utils.unescapeJavaString(Html.fromHtml(getSelection(position)).toString());
if(viewholder instenceOf DownloadViewHolder){
DownloadViewHolder holder = (DownloadViewHolder) viewholder;
holder.textView.setText(item);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.openUri(fragment, item);
}
});
}else{
MirrorViewHolder holder = (MirrorViewHolder) viewholder;
holder.textView.setText(item);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.openUri(fragment, item);
}
});
}
}
@Override
public int getItemCount() {
return items.length;
}
class MirrorViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.mir);
this.itemView = itemView;
}
}
class DownloadViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
this.itemView = itemView;
}
}