我跟着this tutorial在回收者视图中显示我的物品。只有我做的改编是在片段而不是活动中显示它们。在我的应用程序中,我有一个登录屏幕,当按下按钮时,用户将用户重定向到活动。启动屏幕正常工作但按下按钮(→打开活动和片段)我得到的是一个黑屏。 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);
}
}
}
尝试像这样调试
尝试更改:
使用框架布局而不是
如果你能看到黄色然后开始查看包裹的布局可能会有一些问题。
还可以共享更多代码,例如recyclerview适配器行布局,以获得更好的帮助。
1.检查android.support.v4.app.Fragment片段的导入 2.使用下面的代码替换片段
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
}
return false;
}
以下代码存在问题:
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
确保在所有片段中正确使用OnFragmentInteractionListener接口。