键盘相关
显示键盘
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
etContent.setFocusable(true);
etContent.setFocusableInTouchMode(true);
etContent.requestFocus();
imm.showSoftInput(etContent,0);
隐藏键盘
imm.hideSoftInputFromWindow(etContent.getWindowToken(), 0);
类似微信的时间显示格式
public class ShowDateTime {
private int hour;
private int day;
private Date date;
private DateFormat df;
private String pointText;
private Long time;
private Long now;
// private
public ShowDateTime() {
hour = 60 * 60 * 1000;
day = (60 * 60 * 24) * 1000;
time = 28800L;
now = new Date().getTime();
pointText = "1970-01-01";
}
// 获得当天0点时间
public static Long getTimesmorning() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (Long) cal.getTimeInMillis();
}
// 获取星期几
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
// 获取时间点
public String getTimePoint(Long time) {
// 现在时间
now = new Date().getTime();
// 时间点比当天零点早
if (time <= now && time != null) {
date = new Date(time);
if (time < getTimesmorning()) {
if (time >= getTimesmorning() - day) {// 比昨天零点晚
pointText = "yday";
return pointText;
} else {// 比昨天零点早
if (time >= getTimesmorning() - 6 * day) {// 比七天前的零点晚,显示星期几
return getWeekOfDate(date);
} else {// 显示具体日期
String systemLanguage = PreferenceUtil.getSystemSpString(PreferenceUtil.SYSTEM_LANGUAGE);
if(Locale.JAPAN.getLanguage().equals(systemLanguage)){ /*日语*/
df = new SimpleDateFormat("yyyy.M.d");
}else if(Locale.CHINESE.getLanguage().equals(systemLanguage)){/*简体中文*/
df = new SimpleDateFormat("yyyy-MM-dd");
}else{ /*英语*/
df = new SimpleDateFormat("yyyy-MM-dd");
}
pointText = df.format(date);
return pointText;
}
}
} else {// 无日期时间,当天内具体时间
df = new SimpleDateFormat("HH:mm");
pointText = df.format(date);
return pointText;
}
}
return pointText;
}
}
万能适配器
public abstract class CommonAdapter<B> extends RecyclerView.Adapter<ViewHolder> {
protected List<B> mBeanList;
protected int mLayoutId;
protected Context mContext;
public CommonAdapter(Context context, List<B> beanList, int layoutId) {
mBeanList = beanList;
mLayoutId = layoutId;
mContext = context;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return ViewHolder.newInstnace(mContext, mLayoutId, parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
B bean = mBeanList.get(position);
convert(holder,bean, position);
}
public abstract void convert(ViewHolder holder,B bean, int position);
@Override
public int getItemCount() {
return mBeanList.size();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mConvertView;
private SparseArray<View> mViews;
public ViewHolder(View itemView) {
super(itemView);
mConvertView = itemView;
mViews = new SparseArray<>();
}
public static ViewHolder newInstnace(Context context, int layoutId, ViewGroup parentView) {
View view = LayoutInflater.from(context).inflate(layoutId, parentView, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public ViewHolder setText(int viewId, String text) {
TextView textView = getView(viewId);
if(!TextUtils.isEmpty(text)){
textView.setText(text);
}
return this;
}
public ViewHolder setImageDrawable(int viewId,String imageUrl){
ImageView imageView = getView(viewId);
Glide.with(imageView.getContext()).load(imageUrl).into(imageView);
return this;
}
}
RecyclerView嵌套ScrollView
因为添加了上拉刷新布局以后,无法滑动RecyclerView。
二级列表
public class AddVideoExpandAdapter extends BaseExpandableListAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<FollowMineBean> mBeanList;
public AddVideoExpandAdapter(Context context, List<FollowMineBean> beanList) {
this.mContext = context;
this.mBeanList = beanList;
mInflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return mBeanList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return mBeanList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mBeanList.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
VideoFirstViewHolder viewHolder;
if (convertView == null) {
viewHolder = new VideoFirstViewHolder();
convertView = mInflater.inflate(R.layout.item_addvideo_first, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (VideoFirstViewHolder) convertView.getTag();
}
return convertView;
}
private static class VideoFirstViewHolder {
ImageView ivHead;
TextView tvName;
TextView tvEvents;
TextView tvLocation;
ImageView ivAdopt;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
VideoSecondViewHolder viewHolder;
if (convertView == null) {
viewHolder = new VideoSecondViewHolder();
convertView = mInflater.inflate(R.layout.item_addvideo_second, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (VideoSecondViewHolder) convertView.getTag();
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private static class VideoSecondViewHolder {
}
}
播放器 第二次播放失败
我在RelativeLayout中放了一个播放器,然后给此相对布局绑定了一个事件,然后点击播放。
第一次时候没有问题可以成功播放。但是第二次再点击直接报错了:
ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage,inputElapseTime=2 eventTime = 478624690 downTime = 478624690
我怀疑是播放器播放以后拦截了点击事件。所以我还用了自定义的布局
选择本地视频
//去选择一个视频
private void pickFromGallery() {
//授权了以后 去选择视频
Intent intent = new Intent();
intent.setTypeAndNormalize("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_video)), REQUEST_SELECT_VIDEO);
}
//==========================================================选择视频回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_SELECT_VIDEO) {
final Uri selectedUri = data.getData();
if (selectedUri != null) {
String videoPath = FileUtils.getPath(this, selectedUri);
}
}
}
}
Dialog全屏显示
定义style
<style name="Dialog_FullScreen">
<!--去掉状态栏-->
<item name="android:windowFullscreen">true</item>
<!--全屏-->
<item name="android:windowNoTitle">true</item>
</style>
在setContentView之前调用
super(context,R.style.Dialog_FullScreen);
RecyclerView滑动到最后且最后一个item显示完全
添加一个item时,调用下面这一行代码
layoutManager.scrollToPositionWithOffset(beanList.size() - 1,0);
场景
让文件夹路径的最后一个文件名称能显示出来。
我试了scrollToPosition()方式,不能让最后一个显示完全。
另外还要把RecyclerView的宽度高位wrap_content的。否则如果只有一个item,也会从最后一个开始显示。