Android高级控件——GridView ScrollView ViewPager (上)
GridView 网格视图,网格视图组件,九宫图显示数据表格(一种控件)
ScrollView滚动视图
是一个单一容器,只能包含一个组件。
ViewPager左右滑动
SlideMenu侧边栏
PullToRefreshListView下拉刷新
ListView新闻 原声列表视图
xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#fc03" android:state_pressed="true"/> <item android:color="#09090a"/> selector>
xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:color="#f50202" android:width="1dp"/> <corners android:radius="9dp"/> shape>
GridViewActivity程序代码
package com.example.jreduch7292;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GridViewActivity extends AppCompatActivity {
private GridView gv;
private ImageView big;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
gv=(GridView)findViewById(R.id.gv);
big=(ImageView)findViewById(R.id.big);
final List list=new ArrayList();
Map map=new HashMap();
map.put("img",R.mipmap.a);
map.put("name","字母A");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.b);
map.put("name","字母B");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.c);
map.put("name","字母C");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.d);
map.put("name","字母D");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.e);
map.put("name","字母E");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.f);
map.put("name","字母F");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.g);
map.put("name","字母G");
list.add(map);
map=new HashMap();
map.put("img",R.mipmap.h);
map.put("name","字母H");
list.add(map);
/*
1.simpleAdapter使用的数据源必须继承Map接口
2.from参数的意思是指向数据源Map中的键
3.to 参数的意思是为布局中的控件Id赋值
*/
SimpleAdapter sa=new SimpleAdapter(this,list,
R.layout.grid_layout,
new String[]{"img","name"},
new int[]{R.id.iv,R.id.tv});
gv.setAdapter(sa);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Map map=(Map)list.get(position);
Toast.makeText(getBaseContext(),""+map.get("name"),Toast.LENGTH_SHORT).show();
Intent intent=new Intent(getBaseContext(),BigActivity.class);
intent.putExtra("img",(int)map.get("img"));
startActivity(intent);
}
});
}
}
setContentView(R.layout.activity_grid_view);程序代码
/* 1.simpleAdapter使用的数据源必须继承Map接口 2.from参数的意思是指向数据源Map中的键 3.to 参数的意思是为布局中的控件Id赋值 */ SimpleAdapter sa=new SimpleAdapter(this,list, R.layout.grid_layout, new String[]{"img","name"}, new int[]{R.id.iv,R.id.tv}); gv.setAdapter(sa);程序代码
setContentView(R.layout.activity_big);程序代码 放大
package com.example.jreduch7292;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.BounceInterpolator;
import uk.co.senab.photoview.PhotoView;
public class BigActivity extends AppCompatActivity {
private PhotoView big;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big);
big=(PhotoView)findViewById(R.id.big);
Intent intent=getIntent();
int imgId=intent.getIntExtra("img",R.mipmap.ic_launcher);
big.setImageResource(imgId);
//属性动画
ObjectAnimator oja=ObjectAnimator.ofFloat(big,"rotation",0,360);
oja.setDuration(3000);
oja.setRepeatCount(0);
//设置插补器
oja.setInterpolator(new BounceInterpolator());
oja.start();
}
}
GridViewTextActivityr程序代码
package com.example.jreduch7292;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GridViewTextActivityr extends AppCompatActivity {
private GridView gv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view_text_activityr);
getSupportActionBar().hide();
//隐藏ActionBar
gv1=(GridView)findViewById(R.id.gv1);
final List list=new ArrayList();
Map map=new HashMap();
map.put("name","推荐");
list.add(map);
map=new HashMap();
map.put("name","热点");
list.add(map);
map=new HashMap();;
map.put("name","体育");
list.add(map);
map=new HashMap();
map.put("name","烟台");
list.add(map);
map=new HashMap();
map.put("name","视频");
list.add(map);
map=new HashMap();
map.put("name","汽车");
list.add(map);
map=new HashMap();
map.put("name","订阅");
list.add(map);
map=new HashMap();
map.put("name","社会");
list.add(map);
map=new HashMap();;
map.put("name","财经");
list.add(map);
map=new HashMap();
map.put("name","科技");
list.add(map);
map=new HashMap();
map.put("name","健康");
list.add(map);
map=new HashMap();
map.put("name","军事");
list.add(map);
map=new HashMap();
map.put("name","彩票");
list.add(map);
map=new HashMap();
map.put("name","国际");
list.add(map);
map=new HashMap();
map.put("name","段子");
list.add(map);
map=new HashMap();
map.put("name","科技");
list.add(map);
map=new HashMap();
map.put("name","健康");
list.add(map);
map=new HashMap();
map.put("name","军事");
list.add(map);
map=new HashMap();
map.put("name","彩票");
list.add(map);
/*
1.simpleAdapter使用的数据源必须继承Map接口
2.from参数的意思是指向数据源Map中的键
3.to 参数的意思是为布局中的控件Id赋值
*/
SimpleAdapter sa=new SimpleAdapter(this,list,
R.layout.grid2_layout,
new String[]{"name"},
new int[]{R.id.tv});
gv1.setAdapter(sa);
}
}
package com.example.jreduch7292;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.jreduch7292.adapter.MyViewPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ScrollViewActivity extends AppCompatActivity {
private RadioGroup rg;
private ViewPager vp;
private List imgList;
private MyViewPagerAdapter mp;
private RadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8,rb9,rb10;
private HorizontalScrollView hs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
rb1=(RadioButton) findViewById(R.id.rb1);
rb2=(RadioButton) findViewById(R.id.rb2);
rb3=(RadioButton) findViewById(R.id.rb3);
rb4=(RadioButton) findViewById(R.id.rb4);
rb5=(RadioButton) findViewById(R.id.rb5);
rb6=(RadioButton) findViewById(R.id.rb6);
rb7=(RadioButton) findViewById(R.id.rb7);
rb8=(RadioButton) findViewById(R.id.rb8);
rb9=(RadioButton) findViewById(R.id.rb9);
rb10=(RadioButton) findViewById(R.id.rb10);
hs=(HorizontalScrollView)findViewById(R.id.hs);
rg=(RadioGroup)findViewById(R.id.rg);
vp=(ViewPager)findViewById(R.id.vp);
vpInit();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==R.id.rb1){
vp.setCurrentItem(0);
Toast.makeText(getBaseContext(),"推荐1",Toast.LENGTH_SHORT).show();
} else if (checkedId==R.id.rb2){
vp.setCurrentItem(1);
Toast.makeText(getBaseContext(),"推荐2",Toast.LENGTH_SHORT).show();
}
else if (checkedId==R.id.rb3){
vp.setCurrentItem(2);
Toast.makeText(getBaseContext(),"推荐3",Toast.LENGTH_SHORT).show();
}else{
switch(checkedId){
case R.id.rb4:
vp.setCurrentItem(3);
Toast.makeText(getBaseContext(),"推荐4",Toast.LENGTH_SHORT).show();
break;
case R.id.rb5:
vp.setCurrentItem(4);
Toast.makeText(getBaseContext(),"推荐5",Toast.LENGTH_SHORT).show();
break;
case R.id.rb6:
Toast.makeText(getBaseContext(),"推荐6",Toast.LENGTH_SHORT).show();
break;
case R.id.rb7:
Toast.makeText(getBaseContext(),"推荐7",Toast.LENGTH_SHORT).show();
break;
}
}
}
});
}
public void vpInit(){
imgList=new ArrayList<>();
// HashMap map=new HashMap();
// //Map map=new HashMap();
// map.put("name",R.mipmap.eeo);
// imgList.add(map);
// SimpleAdapter sa=new SimpleAdapter();
ImageView iv=new ImageView(this);
iv.setImageResource(R.mipmap.zyfzyf);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eet);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eer);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eep);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eeq);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eew);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eex);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.eez);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.efa);
imgList.add(iv);
iv=new ImageView(this);
iv.setImageResource(R.mipmap.efb);
imgList.add(iv);
mp=new MyViewPagerAdapter(imgList);
vp.setAdapter(mp);
vp.setCurrentItem(0);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
RadioButton rb=(RadioButton) rg.getChildAt(position);
rb.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
SpinnerActivity 程序代码
package com.example.jreduch7292;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class SpinnerActivity extends AppCompatActivity {
private Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
sp=(Spinner)findViewById(R.id.sp);
List cityList=new ArrayList();
cityList.add("北京");
cityList.add("上海");
cityList.add("广州");
//最简单的适配器只能显示一行文字
ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_spinner_item,
android.R.id.text1,cityList);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),sp.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
}
}
string> <string-array name="city"> <item>烟台item> <item>济南item> <item>青岛item> string-array>
作者:冲天之峰 20160801