android卡片布局CardView

android5.0版本推出了新的控件其中CardView则是卡片式布局

效果图:

         android卡片布局CardView_第1张图片

josn数据:

{
	"activity": [{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "android "


		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "java "
		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "php "
		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "UI"
		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "AI"
		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "AI"
		},
		{
			"img": "http://pic27.photophoto.cn/20130522/0010023309252566_b.jpg",
			"tiele": "C++"
		}
	],
	"description": "查询成功",
	"flag": "success"
}
1、添加依赖
    compile files('libs/okhttp-3.0.1.jar')  
    compile files('libs/okio-1.6.0.jar')     
    compile files('libs/picasso-2.5.2.jar')  
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'

2、主布局

    
        

如果是和Toolbar一起使用,添加下面这段,解决重叠问题

 app:layout_behavior="@string/appbar_scrolling_view_behavior"

3、创建RecyclerView的item布局




    
        
        
    


其他属性:

app:cardMaxElevation:阴影最大高度
app:cardBackgroundColor:卡片的背景
app:cardCornerRadius:卡片的圆角大小
app:contentPadding:边距

4、主布局Activity代码

public class materialActivity extends AppCompatActivity {

    public RecyclerView mater_recy;
    public ArrayList> mater_list=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material);
        init();
     
  
    //布局解析
    public void init(){
        mater_recy= (RecyclerView) findViewById(R.id.meaterial_listview);
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okhttp=new OkHttpClient();
                Request request=new Request.Builder().url("http://192.168.1.92/material.json").build();
                try {
                    Response response=okhttp.newCall(request).execute();
                    String date=response.body().string();

                    material_jx(date);
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();

    }


    public void material_jx(String date){
        if(date!=null){
            try {
                JSONObject object=new JSONObject(date);
                String resultCode = object.getString("flag");
                if (resultCode.equals("success")){
                    JSONArray jsonarray=object.getJSONArray("activity");
                    for (int i=0;i map=new HashMap<>();
                        String img=object.getString("img");
                        String title=object.getString("tiele");
                        map.put("img",img);
                        map.put("title",title);
                        mater_list.add(map);

                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Message msg=new Message();
        msg.what=1;
        handler.sendMessage(msg);



    }
    public Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    material_Adapter adapter=new material_Adapter(materialActivity.this,mater_list);
                    GridLayoutManager layoutManager=new GridLayoutManager(materialActivity.this,1);//设置一行几列
                    mater_recy.setLayoutManager(layoutManager);
                    mater_recy.setAdapter(adapter);
                    break;
            }
        }
    };
   
}

5、创建适配器

public class material_Adapter extends RecyclerView.Adapter {
    public Context context;
    public ArrayList> list;
    public LayoutInflater inflaterl;
    public material_Adapter(Context context, ArrayList> list){
        this.context=context;
        this.list=list;
        this.inflaterl=LayoutInflater.from(context);

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflaterl.inflate(R.layout.material_item,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }


    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv.setText(list.get(position).get("title").toString());
        if (TextUtils.isEmpty(list.get(position).get("img").toString())){
            Picasso
                    .with(context)
                    .cancelRequest(holder.img);
            holder.img.setImageDrawable(context.getResources().getDrawable(R.drawable.no));//当图片为空时显示
        }else {
            //图片加载
            Picasso
                    .with(context)
                    .load(list.get(position).get("img").toString())
                    .placeholder(R.drawable.jiazai)//图片加载中显示

                    .into(holder.img);
        }
    }


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


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.material_item_img);
            tv= (TextView) itemView.findViewById(R.id.material_item_tv);
        }
    }
}
最后不要忘了添加网络请求权限
 
  






你可能感兴趣的:(Android)