常用包
http://square.github.io/
EventBus
Scroller 滚动
拖拽 # android.support.design.widget.CollapsingToolbarLayout 收缩顶部TAB(名片)
# android.support.design.widget.NavigationView 左边通栏弹出
# android.support.design.widget.TextInputLayout 带提示输入布局
# android.support.design.widget.TabLayout 顶部tab
# android.support.v4.widget.NestedScrollView 带缓动的滚动
# android.support.v7.widget.CardView 卡片视图 compile 'de.hdodenhof:circleimageview:1.3.0' 圆角图片
compile 'com.loopj.android:android-async-http:1.4.7' 实现类似JQ AJAX
compile 'com.nineoldandroids:library:2.4.0' 动画库 AccessibilityDelegateCompat 无障碍使用 ActionBar
//设置顶部背景
actionBar.setBackgroundDrawable(getDrawable(R.drawable.aa));
//添加自定义视图
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.nemu1);
//显示返回首页ICON
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeActionContentDescription("ccccccccccc");
actionBar.setHomeAsUpIndicator(R.drawable.c); actionBar.setIcon(R.drawable.c);
actionBar.setLogo(R.drawable.aa);
//显示ICON 或LOGO
actionBar.setDisplayShowHomeEnabled(true);
//为真现实LOGO 假显示 ICON
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setTitle("ccccccccc");
actionBar.setSubtitle("subcccccccccc");
//是否显示标题
actionBar.setDisplayShowTitleEnabled(true);
//设置按钮
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP); Toolbar 跟ActionBar 类似
XML:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary" />
JAVA:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.aa);
toolbar.setLogo(R.drawable.c); # ScrollView 滚动视图
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="10000dp"
android:height="10000dp"
/>
</HorizontalScrollView>
</ScrollView> # TabHost
1. 静态
主XML:
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="林炳东" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
TAB XML: xxx.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xxx">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
JAVA:
TabHost th=(TabHost)findViewById(R.id.tabhost);
th.setup();
LayoutInflater i= LayoutInflater.from(this);
i.inflate(R.layout.xxx, th.getTabContentView());
th.addTab(th.newTabSpec("tab1")
.setIndicator("标签1", null)
.setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab2")
.setIndicator("标签2", null)
.setContent(R.id.xxx));
动态内容:
TabHost th=(TabHost)findViewById(R.id.tabHost);
th.setup();
TabHost.TabSpec tabSpec = th.newTabSpec("tab1")
.setIndicator("标签1", null)
.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
TextView text = new TextView(tabactivity.this);
text.setText("text1");
return text;
}
});
th.addTab(tabSpec);
tabSpec = th.newTabSpec("tab2")
.setIndicator("标签2", null)
.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
TextView text = new TextView(tabactivity.this);
text.setText("text2");
return text;
}
});
th.addTab(tabSpec); # ViewStub 延时加载视图
<ViewStub
android:id="@+id/rload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/xxx"
/> ViewStub vs = (ViewStub) findViewById(R.id.rload);
vs.inflate(); # ImageSwitcher [类似 ViewSwitcher TextSwitcher]
XML:
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageSwitcher>
<Button
android:id="@+id/change"
android:text="change"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
JAVA:
private Integer[] mImageIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};
private int i=0;
private float offp=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab); final ImageSwitcher img = (ImageSwitcher) findViewById(R.id.imageSwitcher);
//显示VIEW
img.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView i = new ImageView(tabactivity.this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return i;
}
});
img.setImageDrawable(getDrawable(mImageIds[2]));
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//getRawX()和getRawY()获得的是相对屏幕的位置
//getX()和getY()获得的永远是相对view的触摸位置坐标
//返回 false 将不会触发其他事件
Log.i("MontionEvent",String.valueOf(event.getAction()));
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
offp = event.getX();
break;
case MotionEvent.ACTION_MOVE:
// img.setLeft(-(int)(offp - event.getX()));
break;
case MotionEvent.ACTION_UP:
if (offp - event.getX() > 10) {
i--;
img.setInAnimation(AnimationUtils
.loadAnimation(tabactivity.this,
R.anim.slide_in_right));
img.setOutAnimation(AnimationUtils
.loadAnimation(tabactivity.this,
R.anim.slide_out_left));
} else if (offp - event.getX() < 10) {
i++;
img.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_in_left));
img.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_out_right));
}else
return true;
if (i < 0) i = mImageIds.length;
img.setImageDrawable(getDrawable(mImageIds[i % mImageIds.length]));
break;
} return true;
}
});
Button but = (Button) findViewById(R.id.change);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
img.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_in_left));
img.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_out_right));
img.setImageDrawable(getDrawable(mImageIds[i % mImageIds.length]));
}
});
} # ViewFlipper 带自动播放的 ViewSwitcher
XML :
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/filp">
<!-- 第一个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a1" />
</LinearLayout>
<!-- 第二个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a2"
android:gravity="center" />
</LinearLayout>
<!-- 第三个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"> <ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a3"
android:gravity="center" />
</LinearLayout>
<!-- 第四个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a4"
android:gravity="center" />
</LinearLayout>
</ViewFlipper>
<Button
android:id="@+id/fc"
android:text="change"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
JAVA:
final ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.filp);
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_in_left));
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
android.R.anim.slide_out_right));
Button fc = (Button) findViewById(R.id.fc);
fc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(viewFlipper.isFlipping())
viewFlipper.stopFlipping();
else
viewFlipper.startFlipping();
}
});
viewFlipper.startFlipping(); #AndroidImageSlider 幻灯片 https://github.com/daimajia/AndroidImageSlider
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.slider:library:1.1.5@aar' # ViewPager 左右滚动页面
XML:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="@+id/viewpager">
<!-- PagerTabStrip 标题底部线 -->
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:gravity="center" />
</android.support.v4.view.ViewPager>
JAVA:
ViewPager pager = null;
PagerTabStrip tabStrip = null;
ArrayList<View> viewContainter = new ArrayList<View>();
ArrayList<String> titleContainer = new ArrayList<String>();
public String TAG = "tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
pager = (ViewPager) this.findViewById(R.id.viewpager);
tabStrip = (PagerTabStrip) this.findViewById(R.id.tabstrip);
//取消tab下面的长横线
//tabStrip.setDrawFullUnderline(false);
//设置当前tab页签的下划线颜色
tabStrip.setTabIndicatorColor(this.getResources().getColor(android.R.color.holo_blue_bright));
tabStrip.setTextSpacing(200); View view1 = LayoutInflater.from(this).inflate(R.layout.xxx, null);
View view2 = LayoutInflater.from(this).inflate(R.layout.xxx, null); //viewpager开始添加view
viewContainter.add(view1);
viewContainter.add(view2); //页签项
titleContainer.add("网易新闻");
titleContainer.add("网易体育"); pager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return viewContainter.size();
} @Override
public void destroyItem(ViewGroup container, int position,
Object object) {
((ViewPager) container).removeView(viewContainter.get(position));
} //每次滑动的时候生成的组件
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(viewContainter.get(position));
return viewContainter.get(position);
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} @Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
} @Override
public CharSequence getPageTitle(int position) {
return titleContainer.get(position);
}
});
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override
public void onPageSelected(int position) { } @Override
public void onPageScrollStateChanged(int state) { }
}); }
配合 Fragment 使用
XML:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="@+id/viewPager">
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:gravity="center" />
</android.support.v4.view.ViewPager>
JAVA:
List<Fragment> fragmentList = new ArrayList<Fragment>();
List<String> titleList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
ViewPager vp = (ViewPager)findViewById(R.id.viewPager);
fragmentList.add(new ViewPagerFragment1("页面1"));
fragmentList.add(new ViewPagerFragment1("页面2"));
fragmentList.add(new ViewPagerFragment1("页面3"));
titleList.add("title 1 ");
titleList.add("title 2 ");
titleList.add("title 3 ");
vp.setAdapter(new myPagerAdapter(getSupportFragmentManager(), fragmentList, titleList));
}
class myPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> titleList;
public myPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList){
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public Fragment getItem(int arg0) {
return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(arg0);
}
@Override
public CharSequence getPageTitle(int position) {
return (titleList.size() > position) ? titleList.get(position) : "";
}
@Override
public int getCount() {
return fragmentList == null ? 0 : fragmentList.size();
}
}
public class ViewPagerFragment1 extends Fragment {
public ViewPagerFragment1(String text){
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.xxx, container, false);
return v;
}
} # SwipeRefreshLayout 下拉刷新
ITEM XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="wrap_content"
fresco:placeholderImage="@drawable/aa"
fresco:viewAspectRatio="2"
fresco:roundedCornerRadius="10dp"
/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
XML
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--不用ScrollView 会导致加载图标被覆盖-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="zzzzzzzzz"
android:textSize="20sp"
android:textStyle="bold" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:visibility="gone"
android:id="@+id/empty_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No items."
android:gravity="center"/>
JAVA:
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
//设置刷新时动画的颜色,可以设置4个
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.i("SwipeRefreshLayout","REFRESH");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("SwipeRefreshLayout", "REFRESH OK");
swipeRefreshLayout.setRefreshing(false);
}
}, 6000);
}
});
swipeRefreshLayout.setEmptyView(findViewById(R.id.empty_list)); 带上拉加载的 SwipeRefreshLayout
compile 'com.demievil.library:refreshlayout:1.0.0@aar' XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.demievil.library.RefreshLayout
android:id="@+id/my"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item" />
</com.demievil.library.RefreshLayout>
<TextView
android:visibility="gone"
android:id="@+id/empty_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No items."
android:gravity="center"
android:textColor="@color/colorPrimary"/>
</RelativeLayout>
JAVA:
public class MainActivity extends AppCompatActivity {
RefreshLayout mRefreshLayout;
ListView mListView;
View footerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRefreshLayout = (RefreshLayout) findViewById(R.id.my);
mListView = (ListView) findViewById(R.id.list);
footerLayout = getLayoutInflater().inflate(R.layout.listview_footer, null);
mListView.setEmptyView(findViewById(R.id.empty_list));
mListView.addFooterView(footerLayout);
mRefreshLayout.setChildView(mListView);
final ArrayAdapter apt= new myapt(this,R.layout.item);;
mListView.setAdapter(apt);
final LoaderManager.LoaderCallbacks loadercallback = new LoaderManager.LoaderCallbacks<List<Map<String,String>>>(){
@Override
public Loader<List<Map<String,String>>> onCreateLoader(int id, Bundle args) {
return new myloader(MainActivity.this,args);
}
@Override
public void onLoadFinished(Loader<List<Map<String,String>>> loader, List<Map<String,String>> data) {
apt.addAll(data);
mRefreshLayout.setRefreshing(false);
mRefreshLayout.setLoading(false);
}
@Override
public void onLoaderReset(Loader<List<Map<String,String>>> loader) {
apt.clear();
}
};
mRefreshLayout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// start to refresh
getLoaderManager().destroyLoader(1);
Bundle b = new Bundle();
b.putString("page","1");
getLoaderManager().initLoader(1, b, loadercallback);
}
});
mRefreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
Bundle b = new Bundle();
b.putString("page","2");
getLoaderManager().restartLoader(1,b,loadercallback);
}
});
Bundle b = new Bundle();
b.putString("page","1");
getLoaderManager().initLoader(1, b,loadercallback);
}
static class myloader extends AsyncTaskLoader<List<Map<String,String>>> {
Bundle args;
public myloader(Context context, Bundle args) {
super(context);
this.args=args;
}
Call call = null;
@Override
public List<Map<String,String>> loadInBackground() {
List<Map<String,String>> my= new ArrayList<Map<String,String>>(); OkHttpClient client= new OkHttpClient(); String url="http://wifiad-api.urltest.tk/index/test"; Request request = new Request.Builder().url(url).build();
Response response = null;
String c = null;
try {
call=client.newCall(request);
response = call.execute();
c = response.body().string();
} catch (Exception e) {
e.printStackTrace();
return my;
}
if(!response.isSuccessful()){
return my;
}
JSONTokener jsontoken= new JSONTokener(c);
JSONObject jsonobject = null;
JSONArray jsonarr;
try {
jsonobject = (JSONObject) jsontoken.nextValue();
jsonarr =jsonobject.getJSONArray("data");
for (int i=0;i<jsonarr.length();i++){
JSONObject t=(JSONObject)jsonarr.get(i);
Map<String,String> m1= new HashMap<String,String>();
m1.put("txt",t.getString("tit").toString());
m1.put("pic",t.getString("src").toString());
my.add(m1);
}
} catch (JSONException e) {
e.printStackTrace();
return my;
}
return my;
}
@Override
public void cancelLoadInBackground() {
call.cancel();
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
protected void onStopLoading() {
cancelLoad();
}
@Override
protected void onReset() {
super.onReset();
onStopLoading();
}
}
class myapt extends ArrayAdapter<Map<String,String>>{
int resource;
public myapt(Context context, int resource) {
super(context, resource);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
LayoutInflater inflater = getLayoutInflater();
if (convertView == null) {
view = inflater.inflate(resource, parent, false);
} else {
view = convertView;
}
Map<String,String> data = getItem(position); String c =data.get("pic"); if(c!=null) {
Uri uri = Uri.parse(c);
SimpleDraweeView draweeView = (SimpleDraweeView) view.findViewById(R.id.img);
draweeView.setImageURI(uri);
}
TextView viewById = (TextView) view.findViewById(R.id.txt);
viewById.setText(data.get("txt"));
return view;
}
}
} 数据绑定:
ITEM XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data class=".Userlist">
<import type="android.view.View" />
<import type="com.example.mode.User" alias="User" />
<variable name="handlers" type="com.example.work.dome1.Main2Activity"/>
<variable name="user" type="User"/>
<import type="android.databinding.ObservableMap"/>
<variable name="prs" type="ObservableMap<String, Object>"/>
</data>
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="50dp"
android:layout_height="100dp"
android:text='@{prs["index"], default="5"}'
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/index"
/>
<com.facebook.drawee.view.SimpleDraweeView
app:imageUrl="@{user.src}"
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="wrap_content"
fresco:placeholderImage="@drawable/aa"
fresco:viewAspectRatio="2"
fresco:roundedCornerRadius="10dp"
/>
<TextView
android:onClick="@{handlers.onClickFriend}"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/t1"
android:text="@{user.tit}"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
</LinearLayout>
</layout>
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.work.dome1.Main2Activity"> <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:visibility="gone"
android:id="@+id/empty_list"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="No items."
android:gravity="center"
android:textColor="@color/colorPrimary"/>
</RelativeLayout>
USER JAVA:
public class User extends BaseObservable {
private String tit;
private String src;
@Bindable
public String getTit() {
return tit;
}
public void setTit(String tit){
this.tit=tit;
notifyPropertyChanged(BR.tit);
}
@Bindable
public String getSrc(){
return src;
}
public void setSrc(String src){
this.src=src;
notifyPropertyChanged(BR.src);
}
@BindingAdapter({"bind:imageUrl"})
public static void loadImage(SimpleDraweeView view, String url) {
Uri uri = Uri.parse(url);
view.setImageURI(uri);
}
}
JAVA:
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView mlv = (ListView) findViewById(R.id.main2);
mlv.setEmptyView(findViewById(R.id.empty_list));
myapt myapt = new myapt(this, R.layout.main2);
mlv.setAdapter(myapt);
ProgressDialog dialog = new ProgressDialog(this);
myasync myasync = new myasync(myapt, dialog);
myasync.execute("page=1");
} public void onClickFriend(View view) {
Log.i("xxx","bbb");
} class myasync extends AsyncTask<String,Integer,ArrayList<User>>{
ProgressDialog dialog=null;
ArrayAdapter<User> apt;
public myasync(ArrayAdapter<User> apt,ProgressDialog dialog){
this.dialog=dialog;
this.apt=apt;
}
@Override
protected ArrayList<User> doInBackground(String... params) {
ArrayList<User> my= new ArrayList<User>();
OkHttpClient client= new OkHttpClient();
String url="http://wifiad-api.urltest.tk/index/test";
Request request = new Request.Builder().url(url).build();
Response response = null;
String c = null;
try {
Call call=client.newCall(request);
response = call.execute();
c = response.body().string();
} catch (Exception e) {
e.printStackTrace();
return my;
}
if(!response.isSuccessful()){
return my;
}
JSONTokener jsontoken= new JSONTokener(c);
JSONObject jsonobject = null;
JSONArray jsonarr;
try {
jsonobject = (JSONObject) jsontoken.nextValue();
jsonarr =jsonobject.getJSONArray("data");
for (int i=0;i<jsonarr.length();i++){
JSONObject t=(JSONObject)jsonarr.get(i);
User u= new User();
u.setTit(t.getString("tit").toString());
u.setSrc(t.getString("src").toString());
my.add(u);
}
} catch (JSONException e) {
e.printStackTrace();
return my;
}
return my;
}
@Override
protected void onPreExecute() {
if(dialog!=null) {
dialog.setMessage("加载中");
dialog.show();
}
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<User> s) {
if(dialog!=null)
dialog.hide();
super.onPostExecute(s);
apt.addAll(s);
}
}
class myapt extends ArrayAdapter<User>{
int resource;
public myapt(Context context, int resource) {
super(context, resource);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
LayoutInflater inflater = getLayoutInflater();
com.example.work.dome1.Userlist binding;
binding= DataBindingUtil.inflate(inflater,resource,parent,false);
ObservableArrayMap<String, Object> prs = new ObservableArrayMap<>();
binding.setPrs(prs);
User data = getItem(position);
binding.setUser(data);
return binding.getRoot();
}
}
} RecyclerView 列表
compile 'com.android.support:recyclerview-v7:23.1.1'
ITEM XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="New Text"
android:id="@+id/tt1"
android:gravity="center"
/>
</LinearLayout>
MENU XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add"
android:orderInCategory="100"
app:showAsAction="never"
android:title="添加"
/>
<item
android:id="@+id/del"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:title="删除"
/>
</menu>
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.work.dome1.Main3Activity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_test_rv"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
JAVA:
public class Main3Activity extends AppCompatActivity {
PersonAdapter mylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_test_rv);
//设置动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//设置布局
recyclerView.setLayoutManager(layoutManager);
ArrayList<String> list = new ArrayList<>();
for (int i=0;i<100;i++){
list.add("itemsss"+String.valueOf(i));
}
mylist = new PersonAdapter(list);
recyclerView.setAdapter(mylist);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
mylist.add(1,"new text");
break;
case R.id.del:
mylist.del(0);
break;
}
return super.onOptionsItemSelected(item);
}
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
private List<String> list;
public PersonAdapter(List<String> list) {
this.list = list;
}
public void add(int position,String listitem) {
list.add(position, listitem);
notifyItemInserted(position);
}
public void del(int position) {
list.remove(position);
notifyItemRemoved(position);
}
@Override
public PersonAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建视图
View view = getLayoutInflater().inflate(R.layout.citem, parent, false);
return new PersonViewHolder(view);
}
@Override
public void onBindViewHolder(PersonAdapter.PersonViewHolder viewHolder, int i) {
//把数据绑定到视图
PersonViewHolder holder = viewHolder;
String person = list.get(i);
holder.set_txt(person);
}
@Override
public int getItemCount() {
return list.size();
}
class PersonViewHolder extends RecyclerView.ViewHolder
{
public PersonViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("xxx", "xxx");
}
});
}
public void set_txt(String a){
TextView t1 = (TextView) itemView.findViewById(R.id.tt1);
t1.setText(a);
}
}
}
} 基于 RecyclerView 的上下拉
compile 'com.github.tosslife:pullloadview:1.1.0' SlidingPaneLayout 左右面板 [右边移动]
XML:
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/slidingpanellayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/leftfragment"
android:name="com.example.dome2.tabactivity$BookMarkerFragment"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<fragment
android:id="@+id/rightfragment"
android:name="com.example.dome2.tabactivity$BookMarkerFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1" />
</android.support.v4.widget.SlidingPaneLayout>
JAVA:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab); SlidingPaneLayout spl = (SlidingPaneLayout) this.findViewById(R.id.slidingpanellayout);
spl.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
@Override
public void onPanelClosed(View view) {
//面板打开
}
@Override
public void onPanelOpened(View viw) {
//面板关闭
}
@Override
public void onPanelSlide(View arg0, float arg1) { }
});
}
public static class BookMarkerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xxx, container, false);
return view;
}
}
public static class BookMarkerFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xxx, container, false);
return view;
}
} DrawerLayout 左右面板 [右边固定]
XML:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" > <!-- The main content view --> <FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open"
/>
</FrameLayout> <!-- The navigation drawer --> <ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
JAVA:
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 按钮按下,将抽屉打开
mDrawerLayout.openDrawer(Gravity.LEFT); }
}); FrameLayout 层叠显示 LinearLayout 线性排列 TableLayout TableRow 表格排列 GridLayout 格子 RelativeLayout 相对排列 只针对下级(一级)子元素有效
//相对同级视图 (值:视图ID)
layout_below 放到指定视图下面
layout_above 放到指定视图上面
layout_toLeftOf 放到指定视图左边
layout_toRightOf 放到指定视图右边
layout_alignTop 顶边与指定视图顶边对齐
layout_alignBottom 低边与指定视图低边对齐
layout_alignLeft 左边与指定视图左边对齐
layout_alignRight 右边与指定视图右边对齐
layout_alignBaseline 与指定视图基准线对齐 (如保持文字在同行) //相对父视图 (值:true false)
layout_alignParentTop 顶边与其父视图的顶边对齐
layout_alignParentBottom 底边与其父视图的底边对齐
layout_alignParentLeft 左边与其父视图的左边对齐
layout_alignParentRight 右边与其父视图的右边对齐
layout_centerHorizontal 在父视图中水平居中
layout_centerVertical 在父视图中垂直居中
layout_centerInParent 在父视图中的中央 LinearLayoutCompat 线性布局 每个组件间加 divider
XML:
<android.support.v7.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dip"
android:orientation="vertical"
app:divider="@drawable/line"
app:dividerPadding="5dp"
app:showDividers="beginning|middle|end" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CSDN Zhang Phil" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CSDN Zhang Phil" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CSDN Zhang Phil" />
</android.support.v7.widget.LinearLayoutCompat>

  

