2017-10-11 33 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

先生原諒我,因爲我處於學習的早期階段而寫錯了任何東西。我使用持有者來獲取我的ui小部件,例如我的ViewAdapter中有一個名爲狀態的單選按鈕,所以我想將收音機設置爲作爲檢查點擊時,這就是爲什麼我使用holder.status.setSelected();然後爲其提供反轉布爾值,僅用於切換功能以顯示被選中或未選中。先生!如果使用holder.getAdapterPosition()它會給我適配器的id然後如何改變它的背景。 – Mashhood

+0

持有者給我們的兩個位置是onBindViewHolderMethod中的int位置,它是可見屏幕上的位置。另一個是holder.getAdapterPosition(),它是該項目的實際位置。所以我建議你使用第二個。 –

回答

2

循環機視圖回收OnBindViewHolder.So中的視圖,當項目被點擊時,它會反映到其他一些位置。解決這個問題。

創建一個全局變量來存儲點擊的位置。

private mItemSelected=-1; 

然後在視圖裏面添加clickListener並且點擊存儲被點擊項的位置。

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中獲得的位置,但我的視圖顯示在選擇第一個項目時有7個項目具有[0]索引,這是我的AdapterView – 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 
} 
相關問題