Reputation: 111
Can someone help me try get my RecyclerView to appear. It appears if I do not implement it in a fragment. However, when I tried implementing it into a fragment my other XML code in (CarFront) was displayed apart from the RecyclerView.
I'm getting this error in my log: "E/RecyclerView: No adapter attached; skipping layout"
Below is my code, if you need any other information or other sections of code please feel free to comment below. Thank you very much.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
//holder.countryName.setText(itemList.get(position).getName());
holder.photo.setImageResource(itemList.get(position).getPhoto());
}
@Override
public int getItemCount() {
Log.e("Test", "" + this.itemList.size());
return this.itemList.size();
}
}
public class Welcome extends AppCompatActivity {
// Declaring variables
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private LinearLayout sectionLayout;
private TextView[] sections;
private int[] welcome_screen;
private Button Skip, Next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Changing the window to fullscreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
// Hiding the action bar to make it fullscreen
getSupportActionBar().hide();
// Setting the variables equal to their counter-part in the .xml
viewPager = (ViewPager) findViewById(R.id.view_pager);
sectionLayout = (LinearLayout) findViewById(R.id.sections_layout);
Skip = (Button) findViewById(R.id.skip);
Next = (Button) findViewById(R.id.next);
// Adding the various layouts to the welcome screen
welcome_screen = new int[]{
R.layout.activity_car_front,
R.layout.activity_car_side};
// adding sections
SectionsDesign(0);
viewPagerAdapter = new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
//RecyclerView
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.front_recyclerview);
}
public void nextclick(View v)
{
// Checks to see if it has reached the homescreen, if so proceed
int current = getItem(1);
if (current < welcome_screen.length) {
// moving to the next section
viewPager.setCurrentItem(current);
} else {
launchHomeScreen();
}
}
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
SectionsDesign(position);
// When it reaches the last section, show launch button
if (position == welcome_screen.length - 1) {
// last page. make button text to GOT IT
Next.setText("LAUNCH");
Skip.setVisibility(View.GONE);
} else {
// Physical next button (blank for design)
Next.setText("");
Skip.setVisibility(View.VISIBLE);
}
}
// Not using but need
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
private void SectionsDesign(int currentPage) {
// Getting how many sections there are
sections = new TextView[welcome_screen.length];
sectionLayout.removeAllViews();
for (int i = 0; i < sections.length; i++) {
sections[i] = new TextView(this);
// Shows how many sections there are
sections[i].setText(Html.fromHtml("•"));
// Size of the sections slider (dot)
sections[i].setTextSize(35);
// For other sections that isn't the one you are on
sections[i].setTextColor(getResources().getColor(R.color.section_inactive));
sectionLayout.addView(sections[i]);
}
if (sections.length > 0)
// Change colour to active
sections[currentPage].setTextColor(getResources().getColor(R.color.section_active));
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
// Method for switching to the main screen
private void launchHomeScreen() {
startActivity(new Intent(this, Form.class));
finish();
}
public class ViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public ViewPagerAdapter() {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(welcome_screen[position], container, false);
container.addView(view);
return view;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
@Override
// Number of sections
public int getCount() {
return welcome_screen.length;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
public class CarFront extends AppCompatActivity {
private LinearLayoutManager lLayout;
public static ImageView fbumper;
public static ImageView hood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//remove title/fullscreen
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_car_front);
setTitle(null);
List<ItemObject> rowListItem = getAllItemList();
lLayout = new LinearLayoutManager(CarFront.this);
RecyclerView rView = (RecyclerView)findViewById(R.id.front_recyclerview);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(CarFront.this, rowListItem);
rView.setAdapter(rcAdapter);
//Selected Images
fbumper = (ImageView)findViewById(R.id.fbumperselected);
hood = (ImageView)findViewById(R.id.hoodselected);
}
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("", R.drawable.hood_selected));
//allItems.add(new ItemObject("", R.drawable.hood_card));
return allItems;
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public ViewHolder(View itemView) {
super(itemView);
}
}
}
<?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:orientation="vertical"
android:background="@color/background">
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/top_logo">
</android.support.v7.widget.Toolbar>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/carfront"
android:layout_width="368dp"
android:layout_height="200dp"
android:scaleType="fitStart"
app:srcCompat="@drawable/carfront"
tools:layout_editor_absoluteY="50dp"
tools:layout_editor_absoluteX="8dp" />
<ImageView
android:id="@+id/hoodselected"
android:layout_width="368dp"
android:layout_height="200dp"
android:scaleType="fitStart"
app:srcCompat="@drawable/hood_selected"
tools:layout_editor_absoluteY="50dp"
tools:layout_editor_absoluteX="8dp"
android:visibility="invisible"/>
<ImageView
android:id="@+id/fbumperselected"
android:layout_width="368dp"
android:layout_height="200dp"
android:scaleType="fitStart"
app:srcCompat="@drawable/fbumper_selected"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"
android:visibility="invisible"/>
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/front_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener{
//public TextView countryName;
public ImageView photo;
public ImageView fbumper;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnTouchListener(this);
photo = (ImageView)itemView.findViewById(R.id.cardview);
}
@Override
//make a gesture detector
//make a nice clean graphic after they tap on repair, telling the user how it works
//pointer = multitouch
public boolean onTouch(View view, MotionEvent event)
{
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
actionDown();
break;
case MotionEvent.ACTION_POINTER_UP:
actionUp();
break;
}
return true;
}
// Method for Down Press
public void actionDown()
{
switch (getAdapterPosition())
{
case 0:
CarFront.fbumper.setVisibility(View.VISIBLE);
break;
case 1:
}
}
//Method for Up Press
public void actionUp()
{
switch (getAdapterPosition())
{
case 0:
CarFront.fbumper.setVisibility(View.INVISIBLE);
//break;
case 1:
}
}
}
(I'm rubbish at trying to format and explain my problems. But, basically my RecyclerView is not displaying in my fragement. It is meant to display Cardview/s)
Upvotes: 5
Views: 8898
Reputation: 5988
Ok, first off, your Fragment should be structured more like the following. In your onCreateView
, just inflate the layout, and then in onViewCreated
, that's when you bind/populate your information.
And we're also using newInstance
, as that's the standard way to create new Fragments.
Note, I am using android.support.v4.app.Fragment
, not the older Fragment
.
public class CarFrontFragment extends Fragment {
public ImageView bumper; // Do not use static views.
public ImageView hood;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_car_front, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Bind your views.
RecyclerView list = (RecyclerView) view.findViewById(R.id.front_recyclerview);
bumper = (ImageView) view.findViewById(R.id.fbumperselected);
hood = (ImageView) view.findViewById(R.id.hoodselected);
// Create your layout manager.
LinearLayoutManager layout = new LinearLayoutManager(getContext());
list.setLayoutManager(layout);
// Fetch your items.
List<ItemObject> rowListItem = getAllItemList();
// Set your adapter
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), rowListItem);
list.setAdapter(adapter);
}
// Creating a new Fragment through newInstance.
public static CarFrontFragment newInstance() {
return new CarFrontFragment();
}
// Fetching items, passing in the View they will control.
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("", R.drawable.hood_selected), hood);
allItems.add(new ItemObject("", R.drawable.bumped_selected), bumper);
return allItems;
}
}
Now, as for your Adapter that will contain these, you want a FragmentStatePagerAdapter
. All this will do is bind the position to a Fragment
of your choosing.
public class PagerAdapter extends FragmentStatePagerAdapter {
public static final int NUM_OF_PAGES = 2;
private static final int PAGE_WELCOME = 0;
private static final int PAGE_CAR_FRONT = 1;
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case PAGE_WELCOME:
// Return a new instance of your Fragment.
case PAGE_CAR_FRONT:
return CarFrontFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
return NUM_OF_PAGES;
}
}
In the Adapter, all we're going to do is pass the View
the ItemObject is controlling.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.photo.setImageResource(itemList.get(position).getPhoto());
// Telling the holder about the View.
holder.view = itemList.get(position).getView();
}
@Override
public int getItemCount() {
return itemList.size();
}
}
And now, in the RecyclerViewHolders
, we'll toggle the View's visibility with onTouch
.
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener {
ImageView photo;
public View view;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnTouchListener(this);
photo = (ImageView) itemView.findViewById(R.id.cardview);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
this.view.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_POINTER_UP:
this.view.setVisibility(View.GONE);
break;
}
return true;
}
}
Upvotes: 5