2016-10-05 3 views
0

私は、RecyclerViewと他のビューを備えたフラグメントナビゲーションドロワーを持っています。私がrecyclerviewの項目に指を置くたびに、選択した状態の色は表示されません。私は必要なすべてのコードを入れましたが。フラグメント引き出しRecyclerViewリップルエフェクトが得られないアイテム

RecyclerViewアイテム行:

<LinearLayout android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingTop="15dp" 
android:paddingBottom="15dp" 
android:paddingEnd="10dp" 
android:paddingLeft="10dp" 
android:paddingRight="10dp" 
android:paddingStart="10dp" 
android:clickable="true" 
android:focusable="true" 
android:background="?android:attr/selectableItemBackground" 
android:id="@+id/layout" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 



<ImageView 
    android:layout_width="26dp" 
    android:layout_height="26dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginStart="10dp" 
    android:layout_gravity="center_vertical" 
    android:id="@+id/icon"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:layout_marginStart="15dp" 
    android:textSize="16sp" 
    android:layout_gravity="center_vertical" 
    android:id="@+id/title"/> 

    </LinearLayout> 

RecyclerViewアダプタ:

if(selectedPosition == position){ 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.dark_red)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red)); 
    } else { 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.black)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.black)); 
    } 

しかし、このコードは次のとおりです。

List<NavDrawerItems> data = Collections.emptyList(); 
private LayoutInflater inflater; 
private Context context; 
int selectedPosition = 0; 


public NavigationDrawerAdapter(Context context, List<NavDrawerItems> data) { 
    this.context = context; 
    inflater = LayoutInflater.from(context); 
    this.data = data; 
} 

public void delete(int position) { 
    data.remove(position); 
    notifyItemRemoved(position); 
} 

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

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    if(selectedPosition == position){ 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.dark_red)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red)); 
    } else { 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.black)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.black)); 
    } 
    NavDrawerItems current = data.get(position); 
    holder.title.setText(current.getTitle()); 
    holder.icon.setImageResource(current.getIcon()); 

} 



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

class MyViewHolder extends RecyclerView.ViewHolder { 
    TextView title; 
    ImageView icon; 
    LinearLayout layout; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.title); 
     icon = (ImageView) itemView.findViewById(R.id.icon); 
     layout = (LinearLayout) itemView.findViewById(R.id.layout); 
    } 
} 

私はRecyclerViewアダプタからこのコードを削除した場合、それが正常に動作します現在選択されているアイテムをrecyclerviewに表示するために使用されます。どのように私はそれらの両方を実装することができます。

答えて

1

私はこの問題を、recyclerviewアイテムのコードを変更して解決しました。

<LinearLayout android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/layout" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingTop="15dp" 
    android:paddingBottom="15dp" 
    android:paddingEnd="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingStart="10dp" 
    android:clickable="true" 
    android:focusable="true" 
    android:background="?android:attr/selectableItemBackground"> 

    <ImageView 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     android:layout_gravity="center_vertical" 
     android:id="@+id/icon"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginStart="15dp" 
     android:textSize="16sp" 
     android:layout_gravity="center_vertical" 
     android:id="@+id/title"/> 

    </LinearLayout> 

</LinearLayout> 

私がバックグラウンドで設定されたレイアウトの背景色を変えた、と思われる:

android:background="?android:attr/selectableItemBackground" 

だから私は新しいレイアウトを追加し、その中にコンテンツを移します。

関連する問題