Android 常用布局视图的更多相关文章

  1. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

  2. Android常用布局和控件

    一.Android常用布局属性 1. LinearLayout的特有属性 android:orientation:设置布局排列方式   android:layout_weight:设置所占布局的权重  ...

  3. Android常用布局

    FrameLayout(框架布局):从屏幕的左上角开始显示对象,一个覆盖一个,主要用于选项卡视图和图像切换器.# 所有的组件都放在屏幕的左上角,并且以层叠进行显示. LinearLayout(线性布局 ...

  4. 【深入篇】Android常用布局方式简介

    LinearLayout 线性布局是程序中最常见的布局方式.一般分为水平线性布局和竖直线性布局,通过android.orientation属性可以设置线性布局的方向. 在布局中操作颜色时,要用的是十六 ...

  5. [Irving]Android 常用布局之RelativeLayout

    RelativeLayout相对布局 相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一. 它灵活性大很多,当然属性也多,操作 ...

  6. Android常用布局、文件存储与权限、XML

    常用的布局 LinearLayout Android 2.2开始fill_parent改名为match_parent ,从API Level为8开始我们可以直接用match_parent来代替fill ...

  7. Android常用布局属性解析 -- Layout_weight

    Layout_weight是Android开发中一个比较常用的布局属性,在面试中也经常被问到.下面通过实例彻底搞懂Layout_weight的用法. 先看下面的布局代码: <?xml versi ...

  8. Android 基础:常用布局 介绍 &amp; 使用(附 属性查询)

    Android 基础:常用布局 介绍 & 使用(附 属性查询)   前言 在 Android开发中,绘制UI时常需各种布局 今天,我将全面介绍Android开发中最常用的五大布局 含 Andr ...

  9. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

