我将添加两个RecyclerView
相同的fragment
,如图片所示,但我已经完成了第一次recyclelerview 。我没有得到如何使用我现有的代码添加一个,请帮助我。我的RecyclerView
代码
Communication.class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_communication_summary, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.comm_expected_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setHasFixedSize(true);
mRecyclerViewLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mRecyclerViewLayoutManager);
private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<CommunicationVO> mCommunicationVOsList;
public MyRecyclerViewAdapter(List<CommunicationVO> communicationVOsList) {
mCommunicationVOsList = communicationVOsList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.communication_summary_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder != null) {
CommunicationVO communicationVO = mCommunicationVOsList.get(position);
holder.expInvoiceNumber.setText(communicationVO.getExpInvoiceNumber());
holder.expAmount.setText(CashPunditUtils.formatAmount(communicationVO.getExpAmount()));
holder.expDueDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpDueDate()), "dd-MMM-yy"));
holder.expPromisedDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpPromisedDate()), "dd-MMM-yy"));
}
}
@Override
public int getItemCount() {
return mCommunicationVOsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView expInvoiceNumber, expAmount, expDueDate, expPromisedDate;
public ViewHolder(View itemView) {
super(itemView);
expInvoiceNumber = (TextView) itemView.findViewById(R.id.textview_invnum);
expAmount = (TextView) itemView.findViewById(R.id.textview_amount);
expDueDate = (TextView) itemView.findViewById(R.id.textview_duedate);
expPromisedDate = (TextView) itemView.findViewById(R.id.textview_promisedate);
}
}
答案 0 :(得分:1)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_two"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
</LinearLayout>
在您的Java代码中创建同一个适配器的两个不同实例,并将该适配器设置为两个回收器视图。由于您具有相同的视图和不同的数据,因此您可以使用相同的适配器进行回收器视图。希望这对您有帮助。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_communication_summary, container, false);
//RecyclerView One
mRecyclerView1 = (RecyclerView) view.findViewById(R.id.recycler_one);
mRecyclerView1.setHasFixedSize(true);
adapter1 = new MyRecyclerViewAdapter(communicationVOsList);
mRecyclerViewLayoutManager1 = new LinearLayoutManager(getContext());
mRecyclerView1.setLayoutManager(mRecyclerViewLayoutManager1);
mRecyclerView1.setAdapter(adapter1);
//RecyclerView two
mRecyclerView2 = (RecyclerView) view.findViewById(R.id.recycler_two);
adapter2 = new MyRecyclerViewAdapter(communicationVOsList);
mRecyclerView2.setHasFixedSize(true);
mRecyclerViewLayoutManager2 = new LinearLayoutManager(getContext());
mRecyclerView2.setLayoutManager(mRecyclerViewLayoutManager2);
mRecyclerView2.setAdapter(adapter2);
private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<CommunicationVO> mCommunicationVOsList;
public MyRecyclerViewAdapter(List<CommunicationVO> communicationVOsList) {
mCommunicationVOsList = communicationVOsList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.communication_summary_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder != null) {
CommunicationVO communicationVO = mCommunicationVOsList.get(position);
holder.expInvoiceNumber.setText(communicationVO.getExpInvoiceNumber());
holder.expAmount.setText(CashPunditUtils.formatAmount(communicationVO.getExpAmount()));
holder.expDueDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpDueDate()), "dd-MMM-yy"));
holder.expPromisedDate.setText(CashPunditUtils.formatDate(CashPunditUtils.extractDateAndTime(communicationVO.getExpPromisedDate()), "dd-MMM-yy"));
}
}
@Override
public int getItemCount() {
return mCommunicationVOsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView expInvoiceNumber, expAmount, expDueDate, expPromisedDate;
public ViewHolder(View itemView) {
super(itemView);
expInvoiceNumber = (TextView) itemView.findViewById(R.id.textview_invnum);
expAmount = (TextView) itemView.findViewById(R.id.textview_amount);
expDueDate = (TextView) itemView.findViewById(R.id.textview_duedate);
expPromisedDate = (TextView) itemView.findViewById(R.id.textview_promisedate);
}
}
修改强>:
不要忘记在recyclerview中创建适配器对象。添加这些代码行并尝试一次。
mRecyclerViewAdapter = new MyRecyclerViewAdapter(mCommunicationVOsList);
mRecyclerView.setAdapter(mRecyclerViewAdapter);