android 如何将 recyclerview 放入 recyclerView 中
How to put a recyclerview inside a recyclerView in android
所以我在 RecyclerView
里面有一个 RecyclerView
。我收到关于 RecyclerView
的 LayoutManager
的错误。我使用的数据是 OpenHours object,其中 OpenHours 是 class 我做的概括商店一周的营业时间和营业时间。错误指向我的 parent 回收站视图(这是写在顶部的第一个回收站视图)中的这一行:
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
这是我的class。
public class HoursActivity extends AppCompatActivity
{
private RecyclerView rV;
private TextView addHoursTV;
private HoursRecyclerViewAdapter hoursRecyclerViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hours);
bindActivity();
}
private void bindActivity()
{
if(!Global_Class.getInstance().getValue().openHoursArrayList.isEmpty())
{
rV = (RecyclerView) findViewById(R.id.activity_hours_RV);
rV.setLayoutManager(new LinearLayoutManager(getBaseContext()));
hoursRecyclerViewAdapter = new HoursRecyclerViewAdapter(getBaseContext(),Global_Class.getInstance().getValue().openHoursArrayList);
rV.setAdapter(hoursRecyclerViewAdapter);
rV.setItemAnimator(new DefaultItemAnimator());
}
addHoursTV = (TextView) findViewById(R.id.activity_hours_addHoursTV);
addHoursTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(HoursActivity.this,SetHours.class);
startActivity(i);
}
});
}
//An recycler view adapter that can take in multiple views
public class HoursRecyclerViewAdapter extends RecyclerView.Adapter<HoursRecyclerViewAdapter.MyViewHolder>
{
private LayoutInflater inflater;
private ArrayList<OpenHours> infoList = new ArrayList<>();
private Context context;
public HoursRecyclerViewAdapter(Context context, ArrayList<OpenHours> data)
{
inflater = LayoutInflater.from(context);
this.infoList = data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.view_hours,parent,false);
MyViewHolder holder = new MyViewHolder(view);//special way to avoid "findViewById everytime you make a view
Log.d("Qamar", "onCreateviewHolder called");
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position)//onBindViewHolder does all the work.
{
HoursTwoRecyclerViewAdapter hoursTwoRecyclerViewAdapter;
hoursTwoRecyclerViewAdapter = new HoursTwoRecyclerViewAdapter(getBaseContext(),infoList);
holder.recyclerView.setAdapter(hoursTwoRecyclerViewAdapter);
holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public int getItemCount()
{
return infoList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
RecyclerView recyclerView;
public MyViewHolder(View itemView)
{
super(itemView);
recyclerView = (RecyclerView) findViewById(R.id.view_hours_RV);
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
}
}
}
//An recycler view adapter that can take in multiple views
public class HoursTwoRecyclerViewAdapter extends RecyclerView.Adapter<HoursTwoRecyclerViewAdapter.MyViewHolder>
{
private LayoutInflater inflater;
private ArrayList<OpenHours> infoList = new ArrayList<>();
private Context context;
public HoursTwoRecyclerViewAdapter(Context context, ArrayList<OpenHours> data)
{
inflater = LayoutInflater.from(context);
this.infoList = data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.view_hours_two,parent,false);
MyViewHolder holder = new MyViewHolder(view);//special way to avoid "findViewById everytime you make a view
Log.d("Qamar", "onCreateviewHolder called");
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position)//onBindViewHolder does all the work.
{
OpenHours openHours = infoList.get(position);
holder.dayTV.setText(openHours.getDaysOpen().get(position));
holder.openingTimeHrTV.setText(openHours.getOpeningHour().substring(0,1));
holder.openingTimeMinuteTV.setText(openHours.getOpeningHour().substring(2,3));
holder.closingTimeHrTV.setText(openHours.getClosingHour().substring(0,1));
holder.closingTimeMinuteTV.setText(openHours.getClosingHour().substring(2,3));
}
@Override
public int getItemCount()
{
return infoList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView dayTV,openingTimeHrTV,openingTimeMinuteTV,closingTimeHrTV,closingTimeMinuteTV;
public MyViewHolder(View itemView)
{
super(itemView);
dayTV = (TextView) findViewById(R.id.view_hours_2_day);
openingTimeHrTV = (TextView) findViewById(R.id.view_hours_2_openingTimeHour);
openingTimeMinuteTV = (TextView) findViewById(R.id.view_hours_2_openingTimeMinute);
closingTimeHrTV = (TextView) findViewById(R.id.view_hours_2_closingTimeHour);
closingTimeMinuteTV = (TextView) findViewById(R.id.view_hours_2_closingTimeMinute);
}
}
}
}
这是我的第一个 parent 回收站视图 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout">
</include>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activity_hours_RV"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add extra hours"
android:textColor="@color/green_test"
android:id="@+id/activity_hours_addHoursTV"/>
</LinearLayout>
还有我的childxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_hours_RV"/>
</LinearLayout>
最后是我的日志错误:
10-14 21:42:26.804 18552-18552/com.daprlabs.aaron.swipedeck2 E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter$MyViewHolder.<init>(HoursActivity.java:97)
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter.onCreateViewHolder(HoursActivity.java:73)
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter.onCreateViewHolder(HoursActivity.java:57)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2843)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2685)
at android.view.View.measure(View.java:18834)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1237)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1474)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6100)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.re
删除
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
从您的 MyViewHolder class 并将该行添加到您的 onBindViewHolder
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
holder.recyclerView.setLayoutManager(linearLayoutManager);
所以我在 RecyclerView
里面有一个 RecyclerView
。我收到关于 RecyclerView
的 LayoutManager
的错误。我使用的数据是 OpenHours object,其中 OpenHours 是 class 我做的概括商店一周的营业时间和营业时间。错误指向我的 parent 回收站视图(这是写在顶部的第一个回收站视图)中的这一行:
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
这是我的class。
public class HoursActivity extends AppCompatActivity
{
private RecyclerView rV;
private TextView addHoursTV;
private HoursRecyclerViewAdapter hoursRecyclerViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hours);
bindActivity();
}
private void bindActivity()
{
if(!Global_Class.getInstance().getValue().openHoursArrayList.isEmpty())
{
rV = (RecyclerView) findViewById(R.id.activity_hours_RV);
rV.setLayoutManager(new LinearLayoutManager(getBaseContext()));
hoursRecyclerViewAdapter = new HoursRecyclerViewAdapter(getBaseContext(),Global_Class.getInstance().getValue().openHoursArrayList);
rV.setAdapter(hoursRecyclerViewAdapter);
rV.setItemAnimator(new DefaultItemAnimator());
}
addHoursTV = (TextView) findViewById(R.id.activity_hours_addHoursTV);
addHoursTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(HoursActivity.this,SetHours.class);
startActivity(i);
}
});
}
//An recycler view adapter that can take in multiple views
public class HoursRecyclerViewAdapter extends RecyclerView.Adapter<HoursRecyclerViewAdapter.MyViewHolder>
{
private LayoutInflater inflater;
private ArrayList<OpenHours> infoList = new ArrayList<>();
private Context context;
public HoursRecyclerViewAdapter(Context context, ArrayList<OpenHours> data)
{
inflater = LayoutInflater.from(context);
this.infoList = data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.view_hours,parent,false);
MyViewHolder holder = new MyViewHolder(view);//special way to avoid "findViewById everytime you make a view
Log.d("Qamar", "onCreateviewHolder called");
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position)//onBindViewHolder does all the work.
{
HoursTwoRecyclerViewAdapter hoursTwoRecyclerViewAdapter;
hoursTwoRecyclerViewAdapter = new HoursTwoRecyclerViewAdapter(getBaseContext(),infoList);
holder.recyclerView.setAdapter(hoursTwoRecyclerViewAdapter);
holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public int getItemCount()
{
return infoList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
RecyclerView recyclerView;
public MyViewHolder(View itemView)
{
super(itemView);
recyclerView = (RecyclerView) findViewById(R.id.view_hours_RV);
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
}
}
}
//An recycler view adapter that can take in multiple views
public class HoursTwoRecyclerViewAdapter extends RecyclerView.Adapter<HoursTwoRecyclerViewAdapter.MyViewHolder>
{
private LayoutInflater inflater;
private ArrayList<OpenHours> infoList = new ArrayList<>();
private Context context;
public HoursTwoRecyclerViewAdapter(Context context, ArrayList<OpenHours> data)
{
inflater = LayoutInflater.from(context);
this.infoList = data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.view_hours_two,parent,false);
MyViewHolder holder = new MyViewHolder(view);//special way to avoid "findViewById everytime you make a view
Log.d("Qamar", "onCreateviewHolder called");
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position)//onBindViewHolder does all the work.
{
OpenHours openHours = infoList.get(position);
holder.dayTV.setText(openHours.getDaysOpen().get(position));
holder.openingTimeHrTV.setText(openHours.getOpeningHour().substring(0,1));
holder.openingTimeMinuteTV.setText(openHours.getOpeningHour().substring(2,3));
holder.closingTimeHrTV.setText(openHours.getClosingHour().substring(0,1));
holder.closingTimeMinuteTV.setText(openHours.getClosingHour().substring(2,3));
}
@Override
public int getItemCount()
{
return infoList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView dayTV,openingTimeHrTV,openingTimeMinuteTV,closingTimeHrTV,closingTimeMinuteTV;
public MyViewHolder(View itemView)
{
super(itemView);
dayTV = (TextView) findViewById(R.id.view_hours_2_day);
openingTimeHrTV = (TextView) findViewById(R.id.view_hours_2_openingTimeHour);
openingTimeMinuteTV = (TextView) findViewById(R.id.view_hours_2_openingTimeMinute);
closingTimeHrTV = (TextView) findViewById(R.id.view_hours_2_closingTimeHour);
closingTimeMinuteTV = (TextView) findViewById(R.id.view_hours_2_closingTimeMinute);
}
}
}
}
这是我的第一个 parent 回收站视图 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout">
</include>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activity_hours_RV"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add extra hours"
android:textColor="@color/green_test"
android:id="@+id/activity_hours_addHoursTV"/>
</LinearLayout>
还有我的childxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_hours_RV"/>
</LinearLayout>
最后是我的日志错误:
10-14 21:42:26.804 18552-18552/com.daprlabs.aaron.swipedeck2 E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter$MyViewHolder.<init>(HoursActivity.java:97)
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter.onCreateViewHolder(HoursActivity.java:73)
at com.daprlabs.aaron.swipedeck2.CardInfo.HoursActivity$HoursRecyclerViewAdapter.onCreateViewHolder(HoursActivity.java:57)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2843)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2685)
at android.view.View.measure(View.java:18834)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1237)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1474)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6100)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.re
删除
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
从您的 MyViewHolder class 并将该行添加到您的 onBindViewHolder
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
holder.recyclerView.setLayoutManager(linearLayoutManager);