随机推荐

  1. fedora安装sublime text教程

    下载 http://pan.baidu.com/s/1eRkEegM 解压 终端中切换到下载文件的目录下,执行以下命令: sudo tar -jxvf sublime_text_3_build_308 ...

  2. 外部主机连接mysql服务器延时严重问题

    1.原因:由于mysql对连接的客户端进行DNS反向解析 2.禁用dns解析,在 /etc/my.cnf 中加入skip-name-resolve 3.反向解析说明: 所谓反向解析是这样的:mysql ...

  3. jquery中的on事件

    on()函数用于为指定元素的一个或多个事件绑定事件处理函数. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind(). delegate(). l ...

  4. ubuntu FTP服务安装

    //安装vsftp apt-get install vsftpd -y //增加账号 //1 查找 nologin位置 /usr/sbin/nologin useradd -d /alidata/ww ...

  5. vector成员函数解析

    vector线性集装箱,其元素颜格排序根据线性序列,和动态数组很阶段似,像阵列,它的元素被存储在连续的存储空间,这也意味着,我们不仅能够使用迭代器(iterator)访问元素,也可以用一个指针访问偏移 ...

  6. 利用阿里云Centos7建站过程

    以下可能不尽详述,如有问题欢迎指出 准备过程:1. 阿里云主机一台2.域名一个 3.github个人帐号开始: 1.以root帐号登录云主机 2.安装apache [root@192 ~]# yum ...

  7. Luogu P1381油滴扩展

    传送门 数据范围给的很小啊,n >= 0 && n <= 7,所以给了DFS生存的空间. 对于每一个油滴,可以说在它下一个油滴放置之前,当前的这个油滴的半径并不确定(但是对 ...

  8. Firefox 多行标签的解决方案分享

    更新了 Quantum 以后 TabMixPlus 就不能用了,我最中意的多行标签也没了,捣鼓了一下终于重新回来了. 指南: https://discourse.mozilla.org/t/tabs- ...

  9. chrome实用快捷键速记

    标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 无痕模式下打开新窗口 Ctrl + Shift + n 打开新的标签页,并跳转到该标签页 Ctrl + t 重新打开最后关闭的标签页,并 ...

  10. cmd使用notepad++为打开方式打开文件

    想放一个txt进入vstart中,但是又不想用系统自带的记事本打开,想在vstart中双击时使用notepad++打开. cmd命令如下: "D:\notepad++\notepad++.e ...