常用属性Adapter接口
package com.example.zzm.helloworld.listview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;import com.example.zzm.helloworld.R;public class ListViewActivity extends Activity { private ListView mLv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); mLv1=(ListView)findViewById(R.id.lv_1); mLv1.setAdapter(new MyListAdapter(ListViewActivity.this)); //点击事件 mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Toast.makeText(ListViewActivity.this,"点击pos:"+position, Toast.LENGTH_SHORT).show(); } }); mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) { Toast.makeText(ListViewActivity.this,"长按 pos:"+position, Toast.LENGTH_SHORT).show(); return true; } }); }}`````xml<?xml version="1.0" encoding="utf-8"?>
package com.example.zzm.helloworld.listview;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.baseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.zzm.helloworld.R;import org.w3c.dom.Text;public class MyListAdapter extends baseAdapter{ private Context mContext; private LayoutInflater mLayoutInflater; public MyListAdapter(Context context){ this.mContext=context; mLayoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return 10; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } public class ViewHolder{ public ImageView imageView; public TextView tvTitle,tvTime,tvContent; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null){ convertView=mLayoutInflater.inflate(R.layout.layout_list_view,null); holder=new ViewHolder(); holder.imageView=(ImageView)convertView.findViewById(R.id.iv); holder.tvTitle=(TextView)convertView.findViewById(R.id.tv_title); holder.tvTime=(TextView)convertView.findViewById(R.id.tv_time); holder.tvContent=(TextView)convertView.findViewById(R.id.tv_content); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } //给控件赋值 holder.tvTitle.setText("这是标题"); holder.tvTime.setText("2088-08-08"); holder.tvContent.setText("这是内容"); Glide.with(mContext).load("").into(holder.imageView); return convertView; return null; }}
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
垂直滚动:ScrollView:
子元素只能有一个
<?xml version="1.0" encoding="utf-8"?>
水平滚动:HorizontalScrollView
子布局也只能有一个
<?xml version="1.0" encoding="utf-8"?>
(非常重要的控件)
RecyclerView能够灵活实现大数据的展示,视图的复用管理比ListView更好,能够显示列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能。
但是使用起来会稍微麻烦一点,并且没有类似ListView的onItemClickListener监听事件,需要开发者自己实现。
WebView加载网页
加载URL(网络或者本地assets文件夹下的html文件)
加载网络URL
webview.loadUrl(“xxx”);
加载assets下的html’文件
webview.loadUrl(“xxx”)
加载html代码
webview.loadData();
webview.loadDataWithbaseURL();
Native和Javascript相互调用
//加载本地URL
mWvMain.loadUrl(“file:///android_asset/hello.html”);
//加载网络URL
mWvMain.getSettings().setJavascriptEnabled(true);
mWvMain.loadUrl(“https://m.baidu.com”);