我跟随this tutorial在回收者视图中显示了我的物品。我所做的唯一调整是将它们显示在Fragment而不是Activity中。 在我的App中,我有一个登录屏幕,按下按钮后会将用户重定向到“活动”。启动屏幕正常运行,但是当按下按钮(→打开活动和片段)时,我得到的只是一个黑屏。 Logcat输出不会显示任何错误,因为它不会显示任何类型的错误。我所得到的是:
I / ViewConfigCompat:在ViewConfiguration上找不到方法getScaledScrollFactor()
这听起来不仅仅是布局问题,而不是代码问题,或者我在这里错了吗?此错误可能是由于回收者视图中的某些错误引起的吗?
编辑:
经过大量调试,我至少可以确定调用ShopFragment类/视图时发生的错误。将默认片段设置为其他片段时,它将被渲染。但是一旦我进入ShopFragment,它就会变成空白并冻结。因此,请保持仁慈并帮助我查找错误:
ShopFragment:
public class ShopFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
protected View mView;
private OnFragmentInteractionListener mListener;
private ArrayList<Item> items;
public ShopFragment() {
}
public static ShopFragment newInstance(String param1, String param2) {
ShopFragment fragment = new ShopFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//get reference to recyclerView
View view = inflater.inflate(R.layout.fragment_shop, container, false);
this.mView = view;
RecyclerView itemView = mView.findViewById(R.id.rvCategories);
items = Item.ItemList();
ItemAdapter adapter = new ItemAdapter(items);
itemView.setAdapter(adapter);
itemView.setLayoutManager(new LinearLayoutManager(this.getContext()));
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
fragment_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvCategories"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
项目:
public class Item {
private static ArrayList<Item> mItemList;
private String mID, mTitle, mDescription, mProductType, mPictureLink, mCondition, mAvailability, mPrice, mBrand, mGtin, mMpn, mShippingCountry, mService, mShippingCosts, mpubDate;
public Item() {
}
public String getName() {
return mProductType;
}
public static ArrayList<Item> ItemList() {
XMLHandler itemFetcher = new XMLHandler();
itemFetcher.execute();
while (itemFetcher.processing()) {
}
mItemList = itemFetcher.getItems();
Log.i("ITEMS CONTENT", itemFetcher.getItems().toString());
return itemFetcher.getItems();
}
}
item_singleproduct.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/category_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
ItemAdapter
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> mCategories;
ViewHolder vh;
public ItemAdapter(List<Item> categories) {
mCategories = categories;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_singleproduct, parent, false);
vh = new ViewHolder(contactView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Item item = mCategories.get(position);
TextView textView = vh.nameTextView;
textView.setText(item.getmProductType());
}
@Override
public int getItemCount() {
return mCategories.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.category_name);
}
}
}
答案 0 :(得分:0)
尝试像这样调试
尝试更改:
使用框架布局,而不是
如果您能看到黄色,然后开始查看包装好的版面,肯定有问题。
还共享更多代码,例如回收者视图适配器的行布局,以获得更好的帮助。
答案 1 :(得分:0)
1。检查android.support.v4.app.Fragment的片段的导入
2.使用以下代码替换片段
s1 = s[s.groupby(level=0).transform('nunique') != 1]
答案 2 :(得分:0)
以下代码存在问题:
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
确保在所有片段中正确使用OnFragmentInteractionListener接口。