我正在学习片段,我做了这个应用程序来显示食谱列表,这个列表的每一行都有食谱的图像和食谱的名称,我把recyclerView放在一个片段里,但问题是当我打开应用程序时,recyclelerView的内容变得混乱并在向下滚动后恢复正常,如下面的两个屏幕截图所示。
MainActivity类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListFragment listFragment = new ListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.placeHolder, listFragment);
fragmentTransaction.commit();
}}
ListAdapter类
public class ListAdapter extends RecyclerView.Adapter{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,
false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((ListViewHolder) holder).bindView(position);
}
@Override
public int getItemCount() {
return Recipes.names.length;
}
private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mRecipeText;
private ImageView mRecipeImage;
public ListViewHolder(View itemView) {
super(itemView);
mRecipeImage = itemView.findViewById(R.id.itemImage);
mRecipeText = itemView.findViewById(R.id.itemText);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
}
public void bindView(int position) {
mRecipeText.setText(Recipes.names[position]);
mRecipeImage.setImageResource(Recipes.resourceIds[position]);
}
}}
listFragment class
public class ListFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container , false);
RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/placeHolder"
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"
tools:context="com.example.elswefi.smellslikebakin.MainActivity">
</FrameLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/listRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
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="wrap_content">
<ImageView
android:id="@+id/itemImage"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_margin="8dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/bagels"
/>
<TextView
android:id="@+id/itemText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_margin="12dp"
android:text="TextView"
android:textSize="24sp"
/>
</LinearLayout>
我希望有人可以帮助我,因为我还在学习,我不知道这个bug来自哪里。
答案 0 :(得分:0)
问题已解决
我通过添加这行代码解决了这个问题
android:adjustViewBounds="true"
到list_item.xml文件中的imageView
谢谢你们的评论,我真的很感激。