【问题标题】:Adding two ViewHolders together in a recyclerView在 recyclerView 中添加两个 ViewHolder
【发布时间】:2017-08-17 13:28:06
【问题描述】:

我正在使用一个片段,它是一个画廊,它向您显示从我的自定义视频库中选择的视频的缩略图。 我在图库中使用了一个按钮,它可以帮助您从片段移动到您选择一些视频的活动,然后返回到网格视图中填充数据的同一片段。

问题陈述:我已经关注了这个链接Using two viewHolder in the same Adapter 并实现了新的东西,即在 Addfragment 中,我有一个 Imagebutton,它位于位置 0,当你点击按钮进入下一个活动,当一些视频被选中时,你再次来到 AddFragment,现在同一个 imageButton 改变了它的位置。

对于上述内容,我使用了两个 viewHolder,但由于对使用它的了解较少,我不知道如何使用它。请指导相同,以便我实现我愿意实现的目标。

1.AddFragment.java

public class AddFragment extends Fragment {

private ImageButton nextActivity;
private RecyclerView recyclerView;
ArrayList<File> checkedList = new ArrayList<>();
ImageAdapter imageAdapter;
Button button;
private static final int CustomGallerySelectId = 1;//Set Intent Id

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);

    nextActivity = (ImageButton) view.findViewById(R.id.gotoButton);

    recyclerView = (RecyclerView) view.findViewById(R.id.grid_add_view);

    button = (Button) view.findViewById(R.id.buttonToGallery);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(getContext(),VideoGalleryActivity.class),CustomGallerySelectId);
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.e("IT IS WORKING","YES");

    switch(requestCode){
        case CustomGallerySelectId :
            if(resultCode == RESULT_OK){
                Log.e("ADAPTER SETTING","DOING");
                //getting the passed value from videogallery
                ArrayList<String> getValue = data.getExtras().getStringArrayList("sendData");
                Log.e("RECEIVED_DATA======",data.getExtras().getSerializable("sendData").toString());

                //adding the files to the list
                for(String pathName : getValue) {
                    File filePath = new File(pathName);
                    checkedList.add(filePath);
                }

                //setting the adapter
                imageAdapter = new ImageAdapter(checkedList);
                GridLayoutManager videoGrid = new GridLayoutManager(getContext(),3);
                recyclerView.setLayoutManager(videoGrid);
                recyclerView.setAdapter(imageAdapter);
                recyclerView.setVisibility(View.VISIBLE);

            }
    }
}

//making adapter for RecyclerView which loads the desired files
class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

    class ViewHolderGalleryImage extends RecyclerView.ViewHolder {
        public ImageView imageView;


        public ViewHolderGalleryImage(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
        }
    }

    class ViewHolderImageButton extends RecyclerView.ViewHolder{

        public ImageButton imageButton;

        public ViewHolderImageButton(View itemView) {
            super(itemView);

            imageButton = (ImageButton) itemView.findViewById(R.id.gotoGalleryButton);

        }
    }

    @Override
    public int getItemViewType(int position) {

        if(fileName !=null){

        }
        return super.getItemViewType(position);

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch(viewType){
            case 0 : View galleryView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                    parent,false);
                galleryView.setLayoutParams(new AbsListView.LayoutParams(215,215));
                return new ViewHolderGalleryImage(galleryView);

            case 1 : View imageButtonView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_button_layout,
                    parent,false);
                imageButtonView.setLayoutParams(new AbsListView.LayoutParams(215,215));
                return new ViewHolderImageButton(imageButtonView);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (holder.getItemViewType()){
            case 0: if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);

        }
        }

    }

    @Override
    public int getItemCount() {
        return fileName.size();
    }
} }

布局 1,用于将 VideoGalery 中的图像部署到 AddFragment 中 custom_added_video.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp">

<ImageView
    android:id="@+id/galleryImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_margin="3dp"
    android:layout_centerInParent="true"/>

由图像按钮组成的布局 custom_button_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp">

<ImageButton
    android:id="@+id/gotoGalleryButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_border"
    android:layout_margin="3dp"
    android:src="@mipmap/ic_add_black_24dp"
    android:layout_centerInParent="true"/>

我很困惑是否要在 getItemViewType() 中实现什么来实现结果。

【问题讨论】:

  • 你的意思是getItemViewType() 吗?
  • 是的,没错。对不起,我的坏。

标签: android android-layout android-recyclerview android-viewholder


【解决方案1】:

getItemViewType() 中,您必须使用位置检查数据并返回不同的整数值。一种布局的一个 int 值。看起来您分别使用 0 和 1 表示 galleryViewimageButtonView。你有ArrayList&lt;File&gt; 作为RecyclerView 项目。该列表应该有必要的数据来决定是使用画廊视图还是图像按钮视图。

类似这样的:

public int getItemViewType(int position) {
    if (fileName.size() == posiiton) {
        return 1;
    } else {
        return 0;
    }
}

您将在onCreateViewHolder 回调中收到viewType int 值,使用它您可以扩展不同的布局。

【讨论】:

  • 我没有类似 isGalleryView() 的东西与我的视图相关联,也没有任何建议!
  • 你必须在你的模型对象中实现这样的东西。您应该有一些数据来决定您必须为给定位置使用哪个视图。它不一定必须在该列表中。您必须决定要为给定位置膨胀哪个 View 并为布局返回一个整数。
  • 如果显示文件,我必须将位置附加到文件中,否则 imagebuttonView 必须位于 0 位置。为此,我使用了 if(fileName.get(position).isFile()) then return 0+1 else 1 这是正确的,你能帮我解决这个问题吗,因为这对我来说很复杂.谢谢
  • 如果有图片,你想在哪里显示按钮?
  • 在最后一个位置,如果没有图像,它只是附加 ImageView,然后按钮将在左侧位置 0,如果假设添加了两个文件,那么按钮必须在 pos = 2(0 = image1,1=image2 and 2 = imagebutton)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多