所花时间:五天
代码行:2000
博客量:六篇
了解到知识点 :
Android Studio这个编程软件,十分好用,同时有一些相关便利代码:
1 package com.example.a13313.helloworld.recyclerview; 2 3 import android.content.Context; 4 import android.support.annotation.NonNull; 5 import android.support.v7.widget.RecyclerView; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView; 10 11 import com.example.a13313.helloworld.R; 12 13 /** 14 * Created by 13313 on 2019/4/21. 15 */ 16 public class GridAdapter extends RecyclerView.Adapter<GridAdapter.LinearViewHolder> { 17 private Context mContext; 18 private OnItemClickListener mlistener; //分配空间 19 //private List<String> list; 20 public GridAdapter(Context context, OnItemClickListener listener){ 21 this.mContext=context; //内容 22 this.mlistener=listener; //传输位置 23 } 24 @NonNull 25 @Override 26 public GridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 27 return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_grid_recyclerview_item,viewGroup,false)); 28 } 29 30 @Override 31 public void onBindViewHolder(@NonNull GridAdapter.LinearViewHolder viewHolder, final int i) { 32 viewHolder.textView.setText("Hello"); //内容 33 viewHolder.itemView.setOnClickListener(new View.OnClickListener() { //监听器按键 34 @Override 35 public void onClick(View v) { 36 mlistener.onClick(i); //监听器 37 } 38 }); 39 } 40 41 @Override 42 public int getItemCount() { 43 return 80; 44 } 45 class LinearViewHolder extends RecyclerView.ViewHolder{ 46 public TextView textView; 47 public LinearViewHolder(@NonNull View itemView) { 48 super(itemView); 49 textView=itemView.findViewById(R.id.tv_title); 50 } 51 } 52 public interface OnItemClickListener{ //字节型监听器 53 void onClick(int pos); 54 55 } 56 }
package com.example.a13313.helloworld.recyclerview; import android.graphics.Rect; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import com.example.a13313.helloworld.R; public class HoRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvHor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ho_recycler_view); mRvHor=(RecyclerView) findViewById(R.id.rv_hor); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(HoRecyclerViewActivity.this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRvHor.setLayoutManager(linearLayoutManager); mRvHor.addItemDecoration(new MyDecoration()); mRvHor.setAdapter(new HorAdapter(HoRecyclerViewActivity.this, new HorAdapter.OnItemClickListener() { @Override public void onClick(int pos) { Toast.makeText(HoRecyclerViewActivity.this,"click"+pos,Toast.LENGTH_LONG).show(); //监听器 } })); } class MyDecoration extends RecyclerView.ItemDecoration { public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect,view,parent,state); outRect.set(0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight),0); } } }