Android:RecyclerView 不显示片段中的列表项
Posted
技术标签:
【中文标题】Android:RecyclerView 不显示片段中的列表项【英文标题】:Android: RecyclerView not showing List Items in a Fragment 【发布时间】:2018-01-31 08:25:11 【问题描述】:谁能帮我尝试让我的 RecyclerView 出现。如果我没有在片段中实现它,它就会出现。但是,当我尝试将其实现为片段时,我在 (CarFront) 中的其他 XML 代码与 RecyclerView 分开显示。
我的日志中出现此错误: “E/RecyclerView:未连接适配器;跳过布局”
以下是我的代码,如果您需要任何其他信息或其他代码部分,请随时在下面发表评论。非常感谢。
适配器:
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);
CarFront(片段中我的观点之一):
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):
<?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_
android:layout_
android:orientation="vertical"
android:background="@color/background">
<android.support.v7.widget.Toolbar
android:layout_
android:layout_
android:background="@drawable/top_logo">
</android.support.v7.widget.Toolbar>
<android.support.constraint.ConstraintLayout
android:layout_
android:layout_>
<ImageView
android:id="@+id/carfront"
android:layout_
android:layout_
android:scaleType="fitStart"
app:srcCompat="@drawable/carfront"
tools:layout_editor_absoluteY="50dp"
tools:layout_editor_absoluteX="8dp" />
<ImageView
android:id="@+id/hoodselected"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:scrollbars="none" />
</LinearLayout>
RecyclerViewHolder:
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:
(我在尝试格式化和解释我的问题时很垃圾。但是,基本上我的 RecyclerView 没有显示在我的片段中。它是为了显示 Cardview/s)
【问题讨论】:
您的CarFront
类没有扩展 Fragment,它正在扩展 AppCompatAcvitiy
。
你在哪里创建片段?我在你的代码中找不到。 Welcome
是一种受欢迎的教程吗?
好的,让我尝试帮助您修复设置中的一些项目。给我一点时间。
没关系,我只是试着重写一些东西,然后我们看看这是否有助于你走上正轨。
好的,我发布了一个关于如何构建Fragments
和ViewPager
的答案,请尝试检查一下。
【参考方案1】:
好的,首先,您的 Fragment 的结构应该更像以下内容。在您的 onCreateView
中,只需扩充布局,然后在 onViewCreated
中,您就可以绑定/填充您的信息。
我们还使用newInstance
,因为这是创建新片段的标准方式。
注意,我使用的是android.support.v4.app.Fragment
,而不是旧的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;
现在,对于包含这些的适配器,您需要一个FragmentStatePagerAdapter
。所有这一切都会将位置绑定到您选择的Fragment
。
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;
在适配器中,我们要做的就是传递 ItemObject 所控制的View
。
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();
现在,在RecyclerViewHolders
中,我们将使用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;
【讨论】:
再次感谢您(cmets 真的很有帮助)。这看起来真的很吓人。我担心我现在可能无法像以前那样与单个卡片进行交互。我可以发布我的 RecyclerViewHolder,这样您就可以快速查看我是否还能像以前一样进行交互。 还不错,只是Fragments和一个ViewPager的基本结构而已。你想如何像以前一样与卡片互动?很难准确说出您对每张卡的用途。RecyclerViewHolder
不在您发布的代码中。
好的,你想要的只是让汽车的bumper
隐藏或显示?您可以在 Fragment 中轻松处理它。您不需要对其进行静态引用。
我已经更新了答案,Fragment 现在实现了View.OnTouchListener
,您可以显示/隐藏保险杠。这样做你对 Fragment 有更多的控制,你的 Welcome
类不需要知道这些交互,Fragment 可以处理它们。
是的,只需将它们放入您的 Fragment 中,您就可以为该 Fragment 做任何您需要做的事情。以上是关于Android:RecyclerView 不显示片段中的列表项的主要内容,如果未能解决你的问题,请参考以下文章
android -------- 解决RecyclerView显示不全只显示一条item的问题
Android-RecyclerView系列 RecyclerView滑动后数据显示错乱
Android在滚动列表中实现视频的播放 ListView RecyclerView
scrollview嵌套recyclerview显示不全现象