2016-10-27 6 views
4

私はアンドロイドアプリプロジェクトのようなノートパッドで作業しています。私はリサイクラーを実装しています。 私のプロジェクトは、私がクリックリスナーを使用し、以下のコードを使用して、そのクラスにRecyclerView.Adapter<NotesAdapter.ViewHolder>
を拡張するクラスNotedAdaperリクリューサとアンドロイドを使用したロングクリックリスナーの操作

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> { 

private List<Notes> mNotes; 
private Context mContext; 

public NotesAdapter(Context context, List<Notes> notes) { 
    mNotes = notes; 
    mContext = context; 
} 


@Override 
public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Context context = parent.getContext(); 
    LayoutInflater inflater = LayoutInflater.from(context); 

    // Inflate the custom layout 
    View notesView = inflater.inflate(R.layout.items_notes, parent, false); 

    // Return a new holder instance 
    ViewHolder viewHolder = new ViewHolder(notesView); 
    return viewHolder; 
} 


// Easy access to the context object in the recyclerview 
private Context getContext() { 
    return mContext; 
} 

@Override 
public void onBindViewHolder(NotesAdapter.ViewHolder viewHolder, final int position) { 

    // Get the data model based on position 
    Notes notes = mNotes.get(position); 

    // Set item views based on your views and data model 
    TextView textView = viewHolder.preTitle; 
    textView.setText(notes.getTitle()); 
    TextView textView1 = viewHolder.preText; 
    textView1.setText(notes.getText()); 
    String color=notes.getColor(); 

    CardView preCard=viewHolder.preCard; 
    preCard.setBackgroundColor(Color.parseColor(color)); 
    ImageView img = viewHolder.preImage; 
    img.setVisibility(View.GONE); 

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Notes notes = mNotes.get(position); 
      Intent intent = new Intent(view.getContext(),EditNote.class); 

      Bundle bundle = new Bundle(); 
      bundle.putSerializable("DATA",notes); 
      intent.putExtras(bundle); 
      getContext().startActivity(intent); 

      Toast.makeText(getContext(), "Recycle Click" + position+" ", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

// Returns the total count of items in the list 
@Override 
public int getItemCount() { 
    return mNotes.size(); 
} 

public static class ViewHolder extends RecyclerView.ViewHolder { 
    // Your holder should contain a member variable 
    // for any view that will be set as you render a row 
    public RobotoTextView preTitle, preText; 
    public ImageView preImage; 
    public CardView preCard; 

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

     preTitle = (RobotoTextView) itemView.findViewById(R.id.preTitle); 
     preText = (RobotoTextView) itemView.findViewById(R.id.preText); 
     preImage=(ImageView) itemView.findViewById(R.id.preImage); 
     preCard=(CardView) itemView.findViewById(R.id.preCard); 

    } 
}} 

とその絶対に取り組んで発見されています。リサイクラー内のアイテムをクリックすると、そのアイテムの位置を使用してデータを取得します。別のアクティビティで表示されます。 ちょうど、アクティビティがユーザーによって作成されたメモのリストを表示しているとします。ノートをクリックすると、そのノートの全内容が表示されます。

しかし、私はアイテムに長いクリックリスナーを実装したいと思います。位置を取得します。 ように、私は、そう...

viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       Notes notes = mNotes.get(position); 
       Toast.makeText(getContext(), "long Click" + position+" ", Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 

を次のコードを使用しても取り組んでいます。 しかし、私がしたいことは、長いクリックで、それはトーストだけを示すべきです。 しかし、それは長いクリックトーストを示すだけではありません。クリックリスナーを認識し、トーストを表示した後>> "長いクリック:..."は、シングルクリックイベントのために書かれたコードを実行します。 n私はそれを望んでいません。 両方のリスナーは別々に動作する必要があります。 しかし、その長いクリックの後にシングルクリックを実行する理由何か案が???
どこでも間違いはありますか?

答えて

12

私のコードでは、次のように変更されました。私の出力を達成するのに役立ちます。 1)あなたのビューをデータにバインドするたびに、onBindViewHolderメソッドが呼び出されます。クリックリスナを設定するのに最適な場所はありません。 1つのViewに何度もOnClickListenerを設定する必要はありません。ザッツなぜ、私は、(私はそれを次のですThatsなぜ実際にそれは私の質問はありませんでしたが、私はそれがベストプラクティスだとどこかで読ん)このような

、ViewHolderにクリックリスナーを書いた

public static class ViewHolder extends RecyclerView.ViewHolder { 
     // Your holder should contain a member variable 
     // for any view that will be set as you render a row 
     public ImageView preImage; 
     public CardView preCard; 

     // We also create a constructor that accepts the entire item row 
     // and does the view lookups to find each subview 
     public ViewHolder(final View itemView) { 
      // Stores the itemView in a public final member variable that can be used 
      // to access the context from any ViewHolder instance. 
      super(itemView); 
      itemView.findViewById(R.id.preTitle); 
      preImage=(ImageView) itemView.findViewById(R.id.preImage); 
      preCard=(CardView) itemView.findViewById(R.id.preCard); 


      itemView.setOnLongClickListener(new View.OnLongClickListener() { 
       @Override 
       public boolean onLongClick(View view) { 
        int p=getLayoutPosition(); 
        System.out.println("LongClick: "+p); 
        return true;// returning true instead of false, works for me 
       } 
      }); 

      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        int p=getLayoutPosition(); 

        Notes notes = mNotes.get(p);  
        Toast.makeText(getContext(), "Recycle Click" + p +" ", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

あなたは、onLongClickにおいて、私が "真"を返したことに気付くかもしれません、それは "偽"でした。 この変更は私には役に立ちます。

+0

あまりにもあなたを解決する必要があります私の問題を解決しました。私は疑問がある "itemView"とは何ですか?私たちがどこから得るのかを意味します。 @ヴァシャバランジュ –

1

あなたは両方のリスナーをViewHolderクラスから設定する必要があると思います。

itemView.setOnClickListener(...); 
itemView.setOnLongClickListener(...); 

そしてアイテムのアダプタの位置を取得するためにViewHolderからgetAdapterPosition()を呼び出します。

以下のリソースをチェックアウトすることができます。 https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/

2

だけではなく、return falseonLongClick(View v)戻りreturn trueを作るこれは、それが、おかげで働いた

関連する問題