2016年7月20日星期三
2016年7月4日星期一
RecycleView 加载更多
| public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { |
| public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); | |
| private int previousTotal = 0; // The total number of items in the dataset after the last load | |
| private boolean loading = true; // True if we are still waiting for the last set of data to load. | |
| private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more. | |
| int firstVisibleItem, visibleItemCount, totalItemCount; | |
| private int current_page = 1; | |
| private LinearLayoutManager mLinearLayoutManager; | |
| public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { | |
| this.mLinearLayoutManager = linearLayoutManager; | |
| } | |
| @Override | |
| public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
| super.onScrolled(recyclerView, dx, dy); | |
| visibleItemCount = recyclerView.getChildCount(); | |
| totalItemCount = mLinearLayoutManager.getItemCount(); | |
| firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition(); | |
| if (loading) { | |
| if (totalItemCount > previousTotal) { | |
| loading = false; | |
| previousTotal = totalItemCount; | |
| } | |
| } | |
| if (!loading && (totalItemCount - visibleItemCount) | |
| <= (firstVisibleItem + visibleThreshold)) { | |
| // End has been reached | |
| // Do something | |
| current_page++; | |
| onLoadMore(current_page); | |
| loading = true; | |
| } | |
| } | |
| public abstract void onLoadMore(int current_page); | |
| } |
2016年6月16日星期四
注解使用 (@interface )
一、什么是注释
说起注释,得先提一提什么是元数据(metadata)。所谓元数据就是数据的数据。也就是说,元数据是描述数据的。就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义。而J2SE5.0中提供的注释就是java源代码的元数据,也就是说注释是描述java源代码的。在J2SE5.0中可以自定义注释。使用时在@后面跟注释的名字。
说起注释,得先提一提什么是元数据(metadata)。所谓元数据就是数据的数据。也就是说,元数据是描述数据的。就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义。而J2SE5.0中提供的注释就是java源代码的元数据,也就是说注释是描述java源代码的。在J2SE5.0中可以自定义注释。使用时在@后面跟注释的名字。
有关注解与自定义注解
参考
http://blog.csdn.net/junshuaizhang/article/details/8526244
http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
注解的 格式是 @interaface, 并且默认的值是 使用 default.
首先看看 他是怎么定义的
@Retention(RetentionPolicy.RUNTIME)
public @interface userinfo {
String name() default "";
String age() default "";
}
有个 @Retetion注解来定义范围,如果没有这个 那么在使用它时会报错,更多信息参考 上面的网页
之后再看看 在类中使用 注解的 代码
@userinfo(name="kirsong",age="30")
public class UserCl {
@userinfo(name="kirsongmethod",age="30")
public void outpu(){
}
public static void main(String[] args) {
Class<UserCl> c=UserCl.class;
try {
Method method=c.getMethod("outpu", new Class[]{});
boolean flag=UserCl.class.isAnnotationPresent(userinfo.class);
if(flag){
System.out.println("have annotation");
}
userinfo ui=(userinfo) method.getAnnotation(userinfo.class);
userinfo uime=(userinfo) c.getAnnotation(userinfo.class);
System.out.println(ui.name());
System.out.println(uime.name());
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
你会看到 该类中 分别对类声明以及类方法进行了 注解。
之后在main里 分别读取注解的值,如果你想读取 类声明部分的 值的话 就要使用
class.getAnnotation. 如果是 方法的注解值的话就是 method.getAnnotation
2016年6月1日星期三
android GoogleMap 简单使用
使用这个 需要在 google console里 生成自己的 API Key.网上很多
1. AndroidManifest.xml设置 权限与写入key值
2.获取 当前的所在坐标与名称
3.显示到 GoogleMap上并移动到 当前所在位置
>如果实现 OnMapReadyCallback的话,当地图准备完以后就会进入onMapReady
>SupportMapFragment语句中 把googlemap添加到 指定的id上, 也就是 R.id.map
R.id.map是一个 fragment
1. AndroidManifest.xml设置 权限与写入key值
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />
2.获取 当前的所在坐标与名称
public abstract class GoogleMapLocation { Timer timer1; LocationManager lm; boolean gps_enabled = false; boolean network_enabled = false; String provide = null; private Context context; Geocoder mCoder; public GoogleMapLocation(Context context){ this.context=context; mCoder=new Geocoder(context, Locale.KOREA); getLocation(); } /** * 위치를 가져온다 * @return: true가져오기 성공,false;실패 */ public boolean getLocation() { //I use LocationResult callback class to pass location value from MyLocation to user code. if (lm == null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //exceptions will be thrown if provider is not permitted. try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { } try { network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { } //don't start listeners if no provider is enabled if (!gps_enabled && !network_enabled) return false; if (gps_enabled) { try { lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } provide = "gps"; } if (network_enabled) { try { lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } provide = "network"; } timer1 = new Timer(); timer1.schedule(new GetLastLocation(), 10); return true; } LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); gotLocation(location); try { lm.removeUpdates(this); lm.removeUpdates(locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; LocationListener locationListenerNetwork = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); gotLocation(location); try { lm.removeUpdates(this); lm.removeUpdates(locationListenerGps); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; class GetLastLocation extends TimerTask { @Override public void run() { try { lm.removeUpdates(locationListenerGps); lm.removeUpdates(locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } Location net_loc = null, gps_loc = null; try { if (gps_enabled) gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (network_enabled) net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } //if there are both values use the latest one if (gps_loc != null && net_loc != null) { if (gps_loc.getTime() > net_loc.getTime()) gotLocation(gps_loc); else gotLocation(net_loc); return; } Message messag=new Message(); if (gps_loc != null) { messag.obj=gps_loc; handler.sendMessage(messag); return; } if (net_loc != null) { messag.obj=net_loc; handler.sendMessage(messag); return; } messag.obj=null; handler.sendMessage(messag); } } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Location location= (Location) msg.obj; gotLocation(location); locationAddr(location); } }; /** * 위치 내보내기 * @param location:위치 값 */ public abstract void gotLocation(Location location); /** * 주소 명칭 반환 * @param addr */ public abstract void getLocaionAddr(String addr); private String locationAddr(Location location){ String sLocationInfo=""; if (location!=null){ try { List<Address> addresses=mCoder.getFromLocation(location.getLatitude(),location.getLongitude(),1); if (addresses!=null){ Address addr=addresses.get(0); for (int i = 0; i <= addr.getMaxAddressLineIndex(); i++) { String addLine = addr.getAddressLine(i); sLocationInfo += String.format("%s", addLine); } getLocaionAddr(sLocationInfo); } }catch (IOException e){ } } return sLocationInfo; } }
3.显示到 GoogleMap上并移动到 当前所在位置
>如果实现 OnMapReadyCallback的话,当地图准备完以后就会进入onMapReady
>SupportMapFragment语句中 把googlemap添加到 指定的id上, 也就是 R.id.map
R.id.map是一个 fragment
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap; @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); }
@Overridepublic void onMapReady(GoogleMap googleMap) { mMap = googleMap; LocationResult locationResult = new LocationResult() { @Override public void gotLocation(Location location) { String msg = "lon: "+location.getLongitude()+" -- lat: "+location.getLatitude(); Log.i("map", msg); drawMarker(location); } }; MyLocation myLocation = new MyLocation(); myLocation.getLocation(getApplicationContext(), locationResult);}
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="mipl.amia.com.googlemapsample.MapsActivity" />
2016年5月24日星期二
自定义RatingBar,不同分辨率屏幕下图片拉伸或者显示不完整问题解决
<resources>
<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/my_rating_bar</item>
<item name="android:minHeight">16dip</item>
<item name="android:maxHeight">16dip</item>
</style>
</resources>
如果需要适配多分辨率多屏幕密度的情况下,android:minHeight和 android:maxHeight这两个属性不管设置多大都不合适。一种屏幕合适了,在另外一个屏幕上,就可能显示不完整或者图片被拉伸。
解决的方法如下:
step 1:将
这两张图片分别拷贝到以下文件夹;
setp 2:修改android:minHeight 和 android:maxHeight 这两个属性的值为图片的实际高度,用px为单位。假设图片的尺寸为 36x30,修改后如下:
<resources>
<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/room_rating_bar</item>
<item name="android:minHeight">30px</item>
<item name="android:maxHeight">30px</item>
</style>
</resources>
修改完毕,在不同分辨率的手机下,都能正常显示。
注意:有的同学在替换图片后会发现,星星之间的挨的太近,没有间距。这个时候可以在切图的时候,在星星图片的左右两边都加上几个像素的透明边距。
订阅:
博文 (Atom)