0

Basit ListView ürününün OnItemClick ürününe nasıl ulaşacağını ve gösterileceğini biliyorum, ancak RecyclerView için bir fikir yok.OnItemErişim geri dönüştürünGörüntüleyin, veri alın ve etkinliği başlatın ve verileri görüntüleyin

Sadece belirli bir öğe adını ve görüntüsünü almak istiyorum.

Ne ben yapmış olduğu:

1) MainActivity:

public class MainActivity extends AppCompatActivity { 

    private static RecyclerView.Adapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    private static RecyclerView recyclerView; 
    private static ArrayList<DataModel> data; 
    static View.OnClickListener myOnClickListener; 
    private static ArrayList<Integer> removedItems; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     myOnClickListener = new MyOnClickListener(this); 


     recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true); 

     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 

     data = new ArrayList<DataModel>(); 
     for (int i = 0; i < MyData.nameArray.length; i++) { 
      data.add(new DataModel(
        MyData.nameArray[i], 
        MyData.versionArray[i], 
        MyData.id_[i], 
        MyData.drawableArray[i] 
      )); 
     } 

     removedItems = new ArrayList<Integer>(); 

     adapter = new CustomAdapter(data); 
     recyclerView.setAdapter(adapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 
     if (item.getItemId() == R.id.add_item) { 
      //check if any items to add 
      if (removedItems.size() != 0) { 
       addRemovedItemToList(); 
      } else { 
       Toast.makeText(this, "Nothing to add", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     return true; 
    } 

    private static class MyOnClickListener implements View.OnClickListener { 

     private final Context context; 

     private MyOnClickListener(Context context) { 
      this.context = context; 
     } 

     @Override 
     public void onClick(View v) { 
      removeItem(v); 
      onItemClick(v); 
     } 

     private void onItemClick(View v) { 
      int selectedItemPosition = recyclerView.getChildPosition(v); 
      RecyclerView.ViewHolder viewHolder 
        = recyclerView.findViewHolderForPosition(selectedItemPosition); 
      TextView textViewName 
        = (TextView) viewHolder.itemView.findViewById(R.id.textViewName); 
      ImageView image 
        = (ImageView) viewHolder.itemView.findViewById(R.id.imageView); 
      String selectedName = (String) textViewName.getText(); 

      int selectedItemId = -1; 
      for (int i = 0; i < MyData.nameArray.length; i++) { 
       if (selectedName.equals(MyData.nameArray[i])) { 
        selectedItemId = MyData.id_[i]; 
       } 
      } 

     } 

     private void removeItem(View v) { 
      int selectedItemPosition = recyclerView.getChildPosition(v); 
      RecyclerView.ViewHolder viewHolder 
        = recyclerView.findViewHolderForPosition(selectedItemPosition); 
      TextView textViewName 
        = (TextView) viewHolder.itemView.findViewById(R.id.textViewName); 
      String selectedName = (String) textViewName.getText(); 
      int selectedItemId = -1; 
      for (int i = 0; i < MyData.nameArray.length; i++) { 
       if (selectedName.equals(MyData.nameArray[i])) { 
        selectedItemId = MyData.id_[i]; 
       } 
      } 
      removedItems.add(selectedItemId); 
      data.remove(selectedItemPosition); 
      adapter.notifyItemRemoved(selectedItemPosition); 
     } 
    } 

    private void addRemovedItemToList() { 
     int addItemAtListPosition = 3; 
     data.add(addItemAtListPosition, new DataModel(
       MyData.nameArray[removedItems.get(0)], 
       MyData.versionArray[removedItems.get(0)], 
       MyData.id_[removedItems.get(0)], 
       MyData.drawableArray[removedItems.get(0)] 
     )); 
     adapter.notifyItemInserted(addItemAtListPosition); 
     removedItems.remove(0); 
    } 




} 

2) DataModel:

public class DataModel { 


    String name; 
    String version; 
    int id_; 
    int image; 

    public DataModel(String name, String version, int id_, int image) { 
     this.name = name; 
     this.version = version; 
     this.id_ = id_; 
     this.image=image; 
    } 


    public String getName() { 
     return name; 
    } 


    public String getVersion() { 
     return version; 
    } 

    public int getImage() { 
     return image; 
    } 

    public int getId() { 
     return id_; 
    } 
} 

3) Adaptör:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> { 

    private ArrayList<DataModel> dataSet; 

    public static class MyViewHolder extends RecyclerView.ViewHolder { 

     TextView textViewName; 
     TextView textViewVersion; 
     ImageView imageViewIcon; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      this.textViewName = (TextView) itemView.findViewById(R.id.textViewName); 
      this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion); 
      this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView); 
     } 
    } 

    public CustomAdapter(ArrayList<DataModel> data) { 
     this.dataSet = data; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, 
              int viewType) { 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.cards_layout, parent, false); 

     view.setOnClickListener(MainActivity.myOnClickListener); 

     MyViewHolder myViewHolder = new MyViewHolder(view); 
     return myViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) { 

     TextView textViewName = holder.textViewName; 
     TextView textViewVersion = holder.textViewVersion; 
     ImageView imageView = holder.imageViewIcon; 

     textViewName.setText(dataSet.get(listPosition).getName()); 
     textViewVersion.setText(dataSet.get(listPosition).getVersion()); 
     imageView.setImageResource(dataSet.get(listPosition).getImage()); 


    } 

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

cevap

1

kullanabilirsiniz benim BaseViewHollder: En Adapter yılında

public abstract class BaseViewHolder extends RecyclerView.ViewHolder { 


public BaseViewHolder(View itemView) { 
    super(itemView); 
} 

public BaseViewHolder(View itemView, final ViewHolderClickListener listener) { 
    super(itemView); 
    if (listener != null) { 
     itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       listener.onViewHolderClick(view, getAdapterPosition()); 
      } 
     }); 
    } 
} 


public interface ViewHolderClickListener { 
    void onViewHolderClick(View view, int position); 
} 
} 

, sen extends için ViewHolder den BaseViewHolder var. Sonra şunları yapabilirsiniz:

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(yourLayoutId, parent, false); 
    // your viewHolderClickListener, you can set it in constructor 
    ViewHolder vh = new ViewHolder(v, viewHolderClickListener); 
    return vh; 
} 
0

MyViewHolder sınıfınızda bir ürün için birViewView öğesi verebilirsiniz. Hala bir kod gerekiyorsa, bana bildirin.