2017-10-11 14 views
1

私のRecyclerビューで、間違ったアイテムが選択されたアイテムと一緒に選択されたのはなぜですか? Image To Show Recycler View Problem私のRecyclerビューで、間違ったアイテムがクリックされた正確に選択されたアイテムと共に選択されるのはなぜですか?

クリックイベントで開始されたポップアップアクティビティにRecyclerViewを配置し、RecycleViewで選択したアイテムの背景をビューから変更する必要があります。私はそれがポップアップ表示させるために活動するために、次のコードを使用している

ポップアップアクティビティコード

DisplayMetrics metrics=new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     float width=metrics.widthPixels*8/10; 
     float height=metrics.heightPixels*6/10; 
     getWindow().setLayout((int)width, (int) height); 

public class PopUp extends Activity { 
    public int counter=0; 
    private int mItemSelected=-1; 
    public List<student> students=new ArrayList<student>(); 
    public RecyclerView recyclerView; 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //to set content view for that activity 
     setContentView(R.layout.popup); 
     //end of the setting the layout for the activity 

     //this is mechanism to calculate the width and height of the screen 
     DisplayMetrics metrics=new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     float width=metrics.widthPixels*8/10; 
     float height=metrics.heightPixels*6/10; 
     getWindow().setLayout((int)width, (int) height); 
     //end of the mechanism 

     //the method to populate the list 
     populateList(); 
     //end of the method 

     //now creating the recycler view 
      recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true); 
     // use a linear layout manager 
    LinearLayoutManager mLayoutManager = new 
    LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); 
     recyclerView.setLayoutManager(mLayoutManager); 
     MyAdapter adapter=new MyAdapter(students); 
     recyclerView.setAdapter(adapter); 
     //end of the recycler view 

    } 

    //this is event for the ok button 
    public void Ok(View view){ 

    } 
    //end of the ok button for the pop activity 

    //this is event for the button named as cancel 
    public void Cancel(View view){ 

    } 
    //end of the cancel button event 

    //this is method for printing line 
    public void PrintLine(String line){ 
     Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT).show(); 
    } 
    //end of the method 



    //this method to populate the ArrayList 
     public void populateList(){ 
      String name="Mashhood Qadeer Bhatti"; 
      String address="Sammundri Faisalabad"; 
      boolean status=false; 
      for(int i=0; i<10; i++){ 
       students.add(new student(name+"\t"+i,address,status)); 
      } 
     } 
    //end of the method 


    //this is section for recycler adapter 
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
     private List<student> values; 
       //this is constructor 
       public MyAdapter(List<student> myDataset) { 
        values = myDataset; 
       } 

     //end of the constructor 

     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      LayoutInflater inflater = LayoutInflater.from(
        parent.getContext()); 
      View v = inflater.inflate(R.layout.row_layout, parent, false); 
      // set the view's size, margins, paddings and layout parameters 
      ViewHolder vh = new ViewHolder(v); 
      return vh; 
     } 

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

       holder.name.setText(values.get(position).getName().toString()); 
       holder.address.setText(values.get(position).getAddress().toString()); 
       holder.status.setSelected(values.get(position).getSelction()); 
       holder.name.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if(mItemSelected==position){ 
          v.setBackground(getResources().getDrawable(R.drawable.im)); 
          PrintLine("The position matched"+position); 
          values.get(position).setSelction(!values.get(position).getSelction()); 
          holder.status.setChecked(values.get(position).getSelction()); 
         } 
        } 
       }); 
     } 

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

     //there will be view holder 
     public class ViewHolder extends RecyclerView.ViewHolder { 
      // each data item is just a string in this case 
      public TextView name; 
      public TextView address; 
      public View layout; 
      public RadioButton status; 

      public ViewHolder(View v) { 
       super(v); 
       layout = v; 
       name = (TextView) v.findViewById(R.id.name); 
       address = (TextView) v.findViewById(R.id.address); 
       status=(RadioButton) v.findViewById(R.id.status); 
       layout.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         notifyDataSetChanged(); 
         mItemSelected=getAdapterPosition(); 
        } 
       }); 
       /* v.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
           mItemSelected=getAdapterPosition(); 
           PrintLine("This is position for the"+getAdapterPosition()); 
           notifyDataSetChanged(); 
           values.get(mItemSelected).setSelction(!values.get(mItemSelected).getSelction()); 
         } 
        });*/ 

      } 
     } 

     //end of the view holder 


} 
//end of that section 
} 
+1

なく設けられた位置よりもユーザholder.getAdapterPosition()。 –

+0

私は学習の初期段階から何かを間違って書くと私を許します。私は私のViewAdapterでラジオボタンを持っているラジオボタンを持っているので、私はあなたのラジオを設定したい私がholder.status.setSelected()を使用している理由です。トグル機能を選択または選択しないようにするために反転されたブール値を与えるだけです。サー!もしholder.getAdapterPosition()を使用すると、アダプタのIDを取得し、その背景を変更する方法を教えてくれます。 – Mashhood

+0

ホルダーが私たちに与える位置は2つあり、onBindViewHolderMethodのint位置は可視の画面上の位置です。もう1つはアイテムの実際の位置であるholder.getAdapterPosition()です。だから、私はあなたに2番目のものを使うことを勧めます。 –

答えて

2

リサイクルを以下のようにマイリサイクラービュー・アダプタがあるため、コードビューはOnBindViewHolderのビューをリサイクルします。アイテムがクリックされると、他の位置に反映されます。これを解決するには

クリックした位置を保存するグローバル変数を作成します。

次に、内部のviewholderにclickListenerを追加し、クリックしたアイテムの位置をonClickストアします。

