我想从多个数组中引入recycler_item.xml信息 阵列
newstitl.add(i,titl);
newsdat.add(i,dat);
newsbody.add(我,身体);
定位在
<TextView
android:id="@+id/tv_recycler_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/tv_recycler_item1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/tv_recycler_item2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
在NewsFragment.java中
mRecyclerView = (RecyclerView) viiew.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(newstitl);
mRecyclerView.setAdapter(mAdapter);
有RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<String> mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.tv_recycler_item);
}
}
public RecyclerAdapter(ArrayList<String> dataset) {
mDataset = dataset;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
虽然我只能拿一个阵列 Here is a screenshot
答案 0 :(得分:0)
您应该创建一个名为News
的模型,其中包含日期,标题和正文。然后,您应该将List<News>
传递给适配器而不是List<String>
并根据需要操作数据。
答案 1 :(得分:0)
很简单,你只需要对适配器进行一些调整。你需要的是一个接受3个ArrayList的适配器..
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<String> mDataset;
private ArrayList<String> mDataset2;
private ArrayList<String> mDataset3;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView, mTextView2, mTextView3 ;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.tv_recycler_item);
mTextView2 = (TextView) v.findViewById(R.id.tv_recycler_item1);
mTextView3 = (TextView) v.findViewById(R.id.tv_recycler_item2);
}
}
public RecyclerAdapter(ArrayList<String> dataset, ArrayList<String>dataset2, ArrayList<String>dataset3) {
mDataset = dataset;
mDataset2 = datset2;
mDataset3 = dataset3;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.get(position));
holder.mTextView1.setText(mDataset2.get(position));
holder.mTextView.setText(mDataset3.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
并调用NewsFragment中的3 ArrayList适配器构造函数,如下所示 -
mRecyclerView = (RecyclerView) viiew.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(newstitl,newsdat, newsbody);
mRecyclerView.setAdapter(mAdapter);