RecyclerView的展示及点击跳转

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gylgww/article/details/83661441

一:根据相关接口生成bean类

二:布局xml

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

三:使用butterknife初始化id

@BindView(R.id.recycle_view)
RecyclerView recycleView;

四:主页面代码

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

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

        Call<ShopBean> gouwu = HttpUtils.getData().api.gouwu();
        gouwu.enqueue(new Callback<ShopBean>() {
            @Override
            public void onResponse(Call<ShopBean> call, Response<ShopBean> response) {
                ShopBean shopBean = response.body();
                List<ShopBean.DataBean> data = shopBean.getData();
                //展示
                recycleView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));

                //设置适配器
                MyAdapter adapter = new MyAdapter(MainActivity.this,data);
                recycleView.setAdapter(adapter);

            }

            @Override
            public void onFailure(Call<ShopBean> call, Throwable t) {

            }
        });
    }
}

五:适配器代码

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    
    private Context context;
    private List<ShopBean.DataBean> list;
    //定义点击事件
    private OnItemClickListener onItemClickListener;

    public MyAdapter(Context context, List<ShopBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        //加载图片和文字
        Uri uri = Uri.parse(list.get(position).getIcon().split("\\|")[0]);
        holder.simple_view.setImageURI(uri);

        holder.text_view.setText(list.get(position).getName());
        
        //点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,Main2Activity.class);
                context.startActivity(intent);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView text_view;
        private final SimpleDraweeView simple_view;

        public ViewHolder(View itemView) {
            super(itemView);
            text_view = itemView.findViewById(R.id.text_view);
            simple_view = itemView.findViewById(R.id.simple_view);
        }
    }

    //定义点击接口
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    //点击方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
}
六:item_layout布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/simple_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view"
        android:text="xxxxx"
        android:layout_margin="10dp"/>

</LinearLayout>

七:使用Fresco图片控件加载图片的话,需要创建App类
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //进行磁盘缓存
DiskCacheConfig.Builder builder = DiskCacheConfig.newBuilder(this);
builder.setBaseDirectoryPath(getCacheDir());
DiskCacheConfig diskCacheConfig = builder.build();

ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
        .setMainDiskCacheConfig(diskCacheConfig)
        .build();

        Fresco.initialize(this);
    }
}

在清单文件中进行注册

 

 

 

展开阅读全文

没有更多推荐了,返回首页