public class ViewHolder extends RecyclerView.ViewHolder { 
// each data item is just a string in this case 
public TextView name; 
public TextView address; 
public View layout; 
public RadioButton status; 

public ViewHolder(View v) { 
    super(v); 
    layout = v; 
    name = (TextView) v.findViewById(R.id.name); 
    address = (TextView) v.findViewById(R.id.address); 
    status = (RadioButton) v.findViewById(R.id.status); 
    v.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mItemSelected = getAdapterPosition(); 
      notifyDataSetChanged(); 

     } 
    }); 
    } 
} 

そしてOnBindViewHolder内部 において、

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

    holder.name.setText(values.get(position).getName().toString()); 
    holder.address.setText(values.get(position).getAddress().toString()); 
    holder.status.setSelected(values.get(position).getSelction()); 

    if(mItemSelected==position){ 
     holder.status.setChecked(true)‌; 
     v.setBackground(getResources().getDrawable(R.drawable.im)); 
    }else{ 
     holder.status.setChecked(false)‌; 
     v.setBackground(getResources().getDrawable(unselected Item)); 
    } 
} 
+0

サー!選択とバックグラウンドの変更は機能していますが、私のrecyclerViewは、[0]の位置で最初のアイテムを選択すると、間違ったアイテムを正確に選択し始めます。さらに、[7]を自動的に選択します。私はそれが選択されていない間違った項目を選択しないようにしたいと思います。 – Mashhood

+0

このコードは動作します..表示されなければならないコードは、選択を削除するための条件とコードがなければならないのです。更新されたコードを投稿してください – Anonymous

+0

私は条件付き構造としてコードを試しましたif(mItemSelected == position )とonBindViewHolderという名前のオーバーライドされたメソッドで取得する位置が、私のビューは、私のAdapterViewのちょっとした振る舞いである[0]インデックスの選択時に選択された7項目を示しています – Mashhood

0
public class PopUp extends Activity { 
    public int counter=0; 
    private int mItemSelected=-1; 
    public List<student> students=new ArrayList<student>(); 
    public RecyclerView recyclerView; 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //to set content view for that activity 
     setContentView(R.layout.popup); 
     //end of the setting the layout for the activity 

     //this is mechanism to calculate the width and height of the screen 
     DisplayMetrics metrics=new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     float width=metrics.widthPixels*8/10; 
     float height=metrics.heightPixels*6/10; 
     getWindow().setLayout((int)width, (int) height); 
     //end of the mechanism 

     //the method to populate the list 
     populateList(); 
     //end of the method 

     //now creating the recycler view 
      recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true); 
     // use a linear layout manager 
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); 
     recyclerView.setLayoutManager(mLayoutManager); 
     MyAdapter adapter=new MyAdapter(students); 
     recyclerView.setAdapter(adapter); 
     //end of the recycler view 

    } 

    //this is event for the ok button 
    public void Ok(View view){ 

    } 
    //end of the ok button for the pop activity 

    //this is event for the button named as cancel 
    public void Cancel(View view){ 

    } 
    //end of the cancel button event 

    //this is method for printing line 
    public void PrintLine(String line){ 
     Toast.makeText(getApplicationContext(),line,Toast.LENGTH_SHORT).show(); 
    } 
    //end of the method 



    //this method to populate the ArrayList 
     public void populateList(){ 
      String name="Mashhood Qadeer Bhatti"; 
      String address="Sammundri Faisalabad"; 
      boolean status=false; 
      for(int i=0; i<10; i++){ 
       students.add(new student(name+"\t"+i,address,status)); 
      } 
     } 
    //end of the method 


    //this is section for recycler adapter 
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
     private List<student> values; 
       //this is constructor 
       public MyAdapter(List<student> myDataset) { 
        values = myDataset; 
       } 

     //end of the constructor 

     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      LayoutInflater inflater = LayoutInflater.from(
        parent.getContext()); 
      View v = inflater.inflate(R.layout.row_layout, parent, false); 
      // set the view's size, margins, paddings and layout parameters 
      ViewHolder vh = new ViewHolder(v); 
      return vh; 
     } 

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

       holder.name.setText(values.get(position).getName().toString()); 
       holder.address.setText(values.get(position).getAddress().toString()); 
       holder.status.setSelected(values.get(position).getSelction()); 
       if(values.get(position).getSelction()) { 
        holder.layout.setBackground(getResources().getDrawable(R.drawable.im)); 
       } 
       else{ 
        holder.layout.setBackground(getResources().getDrawable(R.drawable.imagee)); 
       } 
     } 

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

     //there will be view holder 
     public class ViewHolder extends RecyclerView.ViewHolder { 
      // each data item is just a string in this case 
      public TextView name; 
      public TextView address; 
      public View layout; 
      public RadioButton status; 

      public ViewHolder(View v) { 
       super(v); 
       layout = v; 
       name = (TextView) v.findViewById(R.id.name); 
       address = (TextView) v.findViewById(R.id.address); 
       status=(RadioButton) v.findViewById(R.id.status); 
       v.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // mItemSelected = getAdapterPosition(); 
         try{ 
          PrintLine("The adapter position "+getAdapterPosition()); 
          Boolean current_value=values.get(getAdapterPosition()).getSelction(); 
          values.get(getAdapterPosition()).setSelction(!current_value); 
          notifyDataSetChanged(); 
         } 
         catch(Exception ex){ 
         PrintLine("Exception of type"+ex.getMessage()); 
         } 
        } 
       }); 

      } 
     } 

     //end of the view holder 


} 
//end of that section 
} 
関連する問題