Is there any simple implementation or tutorial to implement RecyclerView in android?

前端 未结 2 1045
后悔当初
后悔当初 2020-12-12 00:39

I am trying to port my existing project to material design but am unable to understand the implementation of RecyclerView. My old code uses a ListView but I want GMail app l

相关标签:
2条回答
  • 2020-12-12 01:21

    RecyclerView Adapter Example:

    1. Adapter issue can be solved using below code:

      public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>         
      {
      
      private final Context mContext;
      private List<String> mData1, mData2;
      
      public MyAdapter(Context mContext, String[] data1, String[] data2) {
      this.mContext = mContext;
      if (data1 != null)
          mData1 = new ArrayList<String>(Arrays.asList(data1));
      else
          mData1 = new ArrayList<String>();
      
      if (data2 != null)
          mData2 = new ArrayList<String>(Arrays.asList(data2));
      else
          mData2 = new ArrayList<String>();
      }
      
      public void add(String s, int position) {
      position = position == -1 ? getItemCount() : position;
      mData1.add(position, s);
      
      notifyItemInserted(position);
      }
      
      public void remove(int position) {
      if (position < getItemCount()) {
          mData1.remove(position);
          notifyItemRemoved(position);
      }
      }
      
      @Override
      public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
      View itemView = inflater.inflate(R.layout.list_item, viewGroup, false);
      
      return new MyViewHolder(itemView);
      }
      
      @Override
      public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {
          myViewHolder.tv1.setText(mData1.get(position));
      myViewHolder.tv2.setText(mData2.get(position));
      myViewHolder.itemView.setOnClickListener(new OnClickListener() {
      
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              clickListener.onClick(v, position);
          }
      });
      }
      
      @Override
      public int getItemCount() {
      return mData1.size();
      }
      
      ClickListener clickListener;
      
      public void setClickListener(ClickListener clickListener) {
      this.clickListener = clickListener;
      }
      
      public interface ClickListener {
      public void onClick(View v, int pos);
      
      }
      
      public static class MyViewHolder extends RecyclerView.ViewHolder {
      protected TextView tv1;
      protected TextView tv2;
      
      public MyViewHolder(View itemView) {
          super(itemView);
          tv1 = (TextView) itemView.findViewById(R.id.txt1);
          tv2 = (TextView) itemView.findViewById(R.id.txt2);
      
      }
      
      }
      }
      

    Usage is as below:

    MyAdapter adapter=....;
    adapter.setClickListener(new ClickListener() {
    
            @Override
            public void onClick(View v, int pos) {
                // do whatever you want
            }
        });
    
    1. There is not any Simple way to do that. But i would suggest you one workaround for this. Add below view as a divider at the bottom inside your list_item.xml.

       <View
      android:layout_width="wrap_content"
      android:layout_height="0.5dip" 
      android:background="@color/light_gray"/>
      
    2. Item click listener is also resolved in point no. 1 above.

    I hope this would help you.

    0 讨论(0)
  • 2020-12-12 01:28

    Here is example for Simple Implementation of RecyclerView using a Simple Library

    Add this line in build.gradle

    implementation 'com.hereshem.lib:awesomelib:2.0.1'
    

    Create RecyclerView Layout in Activity with

    <com.hereshem.lib.recycler.MyRecyclerView
            android:id="@+id/recycler"
            app:layoutManager="LinearLayoutManager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    

    Create a ViewHolder by passing the class it suppports

    public static class EVHolder extends MyViewHolder<Events> {
        TextView date, title, summary;
        public EVHolder(View v) {
            super(v);
            date = v.findViewById(R.id.date);
            title = v.findViewById(R.id.title);
            summary = v.findViewById(R.id.summary);
        }
        @Override
        public void bindView(Events c) {
            date.setText(c.date);
            title.setText(c.title);
            summary.setText(c.summary);
        }
    }
    

    Create Items List variable and adapters with very few lines

    List<Events> items = new ArrayList<>();
    MyRecyclerView recycler = findViewById(R.id.recycler);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, items, EVHolder.class, R.layout.row_event);
    recycler.setAdapter(adapter);
    

    ClickListener can be added with folllowing line

    recycler.setOnItemClickListener(new MyRecyclerView.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Toast.makeText(MainActivity.this, "Recycler Item Clicked " + position, Toast.LENGTH_SHORT).show();
        }
    });
    

    More example can be found here

    Hope this helps :)

    0 讨论(0)
提交回复
热议问题