RecycleView

Add Dependencies

Add compile com.android.support:recycleview-v7:[version_number] in build,gradle(Module:app)

In XML

  • RecycleView

  • <android.support.v7.widget.RecyclerView
    
          android:id="@+id/rv_numbers"
    
          android:layout_width="match_parent"
    
          android:layout_height="match_parent"/>

     

  • Adapter

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
      xmlns:tools="http://schemas.android.com/tools"
    
      android:layout_width="match_parent"
    
      android:layout_height="wrap_content"
    
      android:padding="16dp">
    <TextView
    android:id="@+id/tv_item_number"
    style="@style/TextAppearance.AppCompat.Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|start"
    android:fontFamily="monospace"
    android:textSize="42sp"
    tools:text="#42" />
    </FrameLayout>

     

In JAVA

  • ViewHolder (In Adapter class)

    // TODO (12) Create a class called NumberViewHolder that extends RecyclerView.ViewHolder
    public class NumberViewHolder extends RecyclerView.ViewHolder{
    
    // TODO (13) Within NumberViewHolder, create a TextView variable called listItemNumberView
    TextView listItemNumberView;
    
    // TODO (14) Create a constructor for NumberViewHolder that accepts a View called itemView as a parameter
    // TODO (15) Within the constructor, call super(itemView) and then find listItemNumberView by ID
    public NumberViewHolder(View itemView){
    super(itemView);
    listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
    }
    
    // TODO (16) Within the NumberViewHolder class, create a void method called bind that accepts an int parameter called listIndex
    // TODO (17) Within bind, set the text of listItemNumberView to the listIndex
    // TODO (18) Be careful to get the String representation of listIndex, as using setText with an int does something different
    public void bind(int listIndex){
    listItemNumberView.setText(String.valueOf(listIndex));
    }

     

  • Adapter

    // TODO (4) From GreenAdapter, extend RecyclerView.Adapter<NumberViewHolder>
    
    public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder>{
    
      // TODO (1) Add a private int variable called mNumberItems
    
      private int mNumberItems;
    
      // TODO (2) Create a constructor for GreenAdapter that accepts an int as a parameter for numberOfItems
    
      // TODO (3) Store the numberOfItems parameter in mNumberItems
    
      public GreenAdapter(int numberOfItems){
    
          mNumberItems = numberOfItems;
    
      }
    
      // TODO (5) Override the onCreateViewHolder method
    
      // TODO (6) Create and return a new NumberViewHolder within this method
    @Override
    public NumberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
    Context context = parent.getContext();
    int layouIdForListItem = R.layout.number_list_item;
    LayoutInflater inflater = LayoutInflater.from(context);
    boolean shouldAttachTOParentImmediately = false;
    
    View view = inflater.inflate(layouIdForListItem, parent, shouldAttachTOParentImmediately);
    NumberViewHolder viewHolder = new NumberViewHolder(view);
    
    return viewHolder;
    }
    
    // TODO (7) Override onBindViewHolder
    // TODO (8) Within onBindViewHolder, call holder.bind and pass in the position
    
    @Override
    public void onBindViewHolder(NumberViewHolder holder, int position) {
    holder.bind(position);
    }
    
    // TODO (9) Override getItemCount and return the number of items to display
    @Override
    public int getItemCount() {
    Log.v("TAG", "getItemCount(), return mNumberItems = " + mNumberItems);
    return mNumberItems;
    }

     

  • LayoutManager

    public class MainActivity extends AppCompatActivity {
    
      // TODO (1) Create a private static final int called NUM_LIST_ITEMS and set it equal to 100
    
      private static final int NUM_LIST_ITEMS = 100;
    
      // TODO (2) Create a GreenAdapter variable called mAdapter
    
      GreenAdapter mAdapter;
    // TODO (3) Create a RecyclerView variable called mNumbersList
    RecyclerView mNumbersList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // TODO (4) Use findViewById to store a reference to the RecyclerView in mNumbersList
    mNumbersList = (RecyclerView)findViewById(R.id.rv_numbers);
    // TODO (5) Create a LinearLayoutManager variable called layoutManager
    // TODO (6) Use setLayoutManager on mNumbersList with the LinearLayoutManager we created above
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    mNumbersList.setLayoutManager(layoutManager);
    // TODO (7) Use setHasFixedSize(true) to designate that the contents of the RecyclerView won't change an item's size
    mNumbersList.setHasFixedSize(true);
    
    // TODO (8) Store a new GreenAdapter in mAdapter and pass it NUM_LIST_ITEMS
    mAdapter = new GreenAdapter(NUM_LIST_ITEMS);
    // TODO (9) Set the GreenAdapter you created on mNumbersList
    mNumbersList.setAdapter(mAdapter);
    }
    }

     

  • ClickListener

    • GreenAdapter class

      // TODO (3) Create a final private ListItemClickListener called mOnClickListener
      private static final String TAG = GreenAdapter.class.getSimpleName();
      
      // TODO (1) Add an interface called ListItemClickListener
      // TODO (2) Within that interface, define a void method called onListItemClick that takes an int as a parameter
      public interface ListItemClickListener{
      void onListItemClick(int clickItemIndex);
      }
      
      // TODO (4) Add a ListItemClickListener as a parameter to the constructor and store it in mOnClickListener
      public GreenAdapter(int numberOfItems, ListItemClickListener listener) {
      mNumberItems = numberOfItems;
      viewHolderCount = 0;
      mOnClickListener = listener;
      }
      
      public NumberViewHolder(View itemView) {
      super(itemView);
      
      listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
      viewHolderIndex = (TextView) itemView.findViewById(R.id.tv_view_holder_instance);
      
      // TODO (7) Call setOnClickListener on the View passed into the constructor (use 'this' as the OnClickListener)
      itemView.setOnClickListener(this);
      
      }
      
      @Override
      public void onClick(View v) {
      int clickedPosition = getAdapterPosition();
      mOnClickListener.onListItemClick(clickedPosition);
      }
      
      // TODO (6) Override onClick, passing the clicked item's position (getAdapterPosition()) to mOnClickListener via its onListItemClick method
      
      @Override
      public void onClick(View v) {
      int clickedPosition = getAdapterPosition();
      mOnClickListener.onListItemClick(clickedPosition);
      }

       

    • MainActivity class

      // TODO (8) Implement GreenAdapter.ListItemClickListener from the MainActivity
      
      public class MainActivity extends AppCompatActivity implements GreenAdapter.ListItemClickListener{
      private static final int NUM_LIST_ITEMS = 500;
      
      private GreenAdapter mAdapter;
      private RecyclerView mNumbersList;
      
      // TODO (9) Create a Toast variable called mToast to store the current Toast
      private Toast mToast;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // TODO (13) Pass in this as the ListItemClickListener to the GreenAdapter constructor
      mAdapter = new GreenAdapter(NUM_LIST_ITEMS, this);
      mNumbersList.setAdapter(mAdapter);
      }
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
      }
      
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      
      int itemId = item.getItemId();
      
      switch (itemId) {
      case R.id.action_refresh:
      // TODO (14) Pass in this as the ListItemClickListener to the GreenAdapter constructor
      mAdapter = new GreenAdapter(NUM_LIST_ITEMS, this);
      mNumbersList.setAdapter(mAdapter);
      return true;
      }
      return super.onOptionsItemSelected(item);
      }
      
      // TODO (10) Override ListItemClickListener's onListItemClick method
      // TODO (11) In the beginning of the method, cancel the Toast if it isn't null
      // TODO (12) Show a Toast when an item is clicked, displaying that item number that was clicked
      @Override
      public void onListItemClick(int clickItemIndex) {
      if(mToast != null){
      mToast.cancel();
      }
      String toast_msg = "Item #" + clickItemIndex + " clicked!";
      mToast = Toast.makeText(this, toast_msg, Toast.LENGTH_SHORT);
      mToast.show();
      }
      }

       

dp,sp, px and etc

px = dp*(dpi/160)
sp: for text

    分享到:
分类: Android

发表评论

电子邮件地址不会被公开。 必填项已用*标注

验证码 *