我是Android的新手,正在尝试在垂直回收视图中创建水平回收视图。
我一直在关注此博客:https://android-pratap.blogspot.com/2015/12/horizontal-recyclerview-in-vertical.html
但是我认为我的pj中缺少某些内容,但无法弄清楚。我的应用程序在打开时崩溃。
RecycleviewAdapter.java
public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.recycleviewHolder> {
private final ArrayList<String> cinemalist;
//private LayoutInflater inflateboss;
private Context mcontext;
CinemaShowtimeRepository dataRepository;
Date startTime = new Date(0, 0, 0, 10, 30);
List<Showtime> listTimes = dataRepository.getListShowtimes(startTime);
@NonNull
@Override
public recycleviewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View cinemaView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cinema_view,null);
recycleviewHolder holder=new recycleviewHolder(cinemaView);
//return new recycleviewHolder(cinemaView,this);
return holder;
}
@Override
public void onBindViewHolder(@NonNull recycleviewHolder recycleviewHolder, int i) {
String current=cinemalist.get(i);
cinemaAdapter showlist= new cinemaAdapter(mcontext,listTimes);
recycleviewHolder.recyclerchild.setLayoutManager(new LinearLayoutManager(mcontext,LinearLayoutManager.HORIZONTAL,false));
recycleviewHolder.recyclerchild.setAdapter(showlist);
recycleviewHolder.cinema.setText(current);
recycleviewHolder.recyclerchild.setNestedScrollingEnabled(false);
}
@Override
public int getItemCount() {
return cinemalist.size();
}
class recycleviewHolder extends RecyclerView.ViewHolder{
public final RecyclerView recyclerchild;
public final TextView cinema;
//final RecycleviewAdapter adapterboss;
public recycleviewHolder(@NonNull View cinemaView){
super((cinemaView));
recyclerchild=cinemaView.findViewById(R.id.recyleview1);
cinema=cinemaView.findViewById(R.id.cinemaName);
//this.adapterboss=adapter;
}
}
public RecycleviewAdapter(Context context,ArrayList<String> cinema){
this.cinemalist=cinema;
this.mcontext=context;
//inflateboss=LayoutInflater.from(context);
}
}
cinemaAdapter.java
public class cinemaAdapter extends RecyclerView.Adapter<cinemaAdapter.cinemaViewHolder> {
//private final ArrayList<String> cinemalist; //test array
private final List<Showtime> showtimelist;
private LayoutInflater inflate;
class cinemaViewHolder extends RecyclerView.ViewHolder {
//public final TextView cinemaItemView;
public final Button showButton;
final cinemaAdapter madapter;
public cinemaViewHolder(@NonNull View itemView,cinemaAdapter adapter) {
super(itemView);
//cinemaItemView=itemView.findViewById(R.id.word);
showButton=itemView.findViewById(R.id.showtime_button);
this.madapter=adapter;
}
}
/* TEST ADAPTER
public cinemaAdapter(Context context,ArrayList<String> list){
inflate=LayoutInflater.from(context);
this.cinemalist=list;
}*/
// NEW ADAPTER
public cinemaAdapter(Context context, List<Showtime> showtimes){
inflate=LayoutInflater.from(context);
this.showtimelist=showtimes;
}
@NonNull
@Override
public cinemaAdapter.cinemaViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View cinemaView=inflate.inflate(R.layout.showtime_view,viewGroup,false);
return new cinemaViewHolder(cinemaView,this);
}
@Override
public void onBindViewHolder(@NonNull cinemaAdapter.cinemaViewHolder cinemaViewHolder, int i) {
//String mcurrent=cinemalist.get(i);
//cinemaViewHolder.cinemaItemView.setText(mcurrent);
String mcurrent=String.format( "%d : %d",showtimelist.get(i).getTime().getHours(),showtimelist.get(i).getTime().getMinutes());
cinemaViewHolder.showButton.setText(mcurrent);
}
@Override
public int getItemCount() {
//return cinemalist.size();
return showtimelist.size();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();;
private final ArrayList<String> cinemalist=new ArrayList<>();
private RecyclerView mRecycleView;
private cinemaAdapter madapter;
private RecycleviewAdapter radapter;
CinemashowtimeDataSource dataSource;
CinemaShowtimeRepository dataRepository;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().hide();
//initial arraylist
cinemalist.add("Cgv The Dark Kngiht");
cinemalist.add("Cgv Avater");
cinemalist.add("Cgv Star Wars");
cinemalist.add("Cgv Avenger");
cinemalist.add("Cgv Night crawler");
cinemalist.add("Cgv Southpaw");
//////////////////////////////////
dataSource = CinemashowtimeDataSource.getInstance();
dataRepository = CinemaShowtimeRepository.getInstance(dataSource);
// get the sample movie information data from data repository
MovieInfo movie = dataRepository.getMovieInfo();
Log.d(TAG, String.format("Movie title: %s", movie.getMovieTitle()));
// get list of date from data repository with start date. It will get 10 dates from dummy
// data source
Date startDate = new Date(2019, 05, 28);
List<Date> listDates = dataRepository.getListDates(startDate);
for (Date date : listDates) {
Log.d(
TAG,
String.format("d/M/y = %d / %d / %d ;", date.getDate(), date.getMonth(), date.getYear()));
}
// get list of showtimes from data repository with starting time. It will get 10 showtimes
// from dummy data source
Date startTime = new Date(0, 0, 0, 10, 30);
List<Showtime> listTimes = dataRepository.getListShowtimes(startTime);
//horizontal recycleview working
//mRecycleView =findViewById(R.id.parent_recycleview);
//madapter=new cinemaAdapter(this,listTimes);
//mRecycleView.setAdapter(madapter);
//mRecycleView.setLayoutManager(new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false));
mRecycleView =findViewById(R.id.parent_recycleview);
radapter=new RecycleviewAdapter(this,cinemalist);
mRecycleView.setAdapter(madapter);
mRecycleView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
for (Showtime time : listTimes) {
Log.d(
TAG,
String.format(
"h:m = %d : %d; is available = %b",
time.getTime().getHours(), time.getTime().getMinutes(), time.isAvailable()));
}
//////////////////////////////////////////////////
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/parent_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
cinemaview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<TextView
android:id="@+id/cinemaName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CGV blah blah blah"
android:textColor="#000000"
android:layout_marginStart="20dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyleview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
showtime_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="26dp"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:layout_height="26dp"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:id="@+id/showtime_button"
android:background="@drawable/button_border"
android:text=""
android:elevation="4dp"
android:clickable="true"
android:focusable="auto"/>
</android.support.v7.widget.CardView>