-1

我使用的片段,其中是一個畫廊這表明你從我的自定義視頻庫中選擇視頻的縮略圖。 我在畫廊它可以幫助您從片段移動到活動選擇一些影片,然後再回來與填充在網格視圖中的數據相同的片段中使用的按鈕。在recyclerView增加兩個ViewHolders一起

問題陳述:我已經按照這個鏈接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 imlpement()實現的結果。

+1

你的意思'getItemViewType()'? – Bob

+0

耶正確。對不起這是我的錯。 –

回答

0

getItemViewType(),您在使用位置查詢數據並返回不同的整數值。一個佈局的一個int值。看起來你分別使用0和1來代替galleryViewimageButtonView。你有ArrayList<File>作爲RecyclerView項目。該列表應該有必要的數據來決定是否使用圖庫視圖或圖像按鈕視圖。

事情是這樣的:

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

您將在onCreateViewHolder回調,其中使用可以膨脹不同的佈局收到viewType int值。

+0

我沒有像isGalleryView()這樣的關聯到我的視圖,也沒有給我任何建議! –

+0

你必須在你的模型對象中實現這樣的東西。您應該有一些數據來決定您必須在給定位置使用哪個視圖。它不一定必須在該列表中。您必須決定您要爲給定位置膨脹哪個視圖,併爲佈局返回一個整數。 – Bob

+0

如果顯示文件,我必須在文件中附加位置,否則imagebuttonView必須位於0位置。爲此,我已經使用**如果(fileName.get(位置).isFile()),則返回0 + 1,否則1 **這是正確的,你可以幫我這一點,因爲這對我來說相當複雜。謝謝 –