Andorid recyclerview:以片段的垂直方向显示图像不起作用

问题描述 投票:0回答:1

我正在尝试使用LinearLayout Recyclerview在n个android片段中垂直显示图像列表。

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.recyclerview.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/imageListRecyclerView"
        android:name="com.abc.ImageFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp">

    </androidx.recyclerview.widget.RecyclerView>

</FrameLayout>

fragment_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/Image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:scaleType="fitCenter" />
</LinearLayout>

The Fragment class

public class ImagesFragment extends Fragment {

    private static final String TAG = ImagesFragment.class.getSimpleName();

    private OnImageFragmentInteractionListener mListener;
    private List<Image> images;

    public ImagesFragment() {
        images = new ArrayList<>();
    }

    public ImagesFragment(final List<Image> images) {
        this.images = images;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.imageListRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new ImagesRecyclerViewAdapter(this.images, mListener));
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnImageFragmentInteractionListener) {
            mListener = (OnImageFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnImageFragmentInteractionListener {
        // TODO: Update argument type and name
        void onImageFragmentInteraction(Image item);
    }
}

适配器类

public class ImagesRecyclerViewAdapter extends RecyclerView.Adapter<ImagesRecyclerViewAdapter.ImageViewHolder> {



    private final List<Image> images;
    private final OnImageFragmentInteractionListener mListener;

    public ImagesRecyclerViewAdapter(List<Image> items, OnImageFragmentInteractionListener listener) {
        images = items;
        mListener = listener;


    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

         View imageView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_image, parent, false);
    }

    @Override
    public void onBindViewHolder(final ImageViewHolder holder, int position) {
        holder.image = images.get(position);
        holder.mImageImage.setImageResource(ImageToImage.getImageResource(images.get(position)));

        holder.mImageImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onImageFragmentInteraction(holder.image);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return images == null ? 0 : images.size();
    }

    public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView mImageImage;
        public Image image;

        public ImageViewHolder(View view) {
            super(view);
            mImageImage = view.findViewById(R.id.Image);
        }

        @Override
        public void onClick(View view) {
        }
    }
}

阅读doc,我们会发现线性布局内的方向字段可用于更改它,我尝试将其设置为垂直和水平,它们都将图像水平显示,有人可以指出我在做什么吗? ?

android android-fragments android-recyclerview android-framelayout
1个回答
0
投票

请在RecyclerView标记中如下添加android:orientation="vertical" ...


<androidx.recyclerview.widget.RecyclerView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/imageListRecyclerView"
        android:name="com.abc.ImageFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp">
© www.soinside.com 2019 - 2024. All rights reserved.