2017-03-17 72 views
1

我在RecyclerView Adapter Item中使用viewFlipper,但它以異常的方式工作,就像我翻轉視圖然後滾動一樣,recyclerView中的另一個視圖也會翻轉。請幫我解決這個問題..RecyclerView ViewHolder中的ViewFlipper

代碼爲RecyclerView適配器:

public class SelfStudyAdapter extends RecyclerView.Adapter<SelfStudyAdapter.ViewHolder> { 
    private BaseActivity context; 
    private LayoutInflater inflater; 
    private List<SelfStudyModel> dataList = new ArrayList<>(); 
    private AdapterClickListener adapterClickListener; 

    public SelfStudyAdapter(BaseActivity context, List<SelfStudyModel> dataList) { 
     this.context = context; 
     this.dataList = dataList; 
     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = inflater.inflate(R.layout.item_new_learning_path, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     SelfStudyModel data = dataList.get(position); 

     int getLastPercentage = Math.round(data.getLastTestPercentage()); 
     holder.txtChapter.setText(data.getChapter().getName()); 
     holder.txtChapterNo.setText(context.getResources().getString(R.string.chapter) + " " + data.getChapter().getNumber()); 
     holder.txtChapter2.setText(data.getChapter().getName()); 
     holder.txtChapterNo2.setText(context.getResources().getString(R.string.chapter) + " " + data.getChapter().getNumber()); 
     if (getLastPercentage > 0) { 
      holder.txtMarksPercentage2.setText(String.valueOf(getLastPercentage)); 
      holder.progressMarksPercentage2.setProgress(getLastPercentage); 
      holder.flProgressBar2.setVisibility(View.VISIBLE); 
      holder.txtLastPercentage2.setVisibility(View.VISIBLE); 
      holder.txtMarksPercentage2.setVisibility(View.VISIBLE); 
      holder.progressMarksPercentage2.setVisibility(View.VISIBLE); 
     } else { 
      holder.flProgressBar2.setVisibility(View.INVISIBLE); 
      holder.txtLastPercentage2.setVisibility(View.INVISIBLE); 
      holder.txtMarksPercentage2.setVisibility(View.INVISIBLE); 
      holder.progressMarksPercentage2.setVisibility(View.INVISIBLE); 
     } 

     holder.btnLearning2.setText(data.getLqViewed() + "/" + data.getLqTotal()); 
     holder.txtPqViewed.setText(String.valueOf(data.getWqViewed())); 
     holder.txtPqTotal.setText("/" + data.getWqTotal()); 
     holder.btnSummary2.setText(data.getSummaryTimeSpent() + " min"); 
     holder.txtPqUp.setText(String.valueOf(data.getFlaggedWqTotal())); 
     holder.txtPqDown.setText(String.valueOf(data.getNeedHelpWqTotal())); 
    } 

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

    public AdapterClickListener getAdapterClickListener() { 
     return adapterClickListener; 
    } 

    public void setAdapterClickListener(AdapterClickListener adapterClickListener) { 
     this.adapterClickListener = adapterClickListener; 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

     protected ViewFlipper viewFlipper; 
     protected TextView txtChapterNo, txtChapter; 
     protected Button btnSummary, btnLearning, btnPractise; 

     protected TextView txtChapterNo2, txtChapter2; 
     protected Button btnSummary2, btnLearning2; 
     protected LinearLayout llPractise2; 
     protected TextView txtPqViewed, txtPqTotal, txtPqUp, txtPqDown; 
     protected TextView txtLastPercentage2; 
     protected ProgressBar progressMarksPercentage2; 
     protected TextView txtMarksPercentage2; 
     protected FrameLayout flProgressBar2; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      viewFlipper = (ViewFlipper) itemView.findViewById(R.id.view_flipper); 
      txtChapterNo = (TextView) itemView.findViewById(R.id.txt_chapter_no); 
      txtChapter = (TextView) itemView.findViewById(R.id.txt_chapter); 
      btnSummary = (Button) itemView.findViewById(R.id.btn_summary); 
      btnLearning = (Button) itemView.findViewById(R.id.btn_learning); 
      btnPractise = (Button) itemView.findViewById(R.id.btn_practise); 

      txtChapterNo2 = (TextView) itemView.findViewById(R.id.txt_chapter_no2); 
      txtChapter2 = (TextView) itemView.findViewById(R.id.txt_chapter2); 
      btnSummary2 = (Button) itemView.findViewById(R.id.btn_summary2); 
      btnLearning2 = (Button) itemView.findViewById(R.id.btn_learning2); 
      llPractise2 = (LinearLayout) itemView.findViewById(R.id.ll_practice2); 
      txtPqViewed = (TextView) itemView.findViewById(R.id.txt_pq_viewed); 
      txtPqTotal = (TextView) itemView.findViewById(R.id.txt_pq_total); 
      txtPqUp = (TextView) itemView.findViewById(R.id.txt_pq_up); 
      txtPqDown = (TextView) itemView.findViewById(R.id.txt_pq_down); 
      txtMarksPercentage2 = (TextView) itemView.findViewById(R.id.txt_marks_percentage2); 
      txtLastPercentage2 = (TextView) itemView.findViewById(R.id.txt_last_percentage2); 
      progressMarksPercentage2 = (ProgressBar) itemView.findViewById(R.id.progress_marks_percentage2); 
      flProgressBar2 = (FrameLayout) itemView.findViewById(R.id.fl_progress_bar2); 

      btnSummary.setOnClickListener(this); 
      btnLearning.setOnClickListener(this); 
      btnPractise.setOnClickListener(this); 

      btnSummary2.setOnClickListener(this); 
      btnLearning2.setOnClickListener(this); 
      llPractise2.setOnClickListener(this); 

      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        AnimationFactory.flipTransition(viewFlipper, AnimationFactory.FlipDirection.TOP_BOTTOM); 
       } 
      }); 
     } 

     @Override 
     public void onClick(View v) { 
      if (getAdapterClickListener() != null) { 
       getAdapterClickListener().onClick(getAdapterPosition(), v); 
      } 
     } 
    } 
} 

適配器項目的佈局:

<?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"> 

    <ViewFlipper 
     android:id="@+id/view_flipper" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <include layout="@layout/layout1"/> 

     <include layout="@layout/layout2"/> 

    </ViewFlipper> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/line_dimen" 
     android:layout_alignParentBottom="true" 
     android:background="@color/gray_700" /> 

</RelativeLayout> 
+0

共享代碼和其他信息。 – Rachit

+0

分享代碼.. –

回答

0

我不得不使用ViewSwitcher類似的問題。如果你希望它永遠回收到第一子視圖,您可以在onBindViewHolder設置此:

resetFlipper(holder.viewFlipper); 

然後創建一個輔助函數來設置兒童,而不會觸發動畫:

private void resetFlipper(ViewFlipper vf){ 
    Animation inAnimation = vf.getInAnimation(); 
    Animation outAnimation = vf.getOutAnimation(); 
    vf.setInAnimation(null); 
    vf.setOutAnimation(null); 
    vf.setDisplayedChild(0); 
    vf.setInAnimation(inAnimation); 
    vf.setOutAnimation(outAnimation); 
} 
+0

使用,但問題是當顯示第一個子視圖時,它動畫.. –

+0

@SahilMunjal我也必須解決這個問題,我已經更新了我的答案。 –

相